#!/bin/sh
# @(#)installBlank	2.2 10/7/93
# Copyright (c) 1992,1993 Alias Research. All rights reserved.
# This is unpublished proprietary source code of Alias Research
# The copyright notice above does not evidence any
# actual or intended publication of such source code.

# Customer Product Installation Script

####################################################################

# Required OS and Revision Level
OS=405
REV=731

# Behavior modifications
VERSION_MISMATCH_PATTERN="1|2|3"
MUST_BE_ROOT=1
ALTER_LOGINS=1
DEST_DIR=/usr/Alias2D
TITLE=""
RELEASE=1.0.2
MUTEX=""
PAIRS="3 1 |"

# Number of Installable Tape segments (other than the loader)
TAPE_SEGS=3

# Name of each tape segment, and size in MB
#
SEG_NAME_1="Alias Eclipse 1.0........................................... 22 MB"
SEG_SIZE_1=22
SEG_DIR_1=".login .cshrc .profile bin sysbin util"
SEG_BASE_1=""
SEG_NAME_2="Sample Images............................................... 53 MB"
SEG_SIZE_2=53
SEG_DIR_2="demo"
SEG_BASE_2=""
SEG_NAME_3="Multimedia Eclipse Demo (Indigo, Indigo2, Indy only)........ 45 MB"
SEG_SIZE_3=45
SEG_DIR_3="CyberDemo bin"
SEG_BASE_3="CyberDemo"
#SEGMENT_LIST#


####################################################################

#
# Define variables and functions we will use
#

PATH=/usr/sbin:/usr/bsd:/usr/bin:/bin:/usr/bin/X11:/etc:/usr/etc::
SHELL=/bin/sh
export PATH SHELL
NRTAPE=/dev/nrtape

umask 022

out_msg()
{
    echo "Press RETURN\c"
    read a
    exit 0
}

trap "echo \"\n\n** Interrupted! **\n\"; exit 1" 1 2 3 15

failure()
{
    echo "\n!! Installation Failed !!"
    out_msg
    exit 1
}

yorn()
{
    while :
    do
        if [ "$1" = "1" ]
        then
            echo "(y/n)\c"
        else
            echo
            echo "Is this okay? (y/n)\c"
        fi

        if [ "$2" ]
        then
            echo "(${BOLD}$2${UNBOLD})\c"
        fi

        read yorn_value
        if [ "$yorn_value" = "" ]
        then
            yorn_value="$2"
        fi
        case "$yorn_value" in
            y|Y) return 0 ;;
            n|N) return 1 ;;
            *) echo "Please answer using either y or n." ;;
        esac
    done
}

if [ "$1" != "root" ]
then
xwsh -holdonerror -fg white -bg black -bold yellow -fn a16 \
        -geom 90x25+100+100 -cursorfg green -cursorbg green \
        -title "Installing Alias Eclipse $RELEASE..." \
        -e /bin/sh -c \
        "echo $1; \
         echo Installation of this product requires ROOT privileges.; \
         echo If your system has a ROOT password, you must enter it.; \
         echo If you have no ROOT password, you will not be prompted for it.; \
         echo ; \
         for i in 1 2 3; do su root -c \"$0 root\" && exit 0; done"
         exit 0
fi

BOLD=`/usr/bin/tput bold`
UNBOLD=`/usr/bin/tput rmso`

#
# Ensure that this script is being run as root (uid == 0)
#
uid=`/usr/bin/id | cut -c5- | cut '-d(' -f 1`

if [ "$uid" != "0" -a "$MUST_BE_ROOT" = "1" ]
then
    echo "You must be logged in as \"root\" in order to install"
    echo "Alias 2D Products."
    failure
fi

