#
#	aegis - project change supervisor
#	Copyright (C) 2004, 2006-2008 Peter Miller
#
#	This program 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 3 of the License, or
#	(at your option) any later version.
#
#	This program 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 this program. If not, see
#	<http://www.gnu.org/licenses/>.
#
case $# in
1)
    AEGIS_PROJECT=$1
    prefix=@prefix@
    bindir=@bindir@
    libdir=@libdir@
    datadir=@datadir@
    datarootdir=@datarootdir@
    ;;
2)
    AEGIS_PROJECT=$1
    prefix=$2
    bindir=$prefix/bin
    case "$prefix" in
    */aegis/*)
	libdir=$prefix/lib
	datadir=$prefix/share
	;;
    *)
	libdir=$prefix/lib/aegis
	datadir=$prefix/share/aegis
	;;
    esac
    ;;
*)
    echo "Usage: $0 <project> [ <prefix> ]" 1>&2
    exit 1
    ;;
esac

export AEGIS_PROJECT

# find the architecture
arch=`aesub -bl '$arch'`
test $? -eq 0 || exit 1
echo "arch=$arch"

# find the baseline
baseline=`aesub -bl '$baseline'`
test $? -eq 0 || exit 1
echo "baseline=$baseline"

#
# Copy binaries to $bindir
#
echo ${bindir}...
mkdir -p $bindir
test $? -eq 0 || exit 1
cd $bindir
ls $baseline/$arch/bin |
while read f
do
    case $f in
    aemakefile | \
    aefp | aemanifest | ae-symlinks | cklinlen | fmtgen | test_* )
	# don't install these ones.
	;;

    *)
	rm -f $f
	ln -s $baseline/$arch/bin/$f $f
	test $? -eq 0 || exit 1
	;;
    esac
done

echo ${libdir}...
mkdir -p $libdir
test $? -eq 0 || exit 1
cd $libdir
for f in en
do
    rm -rf $f
    ln -s $baseline/$arch/lib/$f $f
    test $? -eq 0 || exit 1
done
for f in profile cshrc
do
    rm -f $f
    ln -s $baseline/$arch/lib/$f $f
    test $? -eq 0 || exit 1
done

echo ${datadir}...
mkdir -p $datadir
test $? -eq 0 || exit 1
cd $datadir
for f in en/html report config.example wish
do
    rm -rf $f
    mkdir -p $f
    ln -s `$bindir/aefind -p $AEGIS_PROJECT -bl -baserel lib/$f \
	-type f ! -name "*,*" ! -name "*.d" -print -resolve` $f/.
    test $? -eq 0 || exit 1
done
for f in report.index aegis.icon aegis.mask
do
    rm -f $f
    ln -s `$bindir/aefind -p $AEGIS_PROJECT -bl -baserel lib/$f \
	-type f ! -name "*,*" ! -name "*.d" -print -resolve` $f
    test $? -eq 0 || exit 1
done
