#!/bin/sh
# BLURB gpl
#
#			Coda File System
#			    Release 6
#
#	    Copyright (c) 1987-2008 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
#*/

prefix=/usr/local
exec_prefix=${prefix}

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

if [ `uname -s` != Linux ] ; then
    domain=""
    while [ x$domain = x ] ; do
	echo -n "What is the domain name for your servers? "
	read domain
    done
    hn=`hostname -s`.$domain
    export domain
else
    hn=`hostname -f`
fi
export hn

#
# initial information
#
conf="`${exec_prefix}/sbin/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="`${exec_prefix}/sbin/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
    ${exec_prefix}/sbin/codaconfedit server.conf vicedir $vicedir
    ${exec_prefix}/sbin/codaconfedit server.conf numservers $numservers
fi

echo "Setting up $vicedir."

for i in auth2 backup db misc vol/remote ; do
    mkdir -p "${vicedir}/$i"
done

if [ $numservers = 1 ]; then
    mkdir -p ${vicedir}/srv
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
	    mkdir -p $dir/$i
	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 ! ${exec_prefix}/sbin/updatefetch -h $scmhost -r db/servers -l servers ; then
	echo "Could not contact SCM."
	echo "Check whether the updatesrv process is running on the SCM."
	exit 1
    fi

    echo "Fetching needed files from SCM $scmhost."
    for which in auth2.pw auth2.tk auth2.lock prot_users.cdb volutil.tk files
    do
	if ! ${exec_prefix}/sbin/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
    # non critical files
    for which in ROOTVOLUME
    do
	${exec_prefix}/sbin/updatefetch -h $scmhost -r db/$which -l $which 2>/dev/null || true
    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.cdb
servers
scm
update.tk
volutil.tk
VLDB
VRDB
VRList
maxgroupid
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

#
# 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/local/etc/rc.vice" /etc/rc.local > /dev/null 2>&1
	if [ $? != 0 ]; then
	    echo "if [ -x /usr/local/etc/rc.vice ]; then /usr/local/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
#
echo $hn > ${vicedir}/hostname

#
# server.conf standard
#
if [ $mkconf = 1 ] ; then
    ${exec_prefix}/sbin/codaconfedit server.conf rvmtruncate 5
    ${exec_prefix}/sbin/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 ! ( ${exec_prefix}/sbin/vice-setup-scm && ${exec_prefix}/sbin/vice-setup-user && \
	   ${exec_prefix}/sbin/vice-setup-rvm && ${exec_prefix}/sbin/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
    DP=`cat ${vicedir}/db/vicetab | awk '{print $2}' | head -n 1`
    echo "Congratulations: your configuration is ready..."
    echo

    start=""
    yesno=""
    until [ "x$yesno" != x ]; do
	echon 'Shall I try to get things started? (y/n) '
	read yesno
    done
    if [ "x$yesno" = xy -o "x$yesno" = xyes -o "x$yesno" = xY ]; then
	start=Y
    else
	echo "Here is what you would have to do to get things up and running"
    fi

    set -e
    echo " - Coda authentication server (auth2 &)"
    [ "$start" ] && ${exec_prefix}/sbin/auth2 &
    echo " - Coda update server (updatesrv)"
    [ "$start" ] && ${exec_prefix}/sbin/updatesrv
    echo " - Coda update client (updateclnt -h $hn)"
    [ "$start" ] && ${exec_prefix}/sbin/updateclnt -h $hn

    if [ $numservers = 1 ]; then
	echo " - Coda file server (startserver)"
	[ "$start" ] && ${exec_prefix}/sbin/startserver
    else
	echo " - Coda file servers:"
	n=1
	while [ $n -le $numservers ]; do
	    echo "   startserver -n $n"
	    [ "$start" ] && ${exec_prefix}/sbin/startserver -n $n
	    n=`expr $n + 1`
	done
    fi
    echo

    if [ "$start" ] ; then
	echo
	echo "Nice, it looks like everything went ok"
	echo "Now I'll try to create an initial root volume"
    fi

    echo " - createvol_rep / ${hn}${DP}"
    [ "$start" ] && ${exec_prefix}/sbin/createvol_rep / ${hn}${DP}

    if [ "$start" ] ; then
	echo
	echo "That seems to have worked..."
	echo "If you have a working Coda client you should now be able to"
	echo "access the new Coda realm"
	echo " - cfs lv /coda/${hn}/"
    fi
    echo
    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 ! ( ${exec_prefix}/sbin/vice-setup-rvm && ${exec_prefix}/sbin/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 "Then all servers need to be shut down and restarted, as they need to"
echo "know about the new server."
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 $hn"
#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