case $0 in
    /*) DEF_BASE="`/bin/dirname $0`/dist" ;;
    *)  DEF_BASE="`pwd`/`/bin/dirname $0`/dist" 
        here=`pwd`
        cd $DEF_BASE
        DEF_BASE=`pwd`
        cd $here
        ;;
esac

echo "\nDefault responses are shown ${BOLD}highlighted${UNBOLD}.\n"

echo "Directory to load from (${BOLD}${DEF_BASE}${UNBOLD}): \c"

read BASE
case $BASE in
    */dist) ;;
    "") BASE=$DEF_BASE ;;
    *) echo "** ERROR: \"$BASE\" does not contain a product distribution"
        out_msg
        exit 0
    ;;
esac

#
# Prompt for which segments to load
#
/usr/bin/tput clear
cat <<EOF
                                    Alias 2D

                                      ${BOLD}${RELEASE}${UNBOLD}

$TITLE
                                Installation Menu

Please select from the following packages:
EOF

while :
do
    echo "\n\n"
    i=0
    ALL=""
    while [ $i -lt $TAPE_SEGS ]
    do
        i=`expr $i + 1`
        echo $BOLD $i $UNBOLD "  " "`IFS=! eval echo \\$SEG_NAME_$i`"
        ALL="$ALL $i"
    done
    # echo $BOLD '*' $UNBOLD "  " "Install ALL packages"

    echo
    packlist=""
    echo "List which packages to install (${BOLD}1 3${UNBOLD}): \c"
    read packlist
    if [ ! "$packlist" ]
    then
       packlist="1 3"
    fi
    if [ "$packlist" = '*' ]
    then
        packlist=$ALL
    fi
    set -- $packlist
    echo 

    found_mutex=1
    mutex_seg=
    for i in $MUTEX
    do
        if [ "$i" = '|' ]
        then
            if [ "$found_mutex" = 1 ]
            then
                echo "\nThe combination of options you have selected"
                echo "is not valid. The following packages cannot be\n"
                echo "loaded together.\n"
                for j in $mutex_seg
                do
                    choice=`expr $j + 0 2>/dev/null`
                    echo "   " "`IFS=! eval echo \\$SEG_NAME_$choice`"
                done
                echo "\nPlease try again."
                packlist=""
                break
            fi
            found_mutex=1
            mutex_seg=
            continue
        fi
        if echo $packlist | grep $i >/dev/null 2>&1
        then
            :
        else
            found_mutex=0
        fi
        mutex_seg=${mutex_seg}" "${i}
    done

    pairs_seg=
    found_pairs=0
    pair_error=0
    for i in $PAIRS
    do
        if [ "$i" = '|' ]
        then
            if [ "$pair_error" = 1 ]
            then
                echo "\nThe combination of options you have selected"
                echo "is not valid. The following packages must be"
                echo "loaded together:\n"
                for j in $pairs_seg
                do
                    choice=`expr $j + 0 2>/dev/null`
                    echo "   " "`IFS=! eval echo \\$SEG_NAME_$choice`"
                done
                echo "\nPlease try again."
                packlist=""
                break
            fi
            found_pairs=0
            pair_error=0
            pairs_seg=
            continue
        fi
        if echo $packlist | grep $i >/dev/null 2>&1
        then
            found_pairs=1
        else
            if [ "$found_pairs" = 1 ]
            then
                pair_error=1
            fi
               found_pairs=0
        fi
        pairs_seg=${pairs_seg}" "${i}
    done

    if [ ! "$packlist" ]
    then
       continue
    fi

    i=0
    total_size=0
    while [ $i -lt $# ]
    do
        i=`expr $i + 1`
        choice=`eval echo $\`eval echo $i\``
        choice=`expr $choice + 0 2>/dev/null`
        if [ "$choice" -lt 1 -o "$choice" -gt $TAPE_SEGS -o "$choice" = "" ]
        then 
            echo "\nBad choice \"$choice\"."
            continue 2
        fi
        echo "${BOLD}\c"
        echo "   " "`IFS=! eval echo \\$SEG_NAME_$choice`"
        echo "${UNBOLD}\c"
        size=`eval echo \\$SEG_SIZE_$choice`
        total_size=`expr $total_size + $size`
    done
    echo "${BOLD}\c"
    echo "$total_size" | awk '
    { for (i = 0; i < 63; i++) printf (" "); 
      printf (" ------\n") 
      for (i = 0; i < 63; i++) printf (" "); 
      printf ("%4d MB Total\n",$1) 
    }'
    echo "${UNBOLD}\c"
    if yorn "" y
    then
        break
    fi
done

if [ -r $BASE/versions ]
then
    VERSION_FILE=$BASE/versions
else
    if [ -r ./versions ]
    then
        VERSION_FILE=`pwd`/versions
    else
        echo "Couldn't find versions file."
        failure
    fi
fi

#
# Check for version match, if required
#
echo "\n"
EOENAME=eoe1.sw.unix

os=`uname -r | sed 's/\.//g' | sed 's/[A-Z]//g'`
rev=`versions -n $EOENAME | grep $EOENAME | 
    awk '{print $3}' | sed "s/[0-9]*\(...\)/\1/"`

if [ "$os" -lt "$OS" -o "$rev" -lt "$REV" ]
then
    set -- $packlist
    while [ "$1" ]
    do
        case $1 in 
            $VERSION_MISMATCH_PATTERN)
            echo "\nThis system is currently running IRIX ${BOLD}$os, A${rev}${UNBOLD}."
            echo "You must upgrade to IRIX ${BOLD}$OS, A$REV ${UNBOLD}before running "
            echo "this Alias 2D Product."
            failure
            ;;
        esac
        shift
    done
