#! /bin/sh
#
# cfg-src -- Copyright (c) 2002 Eberhard Mattes
#
# This file is part of epocemx.
#
# epocemx is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# epocemx is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with epocemx; see the file COPYING.  If not, write to the
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

die()
{
  echo "*** $@"
  exit 1
}

usage()
{
  echo 'Usage:'
  echo '  cfg-src -set PATH'
  echo '  cfg-src -show'
  echo '  cfg-src -unset'
  exit 1
}

do_set()
{
  test -x build || die "./build: not found or not executable"
  test $# = 2 || usage
  test -d "$2" || die "$2: not a directory"
  ok=
  for x in `./build -archives`
  do
    test -f $2/$x.tar.gz && ok=yes
  done
  test -n "$ok" || die "$2: does not contain any relevant source archives"
  rm -f SOURCE
  ln -s $2 SOURCE || "ln failed"
  echo OK
}

do_show()
{
  test $# = 1 || usage
  test -d SOURCE || die 'No source path set'
  ls -ld SOURCE
}

do_unset()
{
  test $# = 1 || usage
  rm -f SOURCE
  echo OK
}

case "$1" in
  -set)   do_set "$@";;
  -show)  do_show "$@";;
  -unset) do_unset "$@";;
  *)     usage
esac
