#!/bin/csh

echo This script will install the 'wbdemo' database which is used to
echo demonstrate WebBUILDER into the Velocis RDBMS.
echo " "
echo WebBUILDER is a product of VPE, Inc.
echo Velocis is a product of Raima, Inc.
echo " "
#
# initialize the variables we will use in this script
#

set ans=""
set vel_inst_dir=""
set vel_srvnam="" 
set vel_script=""
set catuser=Wayne
set catpass=Father

############################################
### patch file required for installation ###
############################################

set patch_tar=${pwd}/patch_wb.tar
set cddemo_tar=${pwd}/cddemo.tar

if -f ${patch_tar} then
    echo Found patch to apply
else
    echo Patch not found in ${patch_tar}
    exit(1)
endif

if -f ${cddemo_tar} then
    echo Found demo tar to install
else
    echo Demo tar data file not found: ${cddemo_tar}
    exit(1)
endif

if -f wbdemo.sql then
    echo Found script to create database
else
    echo Script to create database not found
    exit(1)
endif

if -f wbload.sql then
    echo Found script to load database
else
    echo Script to load database not found
    exit(1)
endif

#########################################################
### check to see if the Velocis database is installed ###
#########################################################

set vel_inst_places=' \
    /velocis /Velocis /VELOCIS \
    /usr/velocis /usr/Velocis /usr/VELOCIS \
    /raima /Raima /RAIMA \
    /usr/raima /usr/Raima /usr/RAIMA '

foreach i ( ${vel_inst_places} )
    if ( $vel_inst_dir == "" ) then
	if ( $i != "" ) then
	    if -d $i then
		if -w $i then
		    set vel_inst_dir=$i
		endif
	    endif
	endif
    endif
end

if ( $vel_inst_dir == "" ) then
    echo " "
    echo 'Is Velocis installed on this machine?'
    set ans=$<
    if ( ( $ans == "y" ) || ( $ans == "yes" ) \
	|| ( $ans == "Y" ) || ( $ans == "YES" ) ) then
	echo Please enter the name of the directory where Velocis is installed:
	set vel_inst_dir=$<
    else
	if -d /cdrom/third-party then
	    echo Using mounted CD-ROM
	else
	    mount /cdrom
	endif

	if -f /cdrom/third-party/Raima/rds.tar.Z then
	    echo 'CD-ROM appears to be mounted'
	else
	    echo 'Please mount the BSD/OS 3.0 Domestic CDROM on /cdrom'
	    exit(1)
	endif

	echo 'Where should Velocis be installed?'
	set vel_inst_dir=$<
	if -d $vel_inst_dir then
	    echo The directory $vel_inst_dir already exists.
	else
	    mkdir $vel_inst_dir
 	endif

	set bytes1=`df $vel_inst_dir | awk '{ if ( NR==2 ) print $4 * 512 }'`

	if -d $vel_inst_dir then
	    if -w $vel_inst_dir then
		( cd $vel_inst_dir \
			; zcat /cdrom/third-party/Raima/rds.tar.Z \
			| tar xf - )
		if -f $vel_inst_dir/install then
		    ( cd $vel_inst_dir ; sh -x  ./install )
		else
		    echo " "
		    echo Could not finish installation.
		    exit(1)
		endif
	    else
		echo No write access for directory $vel_inst_dir
	    endif
	else
	    echo Can not access directory $vel_inst_dir
	endif

    endif
else
    echo Assuming Velocis is installed in $vel_inst_dir
endif

if ( $vel_inst_dir == "" ) then
    echo No directory name entered.  
    exit(1)
else
    if -d $vel_inst_dir then
	if -f $vel_inst_dir/rdshome.csh then
	    set vel_script=$vel_inst_dir/rdshome.csh
	else
	    echo Velocis does not seem to be installed in: $vel_inst_dir
	    exit(1)
	endif
    else
	echo Directory does not exist: $vel_inst_dir
	exit(1)
    endif
endif