fi

while :
do
    echo "\nDirectory to install in (${BOLD}/usr/Alias2D${UNBOLD}): \c"
    read dest_dir
    if [ "$dest_dir" ]
    then
        case "$dest_dir" in
            */Alias2D) ;;
            *) dest_dir="$dest_dir/Alias2D" ;;
        esac
        echo "Install product in ${BOLD}$dest_dir${UNBOLD} \c"
        if yorn 1
        then
            DEST_DIR=$dest_dir
            break
        fi
    else    
        break
    fi
done


FS="/`echo $DEST_DIR | cut -f2 -d'/'`"

#
# Check for enough free space
#
echo "\nChecking free space on ${FS}..."
total_size=0

for i in $packlist
do
    size=`eval echo \\$SEG_SIZE_$i`
    total_size=`expr $total_size + $size`
done

echo "This will require ${BOLD}${total_size}MB ${UNBOLD}of disk space."

availk=`df -k ${FS} | grep -v Filesystem | awk '{print $5}'`
availMB=`expr $availk / 1024`

if [ "$availMB" -lt "$total_size" ]
then
    echo
    echo "!! Insufficient disk space. !!"
    echo "There are only ${availMB}MB available in $FS,"
    echo "and ${total_size}MB is required."
    echo "Please make additional space availble and"
    echo "run this program again."
    failure
else
    echo "${availMB}MB of disk space is available in $FS."
fi

#
# Move old stuff out of the way
#

OLDpacklist=
ADDON=

if [ -d /usr/Alias2D ]
then
    # If no versions file, move whole tree
    if [ ! -f /usr/Alias2D/util/system/versions ]
    then
        echo
        if [ -d /usr/Alias2D.Old ]
        then
            sleep 2
            DATE=`date +%y.%j.%T`
            echo "/usr/Alias2D.Old already exists, moving /usr/Alias2D to /usr/Alias2D.$DATE"
            mv /usr/Alias2D /usr/Alias2D.$DATE
        else
            echo "\nMoving previous version of /usr/Alias2D to /usr/Alias2D.Old ..."
            mv /usr/Alias2D /usr/Alias2D.Old
        fi
    else # Real fun.  They have some of >= 3.3.1
         # Check against what we are loading.
         release=`grep RELEASE= /usr/Alias2D/util/system/versions`
         case $release in
            RELEASE=A*)
                echo "An alpha version of Alias 2D is currently"
                echo "installed on the machine."
                echo "Please remove the existing Alias 2D product tree"
                echo "manually and re-install."
                failure
                ;;
            RELEASE=1*) # Current major release level.  Just add a package.
                echo "Adding selected packages to currently installed product."
                OLDpacklist=`grep PACKLIST= /usr/Alias2D/util/system/versions |
                    cut -f2- -d=`
                ADDON=1
                ;;
            *)  echo "Your /usr/Alias2D directory seems damaged"
                echo "(bad \"versions\" file)"
                echo "Please remove the existing Alias 2D product"
                echo "manually and try again."
                failure
                ;;
         esac
    fi
