#!/bin/sh

CONFDIR=$DESTDIR/etc/ax25/fbb
DATADIR=$DESTDIR/var/ax25/fbb
DOSDIR=$DATADIR/fbbdos
LIBDIR=$DESTDIR/usr/lib/fbb
SBINDIR=$DESTDIR/usr/sbin

fbb_tree()
{
	echo
	echo "creating configuration tree ..."
	echo "  $CONFDIR"
	echo "  $CONFDIR/lang"
	mkdir -p $CONFDIR/lang

	echo "creating pg directory ..."
	echo "  $LIBDIR/pg"
	mkdir -p $LIBDIR/pg

	echo "creating script directory ..."
	echo "  $LIBDIR/script"
	mkdir -p $LIBDIR/script
	
	for hk in maintenance rerun end_session stop_system error_return
	do
		echo "    $LIBDIR/script/$hk"
		if [ -e $LIBDIR/script/$k ] ; then
			if [ ! -d $LIBDIR/script/$hk ] ; then
				echo "file $LIBDIR/script/$hk found and renamed $LIBDIR/script/$hk.old"
				mv $LIBDIR/script/$hk $LIBDIR/script/$hk.old
				mkdir $LIBDIR/script/$hk
			fi
		else
			mkdir -p $LIBDIR/script/$hk
		fi
	done

	echo "creating filters directory ..."
	echo "  $LIBDIR/filter"
	mkdir -p $LIBDIR/filter

	echo "creating servers directory ..."
	echo "  $LIBDIR/server"
	mkdir -p $LIBDIR/server

	echo "creating tools directory ..."
	echo "  $LIBDIR/tool"
	mkdir -p $LIBDIR/tool

	echo "creating fbbdos directory ..."
	echo "  $DOSDIR"
	echo "  $DOSDIR/yapp"
	mkdir -p $DOSDIR/yapp

	if [ ! -d $SBINDIR ] ; then
		echo "creating sbin directory ..."
		echo "  $SBINDIR"
		mkdir -p $SBINDIR
	fi
	
	echo "creating data tree ..."
	echo "  $DATADIR"
	for n in 0 1 2 3 4 5 6 7 8 9 ; do
		echo "  $DATADIR/mail/mail$n"
		mkdir -p $DATADIR/mail/mail$n
	done

	for n in 0 1 2 3 4 5 6 7 8 9 ; do
		echo "  $DATADIR/binmail/mail$n"
		mkdir -p $DATADIR/binmail/mail$n
	done

	echo "  $DATADIR/docs"
	mkdir -p $DATADIR/docs
	echo "  $DATADIR/sat"
	mkdir -p $DATADIR/sat
	echo "  $DATADIR/wp"
	mkdir -p $DATADIR/wp
	echo "  $DATADIR/log"
	mkdir -p $DATADIR/log
	echo "  $DATADIR/oldmail"
	mkdir -p $DATADIR/oldmail
	
	echo "Done"
}

copy_files()
{	
	if [ -d $SRC ] ; then
		echo
		echo "Copying binary files"
		echo -n "  Copying fbb ... "
		cp fbb $SBINDIR
		if [ $? -eq 0 ] ; then
			echo "OK"
		else
			echo "Error copying fbb. Giving up !"
			echo
			exit 1
		fi
		chmod +x $SBINDIR/fbb
		for FILE in xfbbC xfbbd
		do
			echo -n "  Copying $FILE ... "
 			cp $SRC/$FILE $SBINDIR
			if [ $? -eq 0 ] ; then
				echo "OK"
			else
				echo "Error copying src/$FILE. Giving up !"
				echo
				exit 1
			fi
			chmod +x $SBINDIR/$FILE
		done
	fi
	for FILE in 20_epurmess 20_epurwp 20_satupdat; do
		if [ -f $LIBDIR/script/maintenance/$FILE ] ; then
			echo "  Skipping $FILE ... OK"
		else
			cp $FILE $LIBDIR/script/maintenance/$FILE
			chmod +x $LIBDIR/script/maintenance/$FILE
		fi
	done
	if [ -d $LIBDIR/tool ] ; then
		for FILE in clr_user epurmess epurwp maintinf ; do
			cp $SRC/$FILE $LIBDIR/tool
			chmod +x $LIBDIR/tool/$FILE
		done
	fi
	if [ -d $LIBDIR/server ] ; then
		for FILE in reqdir ; do
			cp $SRC/$FILE $LIBDIR/server
			chmod +x $LIBDIR/server/$FILE
		done
	fi
	if [ -d $CONF ] ; then
		echo
		echo "Copying configuration files"
		for FILE in \
				bbs.sys cron.sys forward.sys \
				langue.sys passwd.sys protect.sys \
				redist.sys reject.sys swapp.sys themes.sys
		do
			if [ ! -f $CONFDIR/$FILE ] ; then
				echo -n "  Copying $FILE ... "
	 			cp $CONF/$FILE $CONFDIR
				if [ $? -eq 0 ] ; then
					echo "OK"
				else
					echo "Error !!"
				fi
			else
				echo "  Skipping $FILE ... OK"
			fi
		done
		for FILE in $CONF/lang/*
		do
			F=`basename $FILE`
			if [ ! -f $CONFDIR/lang/$F ] ; then
				echo -n "  Copying `basename $F` ... "
	 			cp $FILE $CONFDIR/lang
				if [ $? -eq 0 ] ; then
					echo "OK"
				else
					echo "Error !!"
				fi
			else
				echo "  Skipping $F ... OK"
			fi
		done
		echo "Done"
	else
		echo
		echo "Warning : original configuration files not found in $CONF"
	fi
}

echo 
echo "FBB installation tool"
echo 
echo "Warning ! This will replace binaries. Configuration files will not be replaced."
echo -n "Go on anyway ? (Y/N) "
read REP
if [ "$REP" = "N" -o "$REP" = "n" ] ; then
   	exit 0
fi

echo

CONF=`dirname $0`/conf
SRC=`dirname $0`/src

fbb_tree
copy_files

echo
echo "Installation done"
echo

exit 0
