#!/bin/sh

# INSTALL
# MAK 05/23/01 Daylight CIS, Inc.
#
# This is a generic installation script for Daylight software major releases.
# This shell script asks questions (if necessary), asks for confirmation, performs an installation,
# and provide post-installation instructions (if applicable)
#
# Assumptions:
#   this script is in the same directory as major release TARball
#   the TARball filename is dayXY1major-<SYSTEM>.tar, where XY1 is the version #
# Options:
#   -b is brave mode, no questions or confirmation\n"
#   -bb is brave & blind mode, no questions, confirmation or printing\n"
#   -f <filename> for TARball specification (necessary if there's more than one file matching day*.tar*)
#   -o overwrite preexisting installation
#   -u uninstalls the major release
#   -v prints verbose information\n"
# features:
#   sets link named 'current' to DY_ROOT (e.g., v481)

# original working directory, convenient to go back to
ODIR=`pwd`

# routine: printing
dy_printf() {
    if [ "_$BLIND" = '_' ]; then
        printf "$1"
    fi
    printf "$1" >> $LOG
}

# refuse to run as "root"
username=`whoami`
if [ "root" = $username ] ; then
dy_printf ""
dy_printf "+---------------------------------------------------------------------------+\n"
dy_printf "| For security reasons, Daylight software should not be owned by the        |\n"
dy_printf "| super-user \"root.\"  Please remove this file, log in as another user       |\n"
dy_printf "| (preferably as user \"thor\") and start the installation procedure again.   |\n"
dy_printf "+---------------------------------------------------------------------------+\n"
dy_printf "Daylight installation aborted\n"
exit 1
fi

# get version from this filename
XY1=`printf $0 | awk '{
    print substr($0,length($0)-2,3)
}'`

# routine: help
Usage() { 
    dy_printf "Usage: `basename $0` [ options ]\n"
    dy_printf "Options:\n"
    dy_printf "\t-b (brave, no questions or confirmation)\n"
    dy_printf "\t-bb (brave & blind, no questions, confirmation or printing)\n"
    dy_printf "\t-f <filename> (e.g., -f day${XY1}major-linux.tar.gz)\n"
    dy_printf "\t-o (overwrite)\n"
    dy_printf "\t-u (uninstall)\n"
    dy_printf "\t-v (verbose)\n"
}
if [ "_$1" != '_' ]; then
    if [ $1 = '?' -o $1 = '-help' -o $1 = '--help' ]; then
        Usage
        exit 1
    fi
fi

# routine: prompt for answer
prompt_answer() {
    dy_printf "$1"
    if [ "_$BRAVE" = '_' ]; then
        read ANSWER
        printf "$ANSWER\n" >> $LOG
    fi
}

# routine: continuation question
# caller syntax: check_to_proceed [ prompt  [ default answer ] ]
# require <CR>, 'y' or 'Y' to proceed, otherwise exit
check_to_proceed()
{
    PROMPT='Do you want to proceed?'
    DEFAULT_ANSWER=yes
    if [ "_$1" != '_' ]; then
        PROMPT="$1"
    fi
    if [ "_$2" = '_n' -o "_$2" = '_N' -o "_$2" = '_no' -o "_$2" = '_No' -o "_$2" = '_NO' ]; then
        DEFAULT_ANSWER=$2
    fi
    prompt_answer "\n$PROMPT {$DEFAULT_ANSWER} "
    ANSWER=`printf "_$ANSWER" | cut -c2`
    if [ "$DEFAULT_ANSWER" != 'yes' ]; then
        if [ "_$ANSWER" = '_' -a "_$ANSWER" != '_n' -a "_$ANSWER" != '_N' ]; then
            dy_printf "Daylight installation aborted\n"
            exit 1
        fi
    elif [ "_$ANSWER" != '_' -a "_$ANSWER" != '_y' -a "_$ANSWER" != '_Y' ]; then
        dy_printf "Daylight installation aborted\n"
        exit 1
    fi
    dy_printf "\n"
}

# options
while [ $# -gt 0 ]
do
    # TARball filename
    if [ $1 = '-b' ]; then
        BRAVE=y
    elif [ $1 = '-bb' ]; then
        BRAVE=y
        BLIND=y
    elif [ $1 = '-f' ]; then
        shift
        DIR=`dirname $1`
        if [ ! -d $DIR ]; then
	dy_printf "ERROR: major file path ($DIR) doesn't exist, exiting\n"
	exit 1
        fi
        cd $DIR
        MAJORPATH=`pwd`
        MAJORFILE=`basename $1`
        if [ ! -f $MAJORPATH/$MAJORFILE ]; then
	    dy_printf "ERROR: $MAJORPATH/$MAJORFILE doesn't exist, exiting\n"
	    exit 1
        fi
        cd $ODIR
    # force overwrite of .vXY1 files
    elif [ $1 = '-o' ]; then
        FORCE=y
    # uninstall
    elif [ $1 = '-u' ]; then
        UNINSTALL=y
    # verbose
    elif [ $1 = '-v' ]; then
        set -x
    else
        dy_printf "WARNING $1 option ignored\n"
    fi
    shift
done

# system type
SYSTEM=`uname -s`
case $SYSTEM in
'Linux')
	SYS=linux
	;;
