#!/bin/sh
# BLURB gpl
# 
#                            Coda File System
#                               Release 6
# 
#           Copyright (c) 1987-2003 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
#         Copyright (c) 1998, Anders Hammarquist <iko@iko.pp.se>
# 
#*/

#/* Rewritten for the Bourne shell by Anders Hammarquist <iko@iko.pp.se> */
#
# The original reinit had /vice/bin/startserver and /vice/bin/restartserver
# This can't possibly have worked, since startserver is (usually) in
# /usr/sbin and restartserver doesn't seem to exists. Since they are just
# edited to prevent the admin from restarting the server while it is being
# reinitialized it's not that big a problem. Let's hope the admin isn't out
# to shoot herself in the foot.
#
# Also, we expect rvmutil, norton-reinit and rdsinit to live in $PATH
# rather than /vice/bin.

VICE=/vice	# so that it can be easily moved
SKIPSALVAGE=${VICE}/vol/skipsalvage

askyesno() {
    while :; do
	read ans
	case x"$ans" in
	    x[Yy][Ee][Ss])
		return 0
		;;
	    x[Nn][Oo])
		return 1
		;;
	    x)
		if [ x$1 != x ]; then
		    expr $1 : '[Yy][Ee][Ss]' > /dev/null
		    return
		else
		    echo -n "Please answer yes or no: "
		fi
		;;
	    x*)
		echo -n "Please answer yes or no: "
		;;
	esac
    done
}

echo "WARNING: This program reinitializes a server."
echo -n "Are you sure you want to proceed? (yes/no) "

if ! askyesno; then
    echo "Exiting"
    exit 1
fi

# Purge backups
while grep -q backup ${VICE}/vol/VolumeList; do
    echo ""
    echo "Reinit cannot restore backup volumes.  You must purge all of the backup"
    echo "volumes from the server before proceeding"
    echo ""
    echo "Reinit still detects backup volumes on this server\!"
    echo -n "Have you already deleted them (yes to continue, no to delete)? "
    if askyesno; then break; fi
    echo ""
    echo "Delete the backup volumes"
    echo "Press <return> when you are ready to proceed"
    read
    echo ""
done

# At this point the old code moved and edited startserver, restartserver
# and /etc/motd.

# Check that the server is not running
while [ -e ${VICE}/srv/pid ]; do
    if ! kill -0 `cat ${VICE}/srv/pid` 2> /dev/null; then break; fi
    echo "The server is still running."
    echo "Shut it down and press <return> to continue"
    read
done

# To keep the server from starting, we write something to pid
echo "Server is being reinitialized" > ${VICE}/srv/pid

# Get the skip volume list
while :; do
    if [ -e $SKIPSALVAGE ]; then
	echo "$SKIPSALVAGE exists.  Its contents are:"
	cat $SKIPSALVAGE
	echo ""

	echo -n "Do you want reinit to purge these volumes (yes/no)? "
	if askyesno; then
	    set `cat $SKIPSALVAGE`
	    shift
	    SKIPSALVAGE="$*"
	else
	    echo "WARNING: Reinit will fail if these volumes are corrupt."
	fi
    else
	echo "$SKIPSALVAGE does not exist"
    fi

    echo -n "Are there any other volumes you want to skip? [yes] "
    if askyesno yes; then
	echo "Enter volume numbers terminated with a new line"
	newvol=yes
	while [ x"$newvol" != x ]; do
	    echo -n "Volume Number (in hex): "
	    read newvol
	    SKIPLIST="$SKIPLIST $newvol"
	done
    fi

    if [ x$SKIPLIST = x ]; then
	echo "Reinit will not skip any volumes"
    else
	echo "Reinit will skip the following volumes:"
	echo $SKIPLIST
	echo ""
	echo "THESE VOLUMES WILL NOT BE PRESENT AFTER THE REINIT\!"
    fi
    echo -n "Do you want to proceed? [yes] "
    if askyesno yes; then break; fi
done

# Dump the server state.
echo ""
echo ""
echo "Now 'reinit' needs to know the server RVM characteristics"
echo -n "Enter server log device: "
read logdev
echo -n "Enter log length: "
read loglen
echo -n "Enter server data device: "
read datadev
echo -n "Enter server data length: "
read datalen
echo -n "Enter RVM starting address: 0x"
read rvmstart
echo -n "Enter heap length: 0x"
read heaplen
echo -n "Enter static length: 0x"
read staticlen
echo -n "Enter nlists: "
read nlists
echo -n "Enter chunksize: "
read chunksize

echo ""
echo "'Reinit' needs a place to dump the server's rvm state, there must"
echo "be enough free space to fit all of the server's RVM space."
echo -n "Enter a dumpfile name: "
read dumpfile

cleanup() {
    if [ -e $dumpfile ]; then
	echo "Dumpfile left in $dumpfile"
	echo "Please remove $dumpfile when you are sure the reinit succeeded."
    fi

    rm -f ${VICE}/srv/pid

    #mv -f $STARTSERVER.dontrun $STARTSERVER
    #mv -f $RESTARTSERVER.dontrun $RESTARTSERVER
    #mv -f /etc/motd.before_reinit /etc/motd
}

echo ""
echo "Dumping server RVM state to $dumpfile..."
if [ x"$SKIPLIST" = x ]; then
  norton-reinit -rvm $logdev $datadev $datalen -dump $dumpfile
else
  norton-reinit -rvm $logdev $datadev $datalen -dump $dumpfile skip $SKIPLIST
fi

if [ $? != 0 ]; then
    echo "ERROR: Dump failed\!"
    cleanup; exit 1
fi

echo ""
echo "THIS IS THE POINT OF NO RETURN\!"
echo "RVM is about to be initialized."
echo -n "Are you sure you want to continue? (yes/no) "
if ! askyesno; then cleanup; exit 1; fi

# Initilize RVM
echo ""
echo "Running rvmutl"
rvmutl << __EOF__
init $logdev $loglen
quit
__EOF__

if [ $? != 0 ]; then
    echo "rvmutl failed, exitting"
    cleanup; exit 1
fi

echo ""
echo "Running rdsinit"
rdsinit $logdev $datadev <<__EOF__
$datalen
$rvmstart
$heaplen
$staticlen
$nlists
$chunksize
__EOF__

if [ $? != 0 ]; then
    echo "rdsinit failed, exitting"
    cleanup; exit 1
fi

echo ""
echo "Re-loading RVM"
norton-reinit -rvm $logdev $datadev $datalen -load $dumpfile
if [ $? != 0 ]; then
  echo "norton-reinit failed, exitting"
  cleanup; exit 1
endif

cleanup
exit 0
