#!/bin/sh

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

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

	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

	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 /usr/sbin
		if [ $? -eq 0 ] ; then
			echo "OK"
		else
			echo "Error copying fbb. Giving up !"
			echo
			exit 1
		fi
		chmod +x /usr/sbin/fbb
		for FILE in clr_user epurmess epurwp maintinf reqdir xfbbC xfbbd
		do
			echo -n "  Copying $FILE ... "
 			cp $SRC/$FILE /usr/sbin
			if [ $? -eq 0 ] ; then
				echo "OK"
			else
				echo "Error copying src/$FILE. Giving up !"
				echo
				exit 1
			fi
			chmod +x /usr/sbin/$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
			fi
		done
		for FILE in $CONF/lang/*
		do
			if [ ! -f $CONFDIR/lang/$FILE ] ; then
				echo -n "  Copying `basename $FILE` ... "
	 			cp $FILE $CONFDIR/lang
				if [ $? -eq 0 ] ; then
					echo "OK"
				else
					echo "Error !!"
				fi
			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 erase binaries and configuration files."
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
