#!/bin/csh
#
# multiraptor.sh - Let user choose multiple raptors to install.
# ============================================================
#
# Well, gdialog seems to work better than kdialog and is everywhere.
# But probably not for long ... sigh. And then there is zenity ... Too many choices!
# And EVERY SINGLE USE OF gdialog AS ROOT WILL GIVE SECURITY RELATED ERRORS! 
# Torvald's "monkey" comment could not be more true.
#
set DIALOG = gdialog
#
# Actually gdialog is often a Perl script that eventually invokes zenity.
# The Perl script doesn't handle \n in strings correctly, so we need to know.
#
set GDPATH = `whereis ${DIALOG} | sed -e s/${DIALOG}://g`
set ISPERL = `file ${GDPATH} | grep -i perl`
#
# Try to find a simple editor that actually works.
#
set GEDIT = "/usr/X11R6/bin/nedit -read"  # gedit crashes on 64 bit systems trying to allocate terabytes of memory. This is Unix.
if ( ! -x /usr/X11R6/bin/nedit ) then
    set GEDIT = "/usr/bin/gedit"          # Well, maybe! RH8 32 bit doesn't have nedit...
endif
#
# Ensure we are root now.
#
if( "`whoami`" != "root" ) then
    ${DIALOG} --title "You must be root." --infobox "I am sorry, but you need to be root. Exiting." 1 80 >& /dev/null
    exit 1
endif
#
# Pop up a list of checkboxes, one for each Raptor.
# Since the output of the lovely gdialog appears on stderr, things are a little ugly.
# Especially since any error messages from gdialog also got to stderr.
# And there ARE error messages due to obscure and ludicrous (as usual) security nonsense. WHAT A MESS.
#
if ( "${ISPERL}" == "" ) then
    set CLTEXT = "Please select the Raptors to install:\n(Please scroll to see all options)"
else
    set CLTEXT = "Please select the Raptors to install (Please scroll to see all options):"
endif
${DIALOG} --title "Multiple Raptors Install" --separate-output --checklist "${CLTEXT}" 10 80 10 1 "Speedo" on 2 "Lenz" on 3 "Fluidz" on 4 "Trailz" on  >& /usr/tmp/zzmrsel
set SELECTED = `cat /usr/tmp/zzmrsel | sed -n -e '/^[1234]/p'`
rm -f /usr/tmp/zzmrsel
#
# If nothing was selected, give up.
#
if( "$SELECTED" == "" ) then
    ${DIALOG} --title "Multiple Raptors Install" --infobox "No Raptors were selected. Exiting." 1 80 >& /dev/null
    exit 1
endif
#
# Define current MACHINE BITS and PACKAGE BUILD numbers.
#
set MBITS = "32"
if ( "32" == "32" ) then
set FMBITS =
else
set FMBITS = "32-"
endif
set SPEEDO_BUILD = "806"
set LENZ_BUILD = "806"
set FLUIDZ_BUILD = "806"
set TRAILZ_BUILD = "806"
#
# Display the EULA ... just this once ...
#
${DIALOG} --title "Display EULA" --infobox "Please prepare to read the Speedsix License Agreement.\nInstallation continues when you close the editor." 1 80 >& /dev/null
${GEDIT} ./speedsix_eula >& /dev/null
${DIALOG} --title "Accept or decline EULA" --yesno "Do you agree?" 1 80 >& /dev/null
#
# The following MESS is necessitated by RH8, where gdialog does NOT return 0 or 1 (as the manual SAYS IT DOES)
# but some other value - 248 or 247 - where the bottom bit is set or clear.
# Testing this with a simple if ( ! $status ) does NOT WORK. (Obviously).
# This is Unix. It is STILL TRUE that manuals often LIE and documented features sometimes do not
# even exist. I have had 26 years of this sort of thing ... and counting.
#
@ jstatus = $status
@ ustatus = ( $jstatus & 1 )
if( $ustatus != 0 ) then
    ${DIALOG} --title "EULA declined" --infobox "Without agreement, nothing can be installed. Goodbye." 1 80 >& /dev/null
    exit 1
endif
setenv EULA_HAS_BEEN_SHOWN 1
#
# Step over the selection installing the things.
#
foreach PACKAGE ( ${SELECTED} )
    switch ( ${PACKAGE} )
	case 1:
	    echo
	    echo
	    echo "Installing Speedo ..."
	    tar xzf Speedo${MBITS}_V1.2.${SPEEDO_BUILD}_linux.tar.gz
	    cd discreet-linux-speedo-raptor-${FMBITS}1.2-${SPEEDO_BUILD}-dist
	    ./install_s6
	    cd ..
	    rm -rf discreet-linux-speedo-raptor-${FMBITS}1.2-${SPEEDO_BUILD}-dist
	    breaksw
        case 2:
	    echo
	    echo
	    echo "Installing Lenz ..."
	    tar xzf Lenz${MBITS}_V1.2.${LENZ_BUILD}_linux.tar.gz
	    cd discreet-linux-lenz-raptor-${FMBITS}1.2-${LENZ_BUILD}-dist
	    ./install_s6
	    cd ..
	    rm -rf discreet-linux-lenz-raptor-${FMBITS}1.2-${LENZ_BUILD}-dist
	    breaksw
	case 3:
	    echo
	    echo
	    echo "Installing Fluidz ..."
	    tar xzf Fluidz${MBITS}_V1.2.${FLUIDZ_BUILD}_linux.tar.gz
	    cd discreet-linux-fluidz-raptor-${FMBITS}1.2-${FLUIDZ_BUILD}-dist
	    ./install_s6
	    cd ..
	    rm -rf discreet-linux-fluidz-raptor-${FMBITS}1.2-${FLUIDZ_BUILD}-dist
	    breaksw
        case 4:
	    echo
	    echo
	    echo "Installing Trailz ..."
	    tar xzf Trailz${MBITS}_V1.2.${TRAILZ_BUILD}_linux.tar.gz
	    cd discreet-linux-trailz-raptor-${FMBITS}1.2-${TRAILZ_BUILD}-dist
	    ./install_s6
	    cd ..
	    rm -rf discreet-linux-trailz-raptor-${FMBITS}1.2-${TRAILZ_BUILD}-dist	    
	    breaksw
    endsw
end
