#!/bin/sh 

#
# Configuration directories:
# 

echo 'Setting up config files (under /vice).'

if [ ! -d /vice ]; then
    mkdir /vice
fi

cd /vice

for i in backup db srv vol bin auth2 ; do
    if [ ! -d $i ]; then
       mkdir $i
    fi
done

if [ ! -d vol/remote ]; then
    mkdir vol/remote
fi

echo Directories under /vice are set up.
echo

#
# tokens
#
echo Setting up tokens for authentication.

cd /vice/db

for which in auth2 volutil; do
    token=""
    until [ x$token != x ]; do
	echo "The following tokens must be identical on all servers."
	echo -n "Enter a random token for $which authentication : "
	read token
	if [ x$token != x ]; then
            rm -f $which.tk
	    touch $which.tk
	    chmod 600 $which.tk
	    echo $token >> $which.tk
        fi
    done
done
echo tokens done!

#
# files file for update
#

echo
echo Setting up the file list for update
cat > /vice/db/files <<EOF
ROOTVOLUME
VLDB
auth2.pw
auth2.tk
auth2.tk.BAK
pro.db
servers
vice.pdb
vice.pcf
volutil.tk
VRDB
files
VSGDB
dumplist
EOF

echo filelist for update ready. 

#
# populating /vice/vol
#

echo
echo Populating /vice/vol...
touch fs.lock
touch volutil.lock
echo lockfiles created.

#
# adding stuff to /etc/services if needed
#

echo 
if grep coda_filesrv /etc/services 1> /dev/null 2> /dev/null ; then 
    echo Services already present. Good.
else
    echo Adding services to /etc/services.
    cat  >> /etc/services <<EOF
# Coda server port numbers
coda_opcons     1355/udp                        # Coda opcons
coda_auth       1357/udp                        # Coda auth
coda_udpsrv     1359/udp                        # Coda updsrv
coda_filesrv    1361/udp                        # Coda filesrv
coda_venus      1363/udp                        # Coda venus
EOF
echo /etc/services set up.
fi


#
# startup scripts
#
CODABASE=
case x`uname` in
  xNetBSD)
	CODABASE=/usr/pkg
	;;
  xFreeBSD)
	CODABASE=/usr/local
	;;
esac

if [ x$CODABASE != x ]; then
yesno=""
until [ x$yesno != x ]; do
   echo -n 'Do you want to start the server at boot time? (y/n) '
   read yesno
   if [ x$yesno = xy  -o  x$yesno = xyes -o x$yesno = xY ]; then
       touch /vice/srv/STARTFROMBOOT
       grep "${CODABASE}/etc/rc.vice" /etc/rc.local > /dev/null 2>1
       if [ $? != 0 ]; then 
         echo "if [ -x ${CODABASE}/etc/rc.vice ]; then ${CODABASE}/etc/rc.vice start ; fi" >> /etc/rc.local 
       fi
       echo "Startup scripts now installed."
   fi
done
fi

#
# /vice/hostname 
# 
hn=`hostname`
bn=${hn%%.*}
# to prevent killing the hostname if it wasn't fully qualified.
if [ x$bn = x ]; then
    bn=$hn
fi

echo $bn > /vice/hostname

#
# end of common setup area, now specialize to scm or non scm
# 

isscm=N
yesno=""
until [ x$yesno != x ]; do
   echo -n 'Is this the master server, aka the SCM machine? (y/n) '
   read yesno
   if [ x$yesno = xy  -o  x$yesno = xyes ]; then
      isscm=Y
      echo -n "Now installing files specific to the SCM..."
   else
      echo -n "Now installing things specific to non-SCM mahines..."
   fi
done


if [ $isscm = Y ]; then
    vice-setup-scm
    vice-setup-user
    vice-setup-rvm
    vice-setup-srvdir
    exit 0 
fi

#
# specifics for non SCM servers only 
# 

until [ x$scmhost != x ]; do
    echo -n "Enter the hostname of the SCM machine : "
    read scmhost
    if [ x$scmhost != x ]; then
       echo $scmhost > /vice/db/scm
    fi
done

vice-setup-rvm
vice-setup-srvdir

echo "You have set up " `cat /vice/hostname`
echo "Your scm is " `cat /vice/db/scm`
echo "Other config files will be fetched from the SCM by updateclnt."






