#!/bin/sh 
# BLURB gpl
# 
#                            Coda File System
#                               Release 5
# 
#           Copyright (c) 1987-1999 Carnegie Mellon University
#                   Additional copyrights listed below
# 
# This  code  is  distributed "AS IS" without warranty of any kind under
# the terms of the GNU General Public Licence Version 2, as shown in the
# file  LICENSE.  The  technical and financial  contributors to Coda are
# listed in the file CREDITS.
# 
#                         Additional copyrights
#                            none currently
# 
#*/

echon() {
    if [ "`echo -n`" = "-n" ] ; then
        echo "$@"'\c'
    else
        echo -n "$@"
    fi
}

#
# arguments
#
if [ $# = 2 ] ; then
    if [ x$1 != x-n -o x$2 != x`expr $2 + 0 2>/dev/null` ] ; then
        echo "Usage: $0 [-n number-of-server-processes]"
        exit 1
    fi
    numservers=$2
else
    if [ $# != 0 ] ; then
        echo "Usage: $0 [-n number-of-server-processes]"
        exit 1
    else
        numservers=1
    fi
fi

#
# Greeting!
#

echo "Welcome to the Coda Server Setup script!"
echo

#
# initial information
#
conf="`codaconfedit server.conf`"
if [ "x$conf" != x/dev/null ] ; then
    yesno=""
    echo "You already have a file $conf!"
    echo "Continueing will remove that file."
    while [ x$yesno = x ]; do
	echon "Do you want to continue? [yes/no] "
	read yesno
	if [ x$yesno = xno -o x$yesno = xNO ] ; then
	    echo "Not configuring a coda server!"
	    exit 1
	fi
	if [ x$yesno != xyes -a x$yesno != xYES ] ; then
	    yesno=""
	    echo "Please answer yes or no."
	fi
    done
    if ! rm $conf ; then
	echo "Could not remove $conf.  Exiting."
	exit 1
    fi
fi

#
# Configuration directories:
# 

echo 'Setting up config files for a coda server.'

# figure out where the server.conf template, and thus the new server.conf
# file are located.
conf_template="`codaconfedit server.conf.ex`"
if [ "x$conf_template" = x/dev/null ] ; then
    echo "Could not find the server.conf.ex template file"
    exit 1
fi
conf_directory="`dirname $conf_template`"
conf="${conf_directory}/`basename $conf_template .ex`"

echon "Do you want the file $conf created? [yes] "
yesno=""
read yesno
if [ x$yesno = x -o x$yesno = xyes -o x$yesno = xy ] ; then
  mkconf=1
  mkdir -p "$conf_directory" 2>/dev/null
else
  mkconf=0
  if [ $numservers != 1 ]; then
    echo "This script can not setup multiple servers without using $conf."
    exit 1
  fi
fi

echon "What is the root directory for your coda server(s)? [/vice] "
vicedir=""
read vicedir

if [ "x$vicedir" = x ] ; then
   vicedir=/vice
fi

if [ $mkconf = 1 ] ; then
   codaconfedit server.conf vicedir $vicedir
   codaconfedit server.conf numservers $numservers
fi

echo "Setting up $vicedir."
if [ ! -d "$vicedir" ]; then
    mkdir -p "$vicedir"
fi

for i in auth2 backup db misc vol/remote ; do
    if [ ! -d "${vicedir}/$i" ]; then
	mkdir -p "${vicedir}/$i"
    fi
done

if [ $numservers = 1 ]; then
    if [ ! -d "${vicedir}/srv" ]; then
	mkdir ${vicedir}/srv
    fi
else
    n=1
    while [ $n -le $numservers ]; do

	sconf="${conf_directory}/server_$n.conf"
        dir="${vicedir}/server_$n"

	echo
	hostname=""
	while [ x$hostname = x ]; do
	    echon "Hostname for server $n: "
	    read hostname
	done

	cat > $sconf << EOF
hostname="${hostname}"
#rvm_log=""
#rvm_data=""
#rvm_data_length=""
#mapprivate=""
EOF

	echo Creating $dir and subdirectories.
	mkdir "$dir"
        for i in backup srv vol ; do
            if [ ! -d $dir/$i ]; then
                mkdir $dir/$i
            fi
        done

	n=`expr $n + 1`
    done
fi

echo Directories under $vicedir are set up.
echo

#
# Setup .../db
#
cd ${vicedir}/db

#
# Ask about scm and tokens....
# 

isscm=N
yesno=""
until [ "x$yesno" != x ]; do
   echon 'Is this the master server, aka the SCM machine? (y/n) '
   read yesno
done
if [ "x$yesno" = xy -o "x$yesno" = xyes -o "x$yesno" = xY ]; then
   isscm=Y
fi

if [ $isscm = N ]; then
    until [ "x$scmhost" != x ]; do
	echon "Enter the hostname of the SCM machine : "
	read scmhost
    done
    echo $scmhost > scm

    token=""
    until [ "x$token" != x ]; do
        echon "Enter the update token that matches SCM $scmhost: "
        read token
    done

    rm -f update.tk
    touch update.tk
    chmod 600 update.tk
    echo "$token" >> update.tk

    if ! updatefetch -h $scmhost -r db/servers -l servers ; then
	echo "Could not contact SCM."
	echo "Check whether the rpc2portmap and updatesrv processes are"
	echo "running on the SCM."
	exit 1
    fi

    echo "Fetching needed files from SCM $scmhost."
    for which in ROOTVOLUME auth2.pw auth2.tk auth2.lock \
	prot_users.db prot_index.db volutil.tk files ; do
        if ! updatefetch -h $scmhost -r db/$which -l $which ; then
	    echo "Could not fetch $which from SCM.  Make sure SCM is setup"
	    echo "correctly and then rerun $0."
	    exit 1
	fi
    done
    echo "Done."

else

    echo
    echo "Setting up tokens for authentication."

    for which in update auth2 volutil; do
	token=""
	until [ "x$token" != x ]; do
	    echo "The following token must be identical on all servers."
	    echon "Enter a random token for $which authentication : "
	    read token
	done

	rm -f $which.tk
	touch $which.tk
	chmod 600 $which.tk
	echo "$token" >> $which.tk
    done
    echo tokens done!

    touch auth2.lock

#
# files file for update
#

    echo
    echo Setting up the file list for update client
    cat > ${vicedir}/db/files <<EOF
auth2.pw
auth2.tk
auth2.tk.BAK
auth2.lock
dumplist
files
prot_users.db
prot_index.db
servers
scm
update.tk
volutil.tk
ROOTVOLUME
VLDB
VRDB
VRList
maxgroupid
VSGDB
EOF

    echo Filelist for update ready. 

fi

# add a couple of empty files to avoid updateclnt problems
for f in auth2.tk.BAK dumplist VLDB VRDB VRList ; do
    if [ ! -f "${vicedir}/db/$f" ]; then
	touch "${vicedir}/db/$f"
    fi
done

#
# adding stuff to /etc/services if needed
#
coda-setup-ports

#
# startup scripts
#
case "x`uname`" in
  x*BSD)
    yesno=""
    until [ x$yesno != x ]; do
	echon 'Do you want to start the server at boot time? (y/n) '
	read yesno
    done
    if [ "x$yesno" = xy  -o  "x$yesno" = xyes -o "x$yesno" = xY ]; then
	touch ${vicedir}/srv/STARTFROMBOOT
	grep "/usr/etc/rc.vice" /etc/rc.local > /dev/null 2>&1
	if [ $? != 0 ]; then 
	    echo "if [ -x /usr/etc/rc.vice ]; then /usr/etc/rc.vice start ; fi" >> /etc/rc.local 
	fi
	echo "Startup scripts now installed."
    else
        echo "You will have to start your coda server by hand."
    fi
    ;;
esac

#
# ${vicedir}/hostname 
# 
hn=`hostname`
bn=`echo $hn | cut -f 1 -d .`
# to prevent killing the hostname if it wasn't fully qualified.
if [ x$bn = x ]; then
    bn=$hn
fi

echo $bn > ${vicedir}/hostname

#
# server.conf standard 
#
if [ $mkconf = 1 ] ; then
    codaconfedit server.conf rvmtruncate 5
    codaconfedit server.conf trace 100
fi

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


if [ $isscm = Y ]; then
    echo  "Now installing files specific to the SCM..."
    # files.export ...
    echo "db" >  ${vicedir}/db/files.export
    for n in `cat ${vicedir}/db/files` ; do
      echo "db/$n" >>  ${vicedir}/db/files.export
    done
    if ! ( vice-setup-scm && vice-setup-user && vice-setup-rvm && \
           vice-setup-srvdir ) ; then
	echo "Your coda server is not completely setup.  You will need"
	echo "set it up by hand or fix the problems and rerun $0."
	exit 1
    fi
    RV=`cat ${vicedir}/db/ROOTVOLUME`
    DP=`cat ${vicedir}/db/vicetab | awk '{print $2}' | head -n 1`
    echo "Congratulations: your configuration is ready...and now"
    echo "to get going do the following:"
    echo " - start the auth2 server as: auth2"
    echo " - start rpc2portmap as: rpc2portmap"
    echo " - start updatesrv as: updatesrv"
    echo " - start updateclnt as:  updateclnt -h $hn"

    if [ $numservers = 1 ]; then    
	echo " - start the fileserver: startserver &"
	echo " - wait until the server is up: tail -f ${vicedir}/srv/SrvLog"
    else
	echo " - start your fileservers as:"
	n=1
	while [ $n -le $numservers ]; do
 	    echo "        startserver -n $n &"
	    n=`expr $n + 1`
	done
	echo " - wait until server $numservers is up: tail -f ${vicedir}/server_$numservers/srv/SrvLog"
    fi

    echo " - create your root volume: createvol_rep $RV E0000100 $DP"
    echo " - setup a client: venus-setup $hn 20000"
    echo " - start venus: venus"
    echo " - enjoy Coda."
    echo " - for more information see http://www.coda.cs.cmu.edu."

    exit 0 
fi

#
# specifics for non SCM servers only 
# 

echo  "Now installing things specific to non-SCM machines..."

if ! ( vice-setup-rvm && vice-setup-srvdir ) ;  then
    echo "Your coda server is not completely setup.  You will need"
    echo "set it up by hand or fix the problems and rerun $0."
    exit 1
fi

scm=`cat ${vicedir}/db/scm`
echo "You have set up " `cat ${vicedir}/hostname`
echo "Your SCM is $scm" 
echo "Other config files will be fetched from the SCM by updateclnt."
echo
echo "To finish your server setup you should"
echo
echo " start updateclnt as:  updateclnt -h $scm "
echo " start the auth2 server as: auth2 -chk"
echo
echo "After that, there is still some configuration needed on the SCM before"
echo "this server can be started."
echo
echo "An entry for this host is needed in /vice/db/servers"
echo "New volume storage groups need to be defined in /vice/db/VSGDB"
echo "Then all servers need to be shut down and restarted, as they need to"
echo "know about the new server and storage groups."
echo "After all that it _should_ be ok to start the new server and create"
echo "your first replicated volume."
#echo " On the SCM ($scm) run: new_server $bn"
#echo
#echon "Now you can " 
#echo
#if [ $numservers = 1 ]; then    
#    echo " start the fileserver: startserver &"
#else
#    echo " start your fileservers as:
#    n=1
#    while [ $n -le $numservers ]; do
#	echo "        startserver -n $n &"
#	n=`expr $n + 1`
#    done
#fi