fi

#
# Edit /etc/cshrc and /etc/profile
# to add umask 000, set environment variables
# (see if they need it first)

echo "\nYou may alter the login environments such that"
echo "the default permissions on all files allow read and write"
echo "by others.  This is done by setting the default umask to 0"
echo "in /etc/cshrc and /etc/profile."
echo
echo "Would you like all users to be able to access each"
echo "others files? \c"

if yorn 1 n
then
    if [ "$ALTER_LOGINS" = 1 ]
    then
        echo "Updating login environments..."

        for file in /etc/cshrc /etc/profile
        do
            sed 's/umask *[0-9][0-9][0-9]/umask 000/g' $file > /tmp/.lt
            if [ $? = 1 ]
            then
                echo "umask 000" >> $file
                rm -f /tmp/.lt
            else
                mv /tmp/.lt $file
            fi
        done
    fi
fi

#
# Read in the software
#
trap "" 1 2 3 15

if [ "$ADDON" ]
then
    cd /usr/Alias2D
else
    if [ ! -d ${DEST_DIR} ]
    then
        mkdir ${DEST_DIR} 
        if [ ! -d ${DEST_DIR} ]
        then
            echo "!! Cannot make install directory.  Please check permissions or disk space. !!"
            failure
        fi
    fi

    chmod 777 ${DEST_DIR}
    cd ${DEST_DIR}

    # In case of earlier failure
    if [ "`/bin/ls`" ]
    then
        echo "\n\nERROR: Files already exist in ${DEST_DIR}."
        echo "       please remove them or select another directory."
        failure
    fi
fi

echo "Loading software from $BASE..."

umask 0
lastpos=0

TARGET=`pwd`
for i in $packlist
do
    cd $BASE/`IFS=! eval echo \\$SEG_BASE_$i`
    find `IFS=! eval echo \\$SEG_DIR_$i` -print | cpio -pdum $TARGET
done

cd $TARGET

#
# Now fix permissions
#

if [ -r CyberDemo/bin/playon ]
then
    chown root CyberDemo/bin/playon
    chmod 4111 CyberDemo/bin/playon

    # Demo is installed, take care of Newport Graphics req.

    if [ "`hinv -c graphics | egrep 'Indy|XL'`" ]
    then
            rm -f CyberDemo/events.bin
            cp CyberDemo/events.bin.indy CyberDemo/events.bin
            chmod 444 CyberDemo/events.bin
    fi

fi

if [ ${DEST_DIR} != "/usr/Alias2D" ]
then
    ln -sf ${DEST_DIR} /usr/Alias2D 
fi

echo "Installing software..."

if [ -d /usr/lib/X11/app-defaults ]
then
    cd /usr/Alias2D/util/app-defaults
    X=`echo *`
    if [ "$X" != '*' ]
    then
    for i in $X
    do
        rm -f /usr/lib/X11/app-defaults/$i
        ln -s /usr/Alias2D/util/app-defaults/$i /usr/lib/X11/app-defaults/$i
    done
    fi
fi

ln -s /usr/Alias2D/util/system/Eclipse.icon /usr/lib/images/Eclipse.icon