if ( $vel_script != "" ) then
    ################################################################### 
    ### this means that the installation of Velocis probably worked ### 
    ################################################################### 
    source $vel_script
    set vel_initfile=${RDSHOME}/catalog/velocis.ini
    if -f $vel_initfile then
	set vel_srvnam=`awk 'BEGIN {FS="="} {if ($1=="ServerName") print $2}' $vel_initfile `
    endif
    #
    # now we know the default server name
    # let's check to see if there are any servers running
    #
    set vel_run_srvcnt=`ps -ax|grep rd|grep ds|wc -l`
    echo Found $vel_run_srvcnt servers running
    if ( $vel_run_srvcnt == 0 ) then
	echo There are no Velocis servers running.
	echo One will be started now.
	( cd $RDSHOME/bin ; ./rds $vel_srvnam & )
	sleep 10
    endif
    if ( $vel_run_srvcnt == 2 ) then
	#
	# there are some running
	#
	set vel_run_srvnam=`ps -ax|grep rd|grep ds|awk '{print $6}'|sort|uniq`
	if ( vel_run_srvnam != "" ) then
	    set vel_srvnam=$vel_run_srvnam
	endif
    endif
    if ( ( $vel_run_srvcnt == 1 ) || ( $vel_run_srvcnt > 2 ) ) then
	echo There appear to be an unusual number of servers running.
	echo There are $vel_run_srvcnt Velocis rds servers running.
	echo Usually there are 2 servers running.
    endif
endif

if ( $vel_srvnam == "" ) then
    echo " "
    echo Please enter the name for the server you want:
    set vel_srvnam=$<
endif

if ( $vel_srvnam == "" ) then
    echo Velocis server not found.
    exit(1)
else
    echo " "
    echo Velocis Server Name is: $vel_srvnam
    echo " "

    echo "Testing to see if we can reach the server."
    if -x $RDSHOME/bin/vping then
	if { $RDSHOME/bin/vping -s 4096 -c 1 $vel_srvnam } then
	    echo Successfully contacted the server $vel_srvnam
	else
	    echo Un-Successful ping
	    echo " "
	    echo Please start the Velocis server and re-run this command.
	    exit(1)
	endif
    endif

    #
    # now we know the Velocis server name, so we need to populate the database
    #

    set bytes2=`df $vel_inst_dir | awk '{ if ( NR==2 ) print $4 * 512 }'`

    ${RDSHOME}/bin/sddlp -L $vel_srvnam';'$catuser';'$catpass wbdemo.sql
    set sddlp_stat=` awk ' { if ( $3 == "detected" ) print $1 } ' sddlp.out `
    if ( $sddlp_stat != 0 ) then
	echo Could not create the database structure.
    else
	echo Loading the database, started at ` date `
	${RDSHOME}/bin/rsql >dbload.log <<EOFEOF
.c 1 $vel_srvnam Wayne Father
.rwbload.sql
.q
EOFEOF
	echo Loading finished at ` date `

    endif
    set bytes3=`df $vel_inst_dir | awk '{ if ( NR==2 ) print $4 * 512 }'`


endif


#######################################
###  WebBuilder part of the install ###
#######################################

setenv VPEHOME /usr/WebBuilder/2.1.3
setenv CFRUN ${VPEHOME}/bin/bsdi
setenv CFLIB ${VPEHOME}/lib
setenv WWWHOME ~www
setenv pwd `pwd`
setenv wb_tar /cdrom/third-party/WebBUILDER/product/WebBUILDER.tar
setenv hostname `hostname`


if -d $WWWHOME then
    if -d $WWWHOME/conf then
	set srmconf=$WWWHOME/conf/srm.conf
	set docroot=`awk '{ if ($1=="DocumentRoot") print $2}' $srmconf`
	set cgidir=`awk '{ if ($1=="ScriptAlias") print $3}' $srmconf`
	set CFIP=${docroot}/vpe/cddemo
    else
	echo Web Server Configuration directory not found.
	exit(1)
    endif
else
    echo Web Server is not installed in $WWWHOME
    exit(1)
endif

if -d $VPEHOME then
    echo WebBuilder version 2.1.3 apparently already installed.
    echo Installation cancelled.
    exit(1)
else
    if -d /usr/WebBuilder then
	echo Directory /usr/WebBuilder already exists
    else
	mkdir /usr/WebBuilder
    endif
    mkdir $VPEHOME
endif
    