'IRIX')
	SYS=sgi
        ;;
'IRIX64')
	SYS=sgi
	;;
'SunOS')
	SYS=sun
	;;
*)
	dy_printf "$SYS is not supported (Linux, IRIX, SunOS only)\n"
	exit 1
	;;
esac

# logging, EXEC indicates USER and DATE of execution
cd `dirname $0`
LOG=`pwd`/`basename $0`.log
dy_printf "EXEC `whoami` `date`\n"
cd $ODIR

# Step 0: instructions
INSTR="Instructions"
dy_printf "$INSTR\n\n"

dy_printf "This is the Daylight software installation program for UNIX computers.\n"
dy_printf "There are four parts to this installation:\n\n"

dy_printf "  1. Questions/answers: you specify settings.\n"
dy_printf "  2. Confirmation:      you confirm that it is OK to start installation.\n"
dy_printf "  3. Installation:      software and other files are copied and decompressed.\n"
dy_printf "  4. Post-installation: instructions and site-specific information.\n\n"

dy_printf "During the question/answer part, the following rules apply:\n\n"

dy_printf "  1. Most questions contain default answers in {braces}.  Enter just\n"
dy_printf "     RETURN to select the default answer.\n\n"

dy_printf "  2. You may abort this installation at any time by pressing the EOF\n"
dy_printf "     character (usually either the CONTROL-D or the DELETE key).\n\n"

dy_printf "This installation script may have trouble if your TERM environment variable\n"
dy_printf "doesn't match the terminal type you are using.  If you don't see \"$INSTR\"\n"
dy_printf "at the top of this section, then TERM is probably not correct.  Your local UNIX \n"
dy_printf "expert can help you with this.\n"

check_to_proceed

# version #, e.g., v471
VNUM=$XY1
VER=v$VNUM
STR_VER=$VER
if [ $? != 0 ]; then
    dy_printf "ERROR: determining version # from $MAJORFILE, exiting\n"
    dy_printf "(require dayXY1major-<SYSTEM>.tar filename format, where XY1 are digits)\n"
    exit 1
fi

# major TARball filename
if [ "_$MAJORFILE" = '_' ]; then
    cd `dirname $0`
    MAJORPATH=`pwd`
    MAJORFILE=day${VNUM}major-$SYS.tar
    if [ ! -f $MAJORFILE -a ! -f $MAJORFILE.gz ]; then
	MAJORFILE=day${VNUM}-$SYS.tar
        if [ ! -f $MAJORFILE -a ! -f $MAJORFILE.gz ]; then
	MAJORFILE=day${VNUM}-"$SYS"5.tar
            if [ ! -f $MAJORFILE -a ! -f $MAJORFILE.gz ]; then
                dy_printf "ERROR: $MAJORFILE TARball not in $MAJORPATH\n"
                dy_printf "HINT: specify TARball filename as argument\n"
                Usage
                exit 1
            fi
        fi
    fi
    cd $ODIR
fi
TARBALL=$MAJORFILE

# Step 1: Q&A
dy_printf "\nStep 1: Q&A\n\n"
# routine: check DY_ROOT, inquire for reset
check_dy_root()
{
    if [ "_$DY_ROOT" = '_' ]; then
        dy_printf "DY_ROOT is not set\n"
        ANSWER=y
    elif [ ! -d $DY_ROOT ]; then
        dy_printf "DY_ROOT ($DY_ROOT) is not a directory\n"
        prompt_answer "Do you want to create $DY_ROOT? {no} "
        ANSWER=`printf "_$ANSWER" | cut -c2`
        if [ "_$ANSWER" != '_' -a "_$ANSWER" != '_n' -a "_$ANSWER" != '_N' ]; then
            mkdir -p $DY_ROOT
            ANSWER=n;
        else
            ANSWER=y
        fi
    else
        dy_printf "DY_ROOT is set to $DY_ROOT\n\n"
        prompt_answer "Do you want to reset DY_ROOT? {no} "
        ANSWER=`printf "_$ANSWER" | cut -c2`
        TRIES=0
    fi
    dy_printf "\n"
}

# fully qualify path
fully_qualify_dir() {
    DIRNAME=`dirname $1`
    cd $DIRNAME
    DIRNAME=`pwd`
    printf "$DIRNAME/`basename $1`"
}
if [ "_$DY_ROOT" != '_' ]; then
    DY_ROOT=`fully_qualify_dir $DY_ROOT`
fi