if [ -d /usr/lib/filetype/install ]
then
    echo "Installing WorkSpace Icons..."
    cd /usr/lib/filetype/install
    cp /usr/Alias2D/util/system/*.ftr .
    if [ ! -d /usr/lib/filetype/install/iconlib ]
    then
        mkdir /usr/lib/filetype/install/iconlib
        chmod 777 /usr/lib/filetype/install/iconlib
    fi
    cp /usr/Alias2D/util/system/*.fti iconlib
    cd ..
    make
fi

if [ ! -r /usr/Alias2D/util/custom/en ]
then
    touch /usr/Alias2D/util/custom/en
    chmod 666 /usr/Alias2D/util/custom/en
fi

chmod 666 /usr/Alias2D/util/system/dashboard.pxy
chmod 666 /usr/Alias2D/util/system/.id.pxy

# set virtual swap to proper size under IRIX 5 
case "$os" in
5*)
	if chkconfig vswap
	then
	        VSWCFG=/etc/config/vswap.options
	        mmemMb=`hinv -t memory | awk '{print $4}'`
	        mmemBl=`expr $mmemMb '*' 2000`
	        swspBl=`swap -l | awk 'BEGIN {sw = 0} {sw += $4} END {print sw}'`
	        totBl=`expr $mmemBl + $swspBl`
	
#    Round total Blocks down to the nearest Megabyte
#
#    There are 1000 * 2 (blocks per kilobyte) blocks in a megabyte.
	
	        MbBl=`expr 1000 '*' 2`
	        totBl=`expr '(' $totBl '/' $MbBl ')' '*' $MbBl`
	
#    If the options file for virtual swap (/etc/config/vswap.options) does
#    not exist, create the file with the amount of virtual swap set to the
#    default of 40 Megabytes (80000 blocks). The next step will then up this
#    to the correct amount.
	
	        if test ! -f $VSWCFG
		then
	            defvsBl=80000
	            cat > $VSWCFG << EOF
#
# This file is used to the startup script /etc/init.d/swap
# to determine the amount of virtual swap to add. You can
# totally disable the addition of virtual swap by
# 'chkconfig vswap off'
# See swap(1M) for a discussion on virtual swap
#
vswaplen=$defvsBl
EOF
	        fi
	
	        if test -f $VSWCFG
		then
	
#    Scan the options file for the current size of virtual swap. If there are
#    more than one `vswaplen=' lines, take the value from the last line.
	
	            currvs=`grep "^vswaplen=" $VSWCFG | awk -F= '{print $2}' | tail -1`
	
#    If the value of vswaplen from the options file does not equal the optimum
#    value conditionally edit the options file and change the value of vswaplen
#    to the optimum value.
	
	            if test $currvs != $totBl
		    then
	
#    Make a backup copy of the virtual swap options file.
	
                        cp $VSWCFG $VSWCFG.pre_alias
	
			sed "s/^\(vswaplen=\).*/\1$totBl/" $VSWCFG.pre_alias >$VSWCFG
	            fi
	        fi
	fi ;;
esac

#
# Indicate which packages are installed
#
cp ${VERSION_FILE} /usr/Alias2D/util/system
echo "PACKLIST=${OLDpacklist} $packlist" >> /usr/Alias2D/util/system/versions

cp ${BASE}/.cut  /usr/Alias2D
chmod 444 /usr/Alias2D/.cut

#
# If no encryption file, get the system id and tell them to call
#
if [ -x /usr/Alias2D/sysbin/getid -a ! -s /usr/Alias2D/util/custom/en ]
then
    echo "\n\n\n"
    echo "Alias software requires a valid encryption"
    echo "string in order to run."
    echo "Please fill in your SPAR form using the following"
    echo "information:"
    echo
    echo ${BOLD}
    /usr/Alias2D/sysbin/getid
    echo ${UNBOLD}
    echo
fi

cp /usr/Alias2D/sysbin/alias2Dpath /bin

echo "\n\nInstallation complete."

echo "\nWould you like to install tape device links at this time? \c"
if yorn 1 n
then
    /usr/Alias2D/sysbin/install_SCSI
else
    echo "\nYou may install tape devices at any time by running"
    echo "/usr/Alias2D/sysbin/install_SCSI."
fi

echo "\nWould you like to install a Wacom II tablet at this time? \c"
if yorn 1 n
then
    /usr/Alias2D/sysbin/install_tablet
else
    echo "\nYou may install a tablet at any time by running"
    echo "/usr/Alias2D/sysbin/install_tablet."
fi

echo "Done\n"

out_msg
exit 0