if -d $VPEHOME then

    if -f ${wb_tar} then
	set bytes4=`df /usr | awk '{ if ( NR==2 ) print $4 * 512 }'`
	( cd /usr ; tar xf ${wb_tar} )
	( cd $VPEHOME; tar xf install/scripts.tar )
	### ( cd $VPEHOME/install ; install_vpe )
	if -d $CFLIB/locale then
		chmod 777 $CFLIB/locale
	endif

	#
	# Build the Profile.vpe file
	#
	echo "#### default Profile.vpe file ####" > $CFLIB/Profile.vpe
	echo CFRUN=$VPEHOME/bin/bsdi >> $CFLIB/Profile.vpe
	echo CFLIB=$CFLIB >> $CFLIB/Profile.vpe
	echo VPEHOME=$VPEHOME >> $CFLIB/Profile.vpe
	echo CFIP=$CFIP >> $CFLIB/Profile.vpe
	echo CFDRM=\$CFIP/rules >> $CFLIB/Profile.vpe
	echo CFCFA=\$CFIP/screens >> $CFLIB/Profile.vpe
	echo CFIMAGES=\$CFIP/images >> $CFLIB/Profile.vpe
	echo CFDATA=\$CFIP/data >> $CFLIB/Profile.vpe
	echo CFAUDIO=\$CFIP/audio >> $CFLIB/Profile.vpe
	echo unset CFHOME >> $CFLIB/Profile.vpe
	echo PATH=\$CFRUN:\$VPEHOME:\$PATH >> $CFLIB/Profile.vpe
	echo export CFRUN CFLIB CFIP CFDRM CFCFA >> $CFLIB/Profile.vpe
	echo export CFIMAGES CFDATA CFAUDIO PATH >> $CFLIB/Profile.vpe

	#
	# Build the Login.vpe file
	#
	echo "#### default Login.vpe file ####" > $CFLIB/Login.vpe
	echo setenv CFRUN $VPEHOME/bin/bsdi  >> $CFLIB/Login.vpe
	echo setenv VPEHOME $VPEHOME  >> $CFLIB/Login.vpe
	echo setenv CFLIB $CFLIB  >> $CFLIB/Login.vpe
	echo setenv CFIP $CFIP  >> $CFLIB/Login.vpe
	echo setenv CFDRM \$CFIP/rules  >> $CFLIB/Login.vpe
	echo setenv CFCFA \$CFIP/screens  >> $CFLIB/Login.vpe
	echo setenv CFIMAGES \$CFIP/images  >> $CFLIB/Login.vpe
	echo setenv CFDATA \$CFIP/data  >> $CFLIB/Login.vpe
	echo setenv CFAUDIO \$CFIP/audio  >> $CFLIB/Login.vpe
	echo unsetenv CFHOME  >> $CFLIB/Login.vpe
	echo 'set path=( $CFRUN $VPEHOME $path )' >> $CFLIB/Login.vpe


	( cd $VPEHOME ; tar xf ${patch_tar} )

	$CFRUN/lsv_status
    else
	echo WebBUILDER installation tar file ${wb_tar} not on CD-ROM.
	echo Installation cancelled.
	exit(1)
    endif
else
    echo Could not create directory $VPEHOME
    echo Installation cancelled.
    exit(1)
endif

set bytes5=`df $VPEHOME | awk '{ if ( NR==2 ) print $4 * 512 }'`

set bytes6=`df $docroot | awk '{ if ( NR==2 ) print $4 * 512 }'`

if -d $cgidir then
    ln -s ${VPEHOME}/bin/bsdi/vpecgi $cgidir
else
    echo Error:  cgi-bin directory $cgidir does not exist
    echo Installation cancelled.
    exit(1)
endif

if -d $docroot/vpe then
    echo Directory already exists: $docroot/vpe 
else
    mkdir $docroot/vpe 
endif


if -f $docroot/vpe/index.tbl then
    echo File already exists:  $docroot/vpe/index.tbl
else

    cat > $docroot/vpe/index.tbl <<EOFEOF
% WebBUILDER application information file

% executable configuration
setenv VPEHOME ${VPEHOME}
setenv CFRUN   ${VPEHOME}/bin/bsdi
setenv CFLIB   ${VPEHOME}/lib
setenv CFNETB 300

%%%% tell where the document root directory is for the HTTP server
setenv	VPE_HTTP_ROOT	${docroot}

%%%% base socket address for vpecgi/vpeweb communication
% setenv VPE_WWW_PORT 3525

%%%% limit to number of simultaneous sessions
%%%%	minimum is 1, maximum is 1000, default is 10
% setenv VPE_WWW_SESSIONS 100

%%%% base names for logging files for vpecgi and vpeweb executables
%%%%	if none are set, logging will not take place
% setenv VPE_CGI_LOG /tmp/cgilog:1
% setenv VPE_WEB_LOG /tmp/weblog:1
% setenv VPE_APP_LOG /tmp/syslog

%%%% enable message pulse if some processes don't timout
%%%% 	if value is missing or empty the pulse won't be sent
% setenv VPEWEB_PING 1