# set DY_ROOT
check_dy_root
TRIES=0
while [ "_$ANSWER" != '_' -a "_$ANSWER" != '_n' -a "_$ANSWER" != '_N' ]
do
    if [ $TRIES -gt 2 ]; then
        dy_printf "setting of DY_ROOT failed $TRIES times, exiting\n"
        exit 1
    fi
    if [ $TRIES = 0 ]; then
        dy_printf "DY_ROOT should be set to the location of Daylight $VER\n"
        dy_printf "current working directory is `pwd`\n\n"
    fi
    prompt_answer "DY_ROOT="
    DY_ROOT=$ANSWER
    # fully qualify path
    if [ "_$DY_ROOT" != '_' ]; then
        DY_ROOT=`fully_qualify_dir $DY_ROOT`
    fi
    check_dy_root
    TRIES=`expr $TRIES + 1`
done

DY_ROOT_DIRNAME=`dirname $DY_ROOT`
DY_ROOT_BASENAME=`basename $DY_ROOT`

# check for problems
if [ "_$UNINSTALL" = '_' ]; then
    # already exists!
    if [ -d $DY_ROOT/bin -a "_$FORCE" = '_' ]; then
        printf "ERROR $VER contains data; use -o option to overwrite, exiting\n"
        exit 1
    fi
else
    # check for previous uninstall
    if [ ! -d $DY_ROOT ]; then
	dy_printf "INFO: Daylight $STR_VER isn't installed at $DY_ROOT, exiting\n"
	exit 1
    fi
fi

# running servers
if [ $SYS = 'linux' ]; then
    PS_OPT=-aux
else # sgi, sun
    PS_OPT='-ef -o pid -o args'
fi
# thor
PROC=`ps $PS_OPT | grep thorserver | grep -v grep`
if [ "_$PROC" != '_' ]; then
    dy_printf "ThorServer is currently running.  Stop and restart it after installation\n"
    dy_printf "Output from the 'ps' command:\n"
    dy_printf "$PROC\n"
    check_to_proceed
fi
# merlin
PROC=`ps $PS_OPT | grep merlinserver | grep -v grep`
if [ "_$PROC" != '_' ]; then
    dy_printf "MerlinServer is currently running.  Stop and restart it after installation\n"
    dy_printf "Output from the 'ps' command:\n"
    dy_printf "$PROC\n"
    check_to_proceed
fi

# ownership
if [ "_$UNINSTALL" = '_' -a "thor" != $username -a "daylight" != $username ] ; then
dy_printf "Daylight software is typically owned by a neutral user named \"thor\" or\n"
dy_printf "\"daylight\".  It is OK for a normal user or system administrator (i.e.\n"
dy_printf "someone other than \"thor\") to own the files if that person is the only\n"
dy_printf "one expecting to use Daylight's software (e.g. for a demo).  Otherwise,\n"
dy_printf "it is more convenient if "thor" owns them.  Installed files will be\n"
dy_printf "owned by the user running this program.\n\n"

dy_printf "You are currently logged in as: $username\n\n"

dy_printf ""
dy_printf "NOTE: You are not logged in as thor.\n"
dy_printf "To have \"thor\" own the Daylight files, answer \"no\" to the next\n"
dy_printf "question , log in as thor, and run this script again.\n"
check_to_proceed
fi

# Step 2: confirmation
dy_printf "\nStep 2: Confirmation\n\n"
dy_printf "Settings:\n"
if [ "_$UNINSTALL" = '_' ]; then
    dy_printf "\tOperation:\tinstall $STR_VER\n"
else
    dy_printf "\tOperation:\tuninstall $STR_VER\n"
fi
dy_printf "\tDY_ROOT:\t$DY_ROOT\n"
dy_printf "\tUSERNAME:\t$username\n"
# confirm install
if [ "_$UNINSTALL" = '_' ]; then
    if [ "_$FORCE" != '_' ]; then
        dy_printf "\tOVERWRITE:\tyes\n"
    fi
    check_to_proceed "Ready to install?"
# confirm uninstall
else
    check_to_proceed "Ready to uninstall?"
fi

cd $DY_ROOT_DIRNAME
# Step 3a: install
if [ "_$UNINSTALL" = '_' ]; then
    dy_printf "\nStep 3: INSTALL Daylight $VER\n\n"
    if [ -f $TARBALL.gz ]; then
	gzip -d $TARBALL.gz
    fi
    tar xvf $TARBALL
# Step 3b: uninstall
else
    dy_printf "\nStep 3: UNINSTALL Daylight $VER\n\n"
    chmod -R 700 $DY_ROOT
    rm -r $DY_ROOT
fi

# Step 4: Post-installation
if [ "_$UNINSTALL" = '_' ]; then
    dy_printf "\nStep 4: Post-installation\n\n"
    dy_printf "Execute $0.root as user root\n"
    dy_printf "Read the README files\n"
    dy_printf "\t$DY_ROOT_DIRNAME/$VER/readme-$STR_VER\n"
    dy_printf "\t$DY_ROOT_DIRNAME/$VER/readme-$STR_VER-$SYS\n"
fi

if [ "_$UNINSTALL" = '_' ]; then
    dy_printf "\nINSTALL Daylight $STR_VER COMPLETE\n\n"
else
    dy_printf "\nUNINSTALL Daylight $STR_VER COMPLETE\n\n"
fi

exit 0