%application    host            shell script
%%%%%%%%%%%%    %%%%%           %%%%%%%%%%%%
testapp         localhost       none
poker           localhost       ${VPEHOME}/demos/poker/runweb
tictactoe       localhost       ${VPEHOME}/demos/tictactoe/runweb
WebConnect      localhost       ${VPEHOME}/tools/WebConnect/bin/WebConHTML
cddemo		localhost	${CFIP}/runweb
EOFEOF

endif

if -d ${CFIP} then
    echo Directory already exists: ${CFIP}
else
    mkdir ${CFIP}
endif

if -d ${CFIP}/logs then
    echo Log Directory already exits: ${CFIP}/logs
else
    mkdir ${CFIP}/logs
    chmod 777 ${CFIP}/logs
endif

( cd ${CFIP} ; tar xf ${cddemo_tar} )

cat > ${CFIP}/runweb <<EOFEOF
#!/bin/csh
setenv CFIP		${CFIP}
setenv CFDRM		${CFIP}/rules
setenv VPE_HTML_PATH	${CFIP}/html
setenv CFIMAGES		${CFIP}/images
setenv CATALOG_DB_HOST	$hostname
setenv CATALOG_DB_TYPE	VELOCIS
setenv CATALOG_DB_USER	$catuser
setenv CATALOG_DB_PASS	$catpass
setenv CATALOG_DB_INST	$vel_srvnam
setenv CFNETB		300
\$CFRUN/vper -L:/dev/null -d4 cddemo >& \$CFIP/logs/runlog.\$\$ &
EOFEOF
chmod +x ${CFIP}/runweb

cat > ${CFIP}/runhtml <<EOFEOF
#!/bin/csh
setenv VPEHOME		\${VPEHOME}
setenv CFRUN		\${CFRUN}
setenv CFLIB		\${CFLIB}
setenv CFIP		\${CFIP}
setenv CFDRM		\$CFIP/rules
setenv VPE_HTML_PATH	\$CFIP/html
setenv CFDATA		\$CFIP/data
setenv CFIMAGES		\$CFIP/images
setenv CATALOG_DB_HOST	$hostname
setenv CATALOG_DB_TYPE	VELOCIS
setenv CFNETB		300
setenv CATALOG_DB_USER	$catuser
setenv CATALOG_DB_PASS	$catpass
setenv CATALOG_DB_INST	$vel_srvnam
setenv VPE_WWW_AUTHID	`\$CFRUN/vpecgi -s -q`
\$CFRUN/vper -w -s cddemo -P VPE_HTML_TEST_VIEWER='netscape %f' VPE_HTML_TEST_HOME=http://$hostname/cgi-bin/vpecgi/testapp.\$VPE_WWW_AUTHID
EOFEOF
chmod +x ${CFIP}/runhtml

cat > ${CFIP}/compile <<EOFEOF
#!/bin/csh
setenv VPEHOME		${VPEHOME}
setenv CFRUN		${VPEHOME}/bin/bsdi
setenv CFLIB		${VPEHOME}/lib
setenv CFIP		${CFIP}
setenv CFDRM		${CFIP}/rules
setenv VPE_HTML_PATH	${CFIP}/html
cd \$CFDRM
\$CFRUN/vper -c -w cddemo 
EOFEOF
chmod +x ${CFIP}/compile

cat > ${CFIP}/startsql <<EOFEOF
#!/bin/csh
setenv VPEHOME		${VPEHOME}
setenv CFRUN		${VPEHOME}/bin/bsdi
setenv CFLIB		${VPEHOME}/lib
setenv CFNETB		300
\$CFRUN/cfsqlctl &
EOFEOF

chmod +x ${CFIP}/startsql

#
# now, run the compile script and start the sql daemon
#

${CFIP}/compile
${CFIP}/startsql

sleep 5

set bytes7=`df $docroot | awk '{ if ( NR==2 ) print $4 * 512 }'`

if -f ${CFIP}/rules/cddemo.dct then
    echo Installation Successful
else
    echo Installation Failed.
endif


echo $bytes1 Bytes free before installing Velocis
echo $bytes2 Bytes free after starting Velocis
echo $bytes3 Bytes free after loading WBdemo database into Velocis 
echo $bytes4 Bytes free before installing WebBuilder 
echo $bytes5 Bytes free after installing WebBuilder 
echo $bytes6 Bytes free before installing cddemo 
echo $bytes7 Bytes free after installing cddemo 

exit(0)
