#!/bin/csh -f
#Tag 1024

#    The preceding tag label has to be on line 2 or workspace does not
#    recognize it properly.


#    Altabletinstall: The following steps are performed to install the tablet
#    drivers and configure the IRIX kernel to recognize them:
#
#    1) Set a default search path:
#
#       The components of the search path are directories that contain tools
#       which are used to load the tablet drivers.
#
#    2) Verify that the user is root. If not, exit.
#
#    3) Determine where this script will place its working files. If /usr/tmp
#       is a directory and is writable, working files will go there; otherwise
#       they will be placed in /tmp.
#
#    4) Determine by which type of media the tablet drivers will be installed:
#
#       The tablet drivers may be installed from CD-ROM or tape (local or
#       remote). Altabletinstall first asks the installer if they wish to
#       install the drivers from a CD-ROM. If they say `yes', altabletinstall
#       determines the mount point of the CD-ROM and verifies it (verifies that
#       the system files image is there). If the contents of the CD-ROM are
#       valid, the installation continues, else altabletinstall exits with an
#       explanatory error message.
#
#       If the installer says `no' to the question of installing from CD-ROM,
#       altabletinstall asks them if they are installing from tape. If they say
#       `no', altabletinstall informs them that the tablet drivers may only be
#       installed from CD-ROM or tape and exits. If they say `yes' to installing
#       from a tape, altabletinstall asks them if the tape drive is connected to
#       the machine. If they say `yes', altabletinstall (with installer
#       interaction) determines the (rewinding and non-rewinding) device names
#       of a local tape drive that can be used for the installation. If the
#       installer says `no' to installing from a local tape drive,
#       altabletinstall asks them if they wish to install the tablet drivers
#       using a remote tape drive. If they say `no', altabletinstall tells them
#       that once they know which tape device they wish to use, they should
#       restart the script at that time and exits. If they say `yes' to
#       installing using a remote tape drive, altabletinstall queries them for
#       three pieces of information:
#
#       - the hostname of the machine with the tape drive
#       - the username of a user account on the remote host which will be
#         used to perform remote commands on the remote host
#       - the full pathname of a rewinding tape device on that machine (only if
#         the the default tape devices do not exist OR are not used for the
#         installation).
#
#       Given this information, altabletinstall verifies the remote hostname
#       (queries DNS and/or hosts file) and verifies that remote commands
#       between your machine and the remote host succeed. If this fails,
#       altabletinstall lists a number of possible reasons for the failure and
#       asks the installer to check them out. Once the execution of remote
#       commands has been verified, altabletinstall (with installer interaction)
#       determines the (rewinding and non-rewinding) device names of a tape
#       drive on the remote host. It verifies that the tape drive is accessible
#       and available for the installation.
#
#       At the end of this step (assuming no errors have occurred),
#       altabletinstall will know the type of media to use for the installation
#       of the tablet drivers (CD-ROM or tape) and its location (CD-ROM mount
#       point or tape device names for rewinding and non-rewinding tape
#       devices).
#
#    5) Inform the installer that the correct versions of the tablet drivers
#       must be installed in order for tablets to be used in conjunction with
#       the Alias software. Before the tablet drivers can be installed, this
#       script must verify that the operating system is acceptable.
#
#    6) Verify that the operating system is acceptable:
#
#       The tablet drivers can be installed on any version of IRIX 5 or IRIX 4.
#
#       If the operating system is a variant of IRIX 5, set the required
#       variables to indicate the location of the tablet drivers and system
#       file under IRIX 5.
#
#       If the operating system is a variant of IRIX 4, set the required
#       variables to indicate the location of the tablet drivers and system
#       file under IRIX 4.
#
#       If the operating system is not a variant of IRIX 5 or IRIX 4, inform the
#       installer that they must upgrade the operating system to a version of
#       IRIX 4 or IRIX 5 before the drivers can be installed.
#
#    7) In order for the ALIAS software to use tablets, the IRIX kernel must
#       have the correct versions of the tablet drivers installed and a kernel
#       system file modified. The locations of these files depends on the
#       operating system running on the machine: if the machine is running
#       a version of IRIX 4, the files comprising the tablet drivers are
#       installed in /usr/sysgen/boot and master.d while the kernel system
#       file has a pathname of /usr/sysgen/system; if the machine is running
#       a version of IRIX 5, the files comprising the tablet drivers are
#       installed in /var/sysgen/boot and master.d while the kernel system
#       file that may be modified has a pathname of /var/sysgen/system/gfx.sm.
#
#       After informing the installer of the requirements for the drivers, the
#       script asks them if they wish to install the drivers. If they say
#       `yes', the installation continues; else the script exits after minor
#       cleanup.
#
#       Assuming the installer assumed the installation of the tablet drivers,
#       the following steps are performed:
#
#       If existing versions of the driver files are found, the driver files
#       from the ALIAS distribution media will be compared with the existing
#       files. If the files are the same (i.e. aliaswacom.o from the media is
#       the same as aliaswacom.o on disk), the file from the distribution
#       media is removed. If the files are different, the file from the
#       distribution media replaces the existing file, which is saved.
#
#       If the kernel system file needs to be modified, a copy of the existing
#       version is saved first, then the file is modified.
#
#       If any of the tablet driver files from the distribution media are
#       installed or the kernel system file is modified, a new IRIX kernel
#       will be built which incorporates the tablet drivers.
#
#    8) If any tablet drivers were added or the kernel system file was modified,
#       build a new IRIX kernel which recognizes the tablet drivers. If the
#       kernel build succeeds, the installer is instructed to reboot the the
#       machine to start using the new kernel.
#
#       If the kernel rebuild fails, the installer is instructed to reboot
#       the machine and answer yes when asked if you wish to `Automatically
#       reconfigure the operating system'. When a new IRIX kernel is built,
#       it will recognize tablets.
#
#    9) In order for the X window system to interpret the tablet data correctly,
#       the correct version of:
#
#           /usr/lib/X11/input/config/tablet
#
#       must be installed.
#
#       If an existing version of the tablet configuration file is found, the
#       configuration file from the ALIAS distribution media is compared with
#       the existing file. If the files are the same, the file from the
#       distribution tape is removed. If the files are different, the file
#       from the distribution tape replaces the existing file, which is saved.
#
#       There are three versions of the tablet configuration file, one for each
#       of Alias StudioPaint, Alias Eclipse and other Alias products (i.e.:
#       Alias Studio). InstallAlias asks the installer which product they are
#       using and extracts the correct version of the configuration file from
#       the tape or CD-ROM.
#
#   10) Determine the type of tablet that the installer is using:
#
#       Ask the installer to indicate the maker of their tablet (one of Hitachi,
#       Kurta and Wacom). Once they have indicated the maker of their tablet,
#       list all supported tablets manufactured by that maker and ask the
#       installer to indicate the type of tablet they are installing support
#       for.
#
#   11) Ask the installer on which serial port the tablet is connected to and
#       verify that it exists.
#
#   12) Cleanup old device files: 
#
#       Verify there is no old device file in /dev/input which pertains to the
#       serial port the tablet is connected to:
#
#       Find the major and minor numbers of the serial port the tablet is
#       connected to. Search /dev/input for a device file with the same
#       <major #, minor #> pair. If such a device file exists, inform the
#       installer that the device file is being removed and remove it.
#
#       Also remove /dev/input/altablet-* as the Alias software can't
#       handle multiple tablets.
#
#   13) Setup the tablet device link in /dev/input:
#
#           /dev/ttydN <-> /dev/input/altablet-<tablet type>
#
#       so that the Alias software can use it. This identifes the serial port
#       and type of tablet to the Alias software. When the link has been
#       created, make the serial port read/write by everyone.
#
#   14) Rename changed system files:
#
#       Go though the "list of saved system files" (All files with a prefix of
#       ".pre_alias"). Compare each saved file with its namesake (minus the
#       ".pre_alias" suffix). If the two files are the same, remove the
#       ".pre_alias" file; else rename the ".pre_alias" file to its namesake
#       with a suffix of "=" (i.e.: mv /etc/passwd.pre_alias /etc/passwd=). The
#       files with a suffix of "=" represent the state of each file before the
#       tablet drivers were installed. As each file is renamed, it is reported
#       to the installer.
#
#   15) If the tablet drivers were added to the system, remind the installer
#       about the state of the rebuilt IRIX kernel:
#
#       If the IRIX kernel rebuild was successful, remind the installer to
#       reboot the machine to install the new kernel.
#
#       If the IRIX kernel rebuild was not successful, remind the installer that
#       they may have to reboot the machine a few times before the rebuild
#       succeeds.
#
#   16) Finish up:
#
#       Rewind the tape drive (if the install was done from tape) and inform the
#       installer that the installation is complete. 

unset tfd
onintr userexit

#   Variables used by altabletinstall

set ALHIT = aliashitachi
set ALKUR = aliaskurta
set ALWAC = aliaswacom
set ALTAB = altablet
set ATABS = ($ALHIT $ALKUR $ALWAC $ALTAB)

set mod_files = ""
set DLR = '$'
set XTABLET = /usr/lib/X11/input/config/tablet

#    Different X tablet configuration files for StudioPaint, Eclipse and
#    the rest of the products (i.e.: Studio).

set XTABSTPA = tablet.spaint
set XTABECL = tablet.eclipse
set XTABDFLT = tablet.dflt
set NSTPA = 1
set NECL = 2
set NDFLT = 3

#    Tablet types

set THIT = 1 # Hitachi
set TKUR = 2 # Kurta
set TWAC = 3 # Wacom

#  Name of tar image on CD-ROM containing tablet files

set SYSFILES = dist/sysfiles.tar

#    Set the search path

set path = (/usr/bsd /usr/bin /usr/sbin /usr/lbin /bin /etc)

#    Verify that the user is root

set whoami = `id | cut -c6-11`
if ("$whoami" != "(root)") then
    echo ""
    echo "This procedure must be run by the super user."
    echo "Please sign on as root and try again."
    echo -n "Press <ENTER> to continue: "
    set a = $<
    exit 1
endif

#    Determine where the script will place its working files. If /usr/tmp is
#    a directory and is writable, working files will go there; otherwise they
#    will be placed in /tmp.

if (-d /var/tmp && -w /var/tmp) then
    set TMPDIR = /var/tmp
else if (-d /usr/tmp && -w /usr/tmp) then
    set TMPDIR = /usr/tmp
else
    set TMPDIR = /tmp
endif

set HOSTS_TMP = $TMPDIR/thosts.$$
set OERR = $TMPDIR/toerr.$$
set OUT = $TMPDIR/tout.$$
set tfd

#    Determine by which type of media the tablet drivers will be installed:
#
#    The tablet drivers may be installed from CD-ROM or tape (local or remote).
#    Altabletinstall first asks the installer if they wish to install the
#    drivers from a CD-ROM. If they say `yes', altabletinstall determines the
#    mount point of the CD-ROM and verifies it (verifies that the system files
#    image is there). If the contents of the CD-ROM are valid, the installation
#    continues, else altabletinstall exits with an explanatory error message.
#
#    If the installer says `no' to the question of installing from CD-ROM,
#    altabletinstall asks them if they are installing from tape. If they say
#    `no', altabletinstall informs them that the tablet drivers may only be
#    installed from CD-ROM or tape and exits. If they say `yes' to installing
#    from a tape, altabletinstall asks them if the tape drive is connected to
#    the machine. If they say `yes', altabletinstall (with installer
#    interaction) determines the (rewinding and non-rewinding) device names of
#    a local tape drive that can be used for the installation. If the installer
#    says `no' to installing from a local tape drive, altabletinstall asks them
#    if they wish to install the tablet drivers using a remote tape drive. If
#    they say `no', altabletinstall tells them that once they know which tape
#    device they wish to use, they should restart the script at that time and
#    exits. If they say `yes' to installing using a remote tape drive,
#    altabletinstall queries them for three pieces of information:
#
#    - the hostname of the machine with the tape drive
#    - the username of a user account on the remote host which will be
#      used to perform remote commands on the remote host
#    - the full pathname of a rewinding tape device on that machine (only if
#      the the default tape devices do not exist OR are not used for the
#      installation).
#
#    Given this information, altabletinstall verifies the remote hostname
#    (queries DNS and/or hosts file) and verifies that remote commands between
#    your machine and the remote host succeed. If this fails, altabletinstall
#    lists a number of possible reasons for the failure and asks the installer
#    to check them out. Once the execution of remote commands has been verified,
#    altabletinstall (with installer interaction) determines the (rewinding and
#    non-rewinding) device names of a tape drive on the remote host. It
#    verifies that the tape drive is accessible and available for the
#    installation.
#
#    At the end of this step (assuming no errors have occurred), altabletinstall
#    will know the type of media to use for the installation of the tablet
#    drivers (CD-ROM or tape) and its location (CD-ROM mount point or
#    tape device names for rewinding and non-rewinding tape devices).

unset mtype
echo ""
echo "Before altabletinstall can install the tablet drivers, it must determine the"
echo "type of media that the drivers are being installed from."

#    Ask the installer if they wish to install the tablet drivers from a CD-ROM.
#    If they say `yes', altabletinstall has to know where the CD-ROM is mounted.
#    Altabletinstall determines its mount point and asks the installer if it is
#    the correct location. If they say `no', Altabletinstall asks them where the
#    CD-ROM is mounted.

echo ""
echo -n "Are you installing the tablet drivers from a CD-ROM? (yes/no): "
while (1)
    set ans = $<
    if ("$ans" !~ [yY][eE][sS] && "$ans" !~ [nN][oO]) then
        echo -n "Please specify yes or no: "
    else
        break
    endif
end
echo ""
if ($ans =~ [yY][eE][sS]) then
    set almedia = "CD-ROM"
    set cdir = `dirname $0`
    if ("$cdir" == ".") then
        set cdir = $cwd
    endif
    echo -n "Is the $almedia mounted at $cdir? (yes/no): "
    while (1)
        set ans = $<
        if ("$ans" !~ [yY][eE][sS] && "$ans" !~ [nN][oO]) then
            echo -n "Please specify yes or no: "
        else
            break
        endif
    end
    if ($ans =~ [Yy][Ee][Ss]) then
        set ALIAS_CDPATH = $cdir
    else
        echo ""
        while (1)
            echo -n "Where is the $almedia mounted? (please specify a full pathname): "
            set pname = $<
            set ALIAS_CDPATH = ($pname)
            if ($#ALIAS_CDPATH == 1) break
        end
    endif

#    If the tablet drivers are being installed from a CD-ROM, the CD-ROM must
#    have been mounted by the installer at a mount point specified by
#    the installer ($ALIAS_CDPATH).

    echo ""
    echo "Validating the $almedia mounted at $ALIAS_CDPATH ..."

#    Once the mount point of the CD-ROM has been determined, altabletinstall
#    validates it:
#
#    Verify that the CD-ROM mount point specified by the installer exists, it
#    is a directory and the CD-ROM is mounted (actually, the pathname specified
#    by ALIAS_CDPATH contains proper images to install the tablet drivers).

    if (-e $ALIAS_CDPATH) then
        if (-d $ALIAS_CDPATH) then
            set valid_cd
            foreach image ($SYSFILES)
                if (! -f $ALIAS_CDPATH/$image) then
                    echo ""
                    echo "The file $image is missing from the CD-ROM."
                    unset valid_cd
                endif
            end
            if (! $?valid_cd) then
                echo ""
                echo "Either the CD-ROM is not mounted, the correct CD-ROM is not being used, the"
                echo "CD-ROM is corrupted or the CD-ROM mount point you specified ($ALIAS_CDPATH)"
                echo "is not correct."
                echo ""
                echo "Once you have resolved the problem, restart the altabletinstall procedure."
                echo ""
                echo -n "Press <ENTER> to continue: "
                set a = $<
                exit 1
            endif
        else
            echo ""
            echo "The CD-ROM mount point that you specified ($ALIAS_CDPATH) is not a directory."
            echo ""
            echo "Please make sure that the CD-ROM is mounted and the pathname that you specified"
            echo "($ALIAS_CDPATH) is the mount point of the CD-ROM, then restart altabletinstall."
            echo ""
            echo -n "Press <ENTER> to continue: "
            set a = $<
            exit 1
        endif
    else
        echo ""
        echo "The CD-ROM mount point specified by the installer ($ALIAS_CDPATH) does not exist."
        echo ""
        echo "Please make sure that the CD-ROM is mounted and/or the pathname specified by"
        echo "the installer is correct, then restart altabletinstall."
        echo ""
        echo -n "Press <ENTER> to continue: "
        set a = $<
        exit 1
    endif
    set mtype = $almedia
endif

#    If the tablet drivers are not being installed from a CD-ROM, ask the
#    installer if they are using a tape drive for the installation.

if (! $?mtype) then
    echo -n "Are you installing the tablet drivers from a tape drive? (yes/no): "
    while (1)
        set ans = $<
        if ("$ans" !~ [yY][eE][sS] && "$ans" !~ [nN][oO]) then
            echo -n "Please specify yes or no: "
        else
            break
        endif
    end
    echo ""
    if ($ans =~ [Yy][Ee][Ss]) then
        unset notape
        set almedia = "tape"
        unset TAPE

#    Once it has been ascertained that the install is being done via a tape
#    drive, determine if the tape drive is local (connected to the machine)
#    or remote (connected to another machine on the network).

        echo -n "Is the tape drive connected to your machine? (yes/no): "
        while (1)
            set ans = $<
            if ("$ans" !~ [yY][eE][sS] && "$ans" !~ [nN][oO]) then
                echo -n "Please specify yes or no: "
            else
                break
            endif
        end
        if ($ans =~ [Yy][Ee][Ss]) then

#    If the installer indicates that they wish to install the tablet drivers
#    using a local tape drive, probe for the existence of /dev/tape and
#    /dev/nrtape. If these devices don't exist or an error occurs when
#    accessing them, warn the installer; else ask them if they wish to
#    continue the installation using the default tape devices.

            set mtype = "local tape"
            set usedflt
            echo ""
            echo "Checking for default tape devices ..."
            foreach tapedev (/dev/{tape,nrtape})
                if ($tapedev:t == "nrtape") then
                    set fill = "non-"
                else
                    set fill = ""
                endif

#    Verify that the default tape devices are character devices. If they are
#    character devices, run `mt stat' on each to make sure they are accessible.

                if (-c $tapedev) then
                    mt -t $tapedev stat >& $OUT
                    if ($status != 0) then
                        echo ""
                        echo "The default ${fill}rewinding tape device ($tapedev) exists,"
                        echo "but an error occurs when altabletinstall attempts to access it:"
                        echo ""
                        cat $OUT | sed "s/^/    /"
                        unset usedflt
                    endif
                    rm -f $OUT
                else

#    If the default tape device is not a character device, verify that it
#    exists.

                    echo ""
                    if (-e $tapedev) then
                        echo "The default ${fill}rewinding tape device ($tapedev) exists,"
                        echo "but it is not a character device as it should be."
                    else
                        echo "The default ${fill}rewinding tape device ($tapedev) does not exist."
                    endif
                    unset usedflt
                endif
            end
            if ($?usedflt) then
                echo ""
                echo "Both default tape devices (/dev/tape, /dev/nrtape) exist and are accessible."
                echo ""
                echo -n "Do you wish to use them to install the tablet drivers? (yes/no): "
                while (1)
                    set ans = $<
                    if ("$ans" !~ [yY][eE][sS] && "$ans" !~ [nN][oO]) then
                        echo -n "Please specify yes or no: "
                    else
                        break
                    endif
                end

#    If the default tape drives exist and are accessible, ask the installer
#    if they wish to install the tablet drivers using them. If they say `yes',
#    set TAPE and NRTAPE to the default tape device names.

                if ($ans =~ [Yy][Ee][Ss]) then
                    set TAPE = /dev/tape
                    set NRTAPE = /dev/nrtape
                endif
            endif

            if (! $?TAPE) then

#    If the default tape drives do not exist or are not usable (not accessible),
#    ask the installer if they wish to install the drivers from an alternate
#    pair of devices (one rewinding, the other non-rewinding).
#
#    If the installer wishes to specify an alternate tape device for the
#    installation, ask them their choice.

                echo ""
                echo "In order to install the tablet drivers, you must specify the device name of"
                echo "a rewinding tape device (i.e.: /dev/rmt/tps0d2). Altabletinstall will verify"
                echo "that it (and its non-rewinding version) exist and are accessible before"
                echo "allowing them to be used for the installation."
                echo ""
                while (1)
                    echo "Which tape device do you wish to use? (please specify a full"
                    echo -n "pathname): "
                    set pname = $<
                    set altape = ($pname)
                    if ($#altape == 1) break
                end

#    Given the installers choice of an alternate rewindable tape device, verify:
#
#    - the device exists 
#    - it is a character device (i.e.: tape device)
#    - its minor device number is even; this indicates a rewindable device
#    - the corresponding non-rewind device exists (same major device number,
#      incremented minor number)
#    - Both devices pass the `mt stat' test with no errors
#
#    If any of these tests fail, altabletinstall informs the installer of the
#    problem and exits; else it continues with the installation.

                echo ""
                echo "Validating your choice of tape device ..."
                if (! -e $altape) then
                    echo ""
                    echo "The tape device that you specified ($altape) does not exist."
                    echo ""
                    echo "You must specify an existing tape device."
                    set notape
                else
                    if (-c $altape) then
                        set maj = `ls -l $altape | awk -F"," '{print $1}' | awk '{print $5}'`
                        set min = `ls -l $altape | awk -F"," '{print $2}' | awk '{print $1}'`
                        @ rem = $min % 2
                        if ($rem) then
                            echo ""
                            echo "The tape device that you specified ($altape) is not a rewinding"
                            echo "tape device."
                            echo ""
                            echo "You must specify a rewinding tape device, not a non-rewinding tape device."
                            echo ""
                            echo "For example, you should choose /dev/rmt/tps0d2 over /dev/rmt/tps0d2nr, as the"
                            echo 'second device is a non-rewinding device (note the "nr").'
                            set notape
                        else
                            set dpref = $altape:h
                            @ min++
                            if ($min < 10) then
                                set majmin = "$maj,  $min"
                            else if ($min < 100) then
                                set majmin = "$maj, $min"
                            else
                                set majmin = "$maj,$min"
                            endif

#    In awk:
#
#    crw-rw-rw-   2 root     sys       23, 64 Feb 11 09:19 /dev/rmt/tps0d2
#
#    has 10 elements while
#
#    crw-rw-rw-   2 root     sys       23,224 Feb 11 09:19 /dev/rmt/tps0d7
#
#    has 9 elements.
#
#    The difference is that the minor device number in the first example is
#    2 chars, thus creating a space in the `ls -l' output. This accounts for
#    the extra element.
#
#    In order to get the filename portion of the line, the next command uses
#    awk to break the line at the comma separating the major and minor numbers,
#    then prints the fifth element (the filename).
#
#    Also note the `ls -l $dpref/.'. The `.' is required to get an accurate
#    long listing if $dpref is a symlink to another directory.

                            set nrdev = `ls -l $dpref/. | grep " $majmin " | awk -F"," '{print $2}' | awk '{print $5}'`

#    A new tangle. Under IRIX 5, a tape device may have two (or more?) different
#    names that share the same <major,minor> number:
#
#    crw-rw-rw-    1 root     sys      144,224 Jul 10 11:37 /dev/rmt/tps0d7
#    crw-rw-rw-    1 root     sys      144,224 Jul 10 11:37 /dev/rmt/tps0d7s
#    crw-rw-rw-    1 root     sys      144,225 Jul 10 11:37 /dev/rmt/tps0d7nr
#    crw-rw-rw-    1 root     sys      144,225 Jul 10 11:37 /dev/rmt/tps0d7nrs
#
#    and exhibit the same behavior (i.e.: /dev/rmt/tps0d7 and /dev/tps0d7s
#    both access tape device 7 on scsi controller 0 which swaps bytes during
#    reads and writes) and are equivalent, despite the different name. All
#    altabletinstall has to do is use one of the device names (the first one).

                            if ($#nrdev > 1) then
                                set nrdev = $nrdev[1]
                            endif
                            set alnrtape = "$dpref/$nrdev"
                            foreach tapedev ($altape $alnrtape)
                                set tdev = $tapedev:t
                                if ($tdev == $nrdev) then
                                    set fill = "non-"
                                else
                                    set fill = ""
                                endif
                                if ($tdev != "") then
                                    mt -t $tapedev stat >& $OUT
                                    if ($status != 0) then
                                        set notape
                                        echo ""
                                        echo "The ${fill}rewinding tape device ($tapedev) exists, but an error occurs"
                                        echo "when altabletinstall attempts to access it:"
                                        echo ""
                                        cat $OUT | sed "s/^/    /"
                                    endif
                                    rm -f $OUT
                                else
                                    set notape
                                    echo ""
                                    echo "The ${fill}rewinding version of the tape device that you specified does not exist."
                                endif
                            end
                            if (! $?notape) then
                                set TAPE = $altape
                                set NRTAPE = $alnrtape
                            endif
                        endif
                    else
                        echo ""
                        echo "All tape devices are character devices. The tape device that you specified"
                        echo "($altape) is not a character device and cannot be used for the installation."
                        echo ""
                        echo "You must specify a valid tape device."
                        set notape
                    endif
                endif
            endif
        else

#    If the tablet driverd are not being installed using a local tape drive, ask
#    the installer if they wish to install the drivers using a remote tape
#    drive. If they say `no', explain that since they have indicated that
#    they wish to install from tape, they must choose to install from a local
#    or remote tape drive. If they say `yes', continue with the installation.

            echo ""
            echo "Do you wish to install the tablet drivers using a remote tape drive?"
            echo -n "(yes/no): "
            while (1)
                set ans = $<
                if ("$ans" !~ [yY][eE][sS] && "$ans" !~ [nN][oO]) then
                    echo -n "Please specify yes or no: "
                else
                    break
                endif
            end
            if ($ans =~ [Yy][Ee][Ss]) then

#    Given that the tablet drivers are being installed using a remote tape
#    drive, altabletinstall requires three pieces of information:
#
#    - the hostname of the machine with the tape drive
#    - the username of a remote account; all commands performed on the
#      remote machine will be executed by that user.
#    - the full pathname of a rewinding tape device on that machine (only
#      if the default tape devices do not exist OR the installer chooses
#      not to use them for the installation).
#
#    Given this information, altabletinstall constructs two variables used by
#    tar. The format of these variables is:
#
#        <username>@<machine>:/dev/<rewinding tape drive>
#        <username>@<machine>:/dev/<non-rewinding tape drive>

                set mtype = "remote tape"
                echo ""
                echo "In order to install the tablet drivers, altabletinstall requires three"
                echo "pieces of information:"
                echo ""
                echo "    - the hostname of the machine whose tape drive you are using."
                echo "    - the full pathname of a (rewinding) tape device on the remote machine."
                echo "    - the username of a user account on the remote machine. All commands"
                echo "      executed on the remote machine are carried out as that user (i.e.: when"
                echo "      extracting the tablet drivers from the distribution $almedia)."
                echo ""

                while (1)
                    echo -n "Please enter the hostname of the machine whose tape drive you are using: "
                    set hname = $<
                    set REMOTE_TAPE_HOST = ($hname)
                    if ($#REMOTE_TAPE_HOST != 1) continue

#    Verify that this machine "knows" about the remote host:
#
#    If a valid resolv.conf file exists (the file exists and has a nameserver
#    entry), try to validate the remote host via the DNS. Since nslookup does
#    not return a status to indicate errors, altabletinatall assumes an error
#    has occurred (i.e.: the host is unknown) if something is written to
#    standard error.
#
#    If the DNS lookup fails (unlikely if the hostname is valid), search
#    the local hosts file (/etc/hosts) for the hostname. If this lookup
#    fails, indicate to the installer that the remote host is unknown.

                    set shf
                    unset unknown_rem_host
                    if (-f /usr/etc/resolv.conf) then
                        set ns = `grep "^nameserver[ 	]" /usr/etc/resolv.conf`
                        if ($#ns != 0) then
                            (nslookup $REMOTE_TAPE_HOST > /dev/null) >& $HOSTS_TMP
                            if (-z $HOSTS_TMP) then
                                unset shf
                            endif
                        endif
                    endif

                    if ($?shf) then
                        sed "/^#/d" /etc/hosts | awk '{for (i=2; i<=NF; i++) print $i}' >& $HOSTS_TMP
                        grep -s "^${REMOTE_TAPE_HOST}${DLR}" $HOSTS_TMP >& /dev/null
                        if ($status != 0) then
                            grep -s "^${REMOTE_TAPE_HOST}\." $HOSTS_TMP >& /dev/null
                            if ($status != 0) set unknown_rem_host
                        endif
                    endif
                    rm -f $HOSTS_TMP

#    If the specified host is unknown, ask the installer if they wish to
#    specify another hostname. If they say `no', exit; else accept and
#    validate the new hostname.

                    if ($?unknown_rem_host) then
                        echo ""
                        echo "Altabletinstall is unable to verify the existence of $REMOTE_TAPE_HOST."
                        echo ""
                        echo -n "Do you wish to specify another hostname? (yes/no): "
                        while (1)
                            set ans = $<
                            if ("$ans" !~ [yY][eE][sS] && "$ans" !~ [nN][oO]) then
                                echo -n "Please specify yes or no: "
                            else
                                break
                            endif
                        end
                        echo ""
                        if ($ans =~ [Nn][Oo]) break
                    else
                        break
                    endif
                end
                if ($?unknown_rem_host) then
                    set errmsg1 = "$REMOTE_TAPE_HOST is an unknown host."
                    goto baderr
                endif

#    Now that altabletinstall has the hostname of a valid remote host, it asks
#    for the username of the user account on the machine. This account is
#    necessary to carry out remote commands on the remote host.

                echo ""
                while (1)
                    echo "Please enter the username of the user account on $REMOTE_TAPE_HOST"
                    echo -n "that will be used to execute remote commands from altabletinstall: "
                    set puser = $<
                    set ruser = ($puser)
                    if ($#ruser == 1) break
                end

#    Before altabletinstall can continue , it has to test if can get root
#    access to the remote machine. If "rsh <hostname> -l <ruser> date" fails
#    (where ruser is the username specified by the installer), remote commands
#    between this machine and the remote machine will not work.

                echo ""
                echo "Verifying remote command access to $REMOTE_TAPE_HOST ..."
                set hostname = `hostname`
                rsh $REMOTE_TAPE_HOST -l $ruser date >& /dev/null
                if ($status != 0) then
                    echo ""
                    echo "Altabletinstall cannot run commands as root on $REMOTE_TAPE_HOST. There are"
                    echo "three possible reasons for this:"
                    echo ""
                    echo "    - there is no user account on $REMOTE_TAPE_HOST with a username of $ruser."
                    echo "    - the .rhosts file in ${ruser}'s home directory on $REMOTE_TAPE_HOST is"
                    echo "      not complete."
                    echo "    - the permissions of the .rhosts file are incorrect."
                    echo ""
                    echo "These problems must be addressed in order for the installation to continue."
                    echo ""
                    echo "Assuming that the $ruser account exists, the .rhosts file must be edited"
                    echo "in order to allow root to execute commands as $ruser on $REMOTE_TAPE_HOST."
                    echo "The following entry must appear in ${ruser}'s .rhosts file on ${REMOTE_TAPE_HOST}:"
                    echo ""
                    if ($ruser == root) then
                        echo "$hostname"
                    else
                        echo "$hostname root"
                    endif
                    echo ""   
                    echo "If the hostname portion of the entry ($hostname) does not include your"
                    echo "domain name (assuming you have a domain name), be sure to include it as"
                    echo "part of the hostname. Verify that the entry appears in the .rhosts file"
                    echo "and the permissions are correct (chmod go-w .rhosts)."
                    echo ""
                    echo "Any entries that you add to the .rhosts file should be removed when"
                    echo "altabletinstall has successfully completed."
                    echo ""
                    echo "If you are unsure of what to do next, contact Alias Assist for help."
                    echo ""
                    echo -n "Press <ENTER> to continue: "
                    set a = $<
                    exit 1
                endif

#    Now that we have a verified remote hostname and user account, it's time
#    to find a tape drive on the remote host. First, altabletinstall checks for
#    the existence of /dev/tape and /dev/nrtape by `mt stat'ing them. If no
#    errors occur when `mt stat'ing them, altabletinstall asks the installer if
#    they wish to use them for the installation. If they say `no', it asks them
#    for their choice of rewinding tape device. Given the installers choice,
#    altabletinstall verifies that the tape exists and is rewindable (minor
#    number is even). Given the major and minor numbers of the rewindable
#    device, altabletinstall increments the minor number and tries to find the
#    non-rewinding device with that <major, minor> device number.

                echo ""
                echo "Checking for default tape devices on $REMOTE_TAPE_HOST ..."
                set usedflt
                foreach tapedev (/dev/{tape,nrtape})
                    mt -t $ruser@${REMOTE_TAPE_HOST}:$tapedev stat >& $OUT

#    Probe for the existence of the rewinding tape drive (/dev/tape) and
#    non-rewinding tape drive (/dev/nrtape). If a probe fails, explain to the
#    installer the problem (stripping the `<ruser>@' off the error messages to
#    prevent confusion for those not familiar with the <user>@<mach>:/dev/tape
#    syntax).

                    if ($status != 0) then
                        unset usedflt
                        echo ""
                        echo -n "An error has occurred when trying to access the "
                        if ($tapedev == "/dev/nrtape") echo -n "non-"
                        echo "rewinding tape drive"
                        echo "($tapedev) on ${REMOTE_TAPE_HOST}:"
                        echo ""
                        cat $OUT | sed -e "s/$ruser@//" -e "s/^/    /"
                    endif
                    rm -f $OUT
                end
                if ($?usedflt) then
                    echo ""
                    echo "Both default tape devices on $REMOTE_TAPE_HOST (/dev/tape, /dev/nrtape) exist"
                    echo "and are accessible."
                    echo ""
                    echo -n "Do you wish to use them to install the tablet drivers? (yes/no): "
                    while (1)
                        set ans = $<
                        if ("$ans" !~ [yY][eE][sS] && "$ans" !~ [nN][oO]) then
                            echo -n "Please specify yes or no: "
                        else
                            break
                        endif
                    end

#    If the default tape drives on the remote machine exist and are accessible,
#    ask the installer if they wish to install the tablet drivers using them.
#    If they say `yes', set TAPE and NRTAPE to the appropriate names.

                    if ($ans =~ [Yy][Ee][Ss]) then
                        set TAPE = $ruser@${REMOTE_TAPE_HOST}:/dev/tape
                        set NRTAPE = $ruser@${REMOTE_TAPE_HOST}:/dev/nrtape
                    endif
                endif

                if (! $?TAPE) then

#    If the default tape drives do not exist or are not usable (not accessible),
#    ask the installer if they wish to install the drivers from an alternate
#    pair of devices (one rewinding, the other non-rewinding).

                    echo ""
                    echo "In order to install the tablet drivers, you must specify the name of"
                    echo "a rewinding tape device on $REMOTE_TAPE_HOST (i.e.: /dev/rmt/tps0d2)."
                    echo "Altabletinstall will verify that it (and its non-rewinding version)"
                    echo "exist and are accessible before allowing them to be used for the"
                    echo "installation."
                    echo ""
                    while (1)
                        echo "Which tape device do you wish to use? (please specify a full"
                        echo -n "pathname): "
                        set pname = $<
                        set altape = ($pname)
                        if ($#altape == 1) break
                    end

#    Given the installers choice of rewindable tape device, verify:
#
#    - the device exists 
#    - it is a character device (i.e.: tape drive)
#    - its minor device number is even; this indicates a rewindable device
#    - the corresponding non-rewind device exists (same major device number,
#      incremented minor number)
#    - Both devices pass the `mt stat' test with no errors
#
#    If any of these tests fail, altabletinstall informs the installer of the
#    problem and exits; else it continues with the installation.

                    echo ""
                    echo "Validating your choice of tape device ..."
                    (rsh $REMOTE_TAPE_HOST -l $ruser ls -l $altape > $OUT) >& $OERR
                    if ($status != 0) then
                        rm -f $OUT
                        echo ""
                        echo "An error occurred while probing for $altape on ${REMOTE_TAPE_HOST}:"
                        echo ""
                        cat $OERR | sed "s/^/    /"
                        rm -f $OERR
                        echo ""
                        set errmsg1 = "Cannot probe for $altape on $REMOTE_TAPE_HOST"
                        goto baderr
                    endif
                    rm -f $OERR
                    if (-z $OUT) then
                        echo ""
                        echo "The tape device that you specified ($altape) does not exist."
                        echo ""
                        echo "You must specify an existing tape device."
                        set notape
                    else
                        set ftype = `cut -c1 $OUT`
                        if ($ftype == "c") then
                            set maj = `awk -F"," '{print $1}' $OUT | awk '{print $5}'`
                            set min = `awk -F"," '{print $2}' $OUT | awk '{print $1}'`
                            @ rem = $min % 2
                            if ($rem) then
                                echo ""
                                echo "The tape device that you specified ($altape) is not a rewinding"
                                echo "tape device."
                                echo ""
                                echo "You must specify a rewinding tape device, not a non-rewinding tape device."
                                echo ""
                                echo "For example, you should choose /dev/rmt/tps0d2 over /dev/rmt/tps0d2nr, as the"
                                echo 'second device is a non-rewinding device (note the "nr").'
                                set notape
                            else
                                set dpref = $altape:h
                                @ min++
                                if ($min < 10) then
                                    set majmin = "$maj,  $min"
                                else if ($min < 100) then
                                    set majmin = "$maj, $min"
                                else
                                    set majmin = "$maj,$min"
                                endif
                                (rsh $REMOTE_TAPE_HOST -l $ruser ls -l $dpref/. | grep " $majmin " > $OUT) >& $OERR
                                if (! -z $OERR) then
                                    rm -f $OUT
                                    echo ""
                                    echo "An error occurred while probing for the non-rewinding version of $altape on"
                                    echo "${REMOTE_TAPE_HOST}:"
                                    echo ""
                                    cat $OERR | sed "s/^/    /"
                                    rm -f $OERR
                                    echo ""
                                    set errmsg1 = "Cannot probe for non-rewind tape device on $REMOTE_TAPE_HOST."
                                    goto baderr
                                endif
                                rm -f $OERR
                                set nrdev = `awk -F"," '{print $2}' $OUT | awk '{print $5}'`

#    A new tangle. Under IRIX 5, a tape device may have two (or more?) different
#    names that share the same <major,minor> number:
#
#    crw-rw-rw-    1 root     sys      144,224 Jul 10 11:37 /dev/rmt/tps0d7
#    crw-rw-rw-    1 root     sys      144,224 Jul 10 11:37 /dev/rmt/tps0d7s
#    crw-rw-rw-    1 root     sys      144,225 Jul 10 11:37 /dev/rmt/tps0d7nr
#    crw-rw-rw-    1 root     sys      144,225 Jul 10 11:37 /dev/rmt/tps0d7nrs
#
#    and exhibit the same behavior (i.e.: /dev/rmt/tps0d7 and /dev/tps0d7s
#    both access tape device 7 on scsi controller 0 which swaps bytes during
#    reads and writes) and are equivalent, despite the different name. All
#    altabletinstall has to do is use one of the device names (the first one).

                                if ($#nrdev > 1) then
                                    set nrdev = $nrdev[1]
                                endif
                                set alnrtape = "$dpref/$nrdev"
                                foreach tapedev ($altape $alnrtape)
                                    set tdev = $tapedev:t
                                    if ($tdev == $nrdev) then
                                        set fill = "non-"
                                    else
                                        set fill = ""
                                    endif
                                    if ($tdev != "") then
                                        mt -t $ruser@${REMOTE_TAPE_HOST}:$tapedev stat >& $OUT
                                        if ($status != 0) then
                                            set notape
                                            echo ""
                                            echo "The ${fill}rewinding tape device ($tapedev) exists, but an error occurs"
                                            echo "when altabletinstall attempts to access it:"
                                            echo ""
                                            cat $OUT | sed -e "s/$ruser@//" -e "s/^/    /"
                                        endif
                                        rm -f $OUT
                                    else
                                        set notape
                                        echo ""
                                        echo "The ${fill}rewinding version of the tape device that you specified does not exist."
                                    endif
                                end
                                if (! $?notape) then
                                    set TAPE = $ruser@${REMOTE_TAPE_HOST}:$altape
                                    set NRTAPE = $ruser@${REMOTE_TAPE_HOST}:$alnrtape
                                endif
                            endif
                        else
                            echo ""
                            echo "All tape devices are character devices. The tape device that you specified"
                            echo "($altape) is not a character device and cannot be used for the installation."
                            echo ""
                            echo "You must specify a valid tape device."
                            set notape
                        endif
                    endif
                endif
            endif
        endif
        rm -f $OUT $OERR

#    If acceptable tape device(s) were not found, inform the installer and
#    exit.

        if ($?notape) then
            echo ""
            echo "Once you know which tape device you wish to use, restart the altabletinstall"
            echo "procedure."
            echo ""
            echo -n "Press <ENTER> to continue: "
            set a = $<
            exit 1
        endif
    endif
endif


#    If the installer indicates that they do wish to install from CD-ROM or
#    tape, inform them that those are the only methods to install the
#    tablet drivers.

if (! $?mtype) then
    echo ""
    echo "You have indicated that you do not wish to install the tablet drivers from"
    echo "CD-ROM or tape. In order to install the drivers, you must choose one of the"
    echo "two methods."
    echo ""
    echo "Once you have determined the appropriate method, restart the altabletinstall"
    echo "procedure."
    echo ""
    echo -n "Press <ENTER> to continue: "
    set a = $<
    exit 1
endif

echo ""
echo "Installing the tablet drivers from a $mtype drive."

#    Prepare to install the tablet drivers

echo ""
echo "If you are planning to do any peripheral work with the new Alias software"
echo "and tablets, your machine must have the correct tablet drivers installed in"
echo "the IRIX kernel. Before proceeding with the installation, altabletinstall must"
echo "verify that your machine is running an operating system which is compatible"
echo "with the drivers."

#    Verify that the operating system is acceptable:
#
#    The tablet drivers can be installed on any version of IRIX 5 or IRIX 4.
#
#    If the operating system is a variant of IRIX 5, set the required variables
#    to indicate the location of the tablet drivers and system file under
#    IRIX 5.
#
#    If the operating system is a variant of IRIX 4, set the required variables
#    to indicate the location of the tablet drivers and system file under
#    IRIX 4.
#
#    If the operating system is not a variant of IRIX 5 or IRIX 4, inform the
#    installer that they must upgrade the operating system to a version of
#    IRIX 4 or IRIX 5 before the drivers can be installed.

set IRIX5I = 5
set IRIX4I = 4
unset vsuf
set os = `uname -r`
set rfc = `expr $os : '\(.\)'`
if ($rfc == $IRIX5I) then

#    If the machine is running a version of IRIX 5, the tablet master and
#    object files are installed in /var/sysgen and the kernel system file is
#    /var/sysgen/system/gfx.sm.

    set vsuf = irix5
    set pref_tmaster = /var/sysgen/master.d
    set pref_tobj = /var/sysgen/boot
    set system = /var/sysgen/system/gfx.sm

else if ($rfc == $IRIX4I) then

#    If the machine is running a version of IRIX 4, the tablet master and
#    object files are installed in /usr/sysgen and the kernel system file is
#    /usr/sysgen/system.

    set vsuf = irix4
    set pref_tmaster = /usr/sysgen/master.d
    set pref_tobj = /usr/sysgen/boot
    set system = /usr/sysgen/system

else

#    If the machine is not running a version of IRIX 5 or IRIX 4, inform the
#    installer that the tablet drivers cannot be installed because they are
#    not supported under other operating systems.

    echo ""
    echo "The tablet drivers are incompatible with the operating system on this machine."
    echo "The drivers are supported with versions of IRIX 4 and IRIX 5, but your machine"
    echo "is running IRIX $os."
    echo ""
    echo "If you wish to install the tablet drivers, you will have to update the operating"
    echo "system on your machine to a version of IRIX 4 or IRIX 5 (i.e. IRIX 4.0.5F)."

    set errmsg1 = "Tablet drivers are incompatible with operating system."
    goto baderr
endif

if ($?vsuf) then
    echo ""
    echo "The operating system is acceptable."
endif

#    Attempt to install the tablet drivers.
#
#    In this case, tab_masters and tab_objs represent the names of the
#    tablet master and object files, while tab_vdmasters and tab_vdobjs
#    represent the names of the same files but with a version designator
#    attached (i.e.: irix4 or irix5). The latter variables are used
#    to extract the correct versions of the tablet drivers from the
#    distribution media.

set tab_masters = ""
set tab_objs = ""
set tab_vdmasters = ""
set tab_vdobjs = ""
@ i = 1
while ($i <= $#ATABS)
    set tab_masters = ($ATABS[$i] $tab_masters)
    set tab_vdmasters = ($ATABS[$i].$vsuf $tab_vdmasters)
    @ i++
end
@ i = 1
while ($i <= $#ATABS)
    set tab_objs = ($ATABS[$i].o $tab_objs)
    set tab_vdobjs = ($ATABS[$i].o.$vsuf $tab_vdobjs)
    @ i++
end

echo ""
echo "In order to install the tablet drivers, the correct versions of the following"
echo "files must be installed:"
echo ""
@ i = 1
while ($i <= $#tab_objs)
    echo "    $pref_tobj/$tab_objs[$i]"
    @ i++
end
@ i = 1
while ($i <= $#tab_masters)
    echo "    $pref_tmaster/$tab_masters[$i]"
    @ i++
end
echo ""
echo "and the file $system modified to recognize the drivers."
echo ""
echo "The files located in $pref_tobj are the tablet object files and the"
echo "files located in $pref_tmaster are the master or kernel description"
echo "files."
echo ""
echo "If older versions of these files exist, they will be compared with the same"
echo "files from the Alias software $almedia. If they differ, the file(s) from the"
echo "distribution $almedia will replace the existing version(s), which will be saved."
echo "If any of the files from the distribution $almedia are installed or the system"
echo "file is modified, altabletinstall will build a new IRIX kernel which includes"
echo "the tablet drivers."

#    Ask the installer if they wish to install the tablet drivers. If they
#    say `no', the tablet driver files are not installed. 

echo ""
echo -n "Do you wish to install the tablet drivers? (yes/no): "
while (1)
    set ans = $<
    if ($ans !~ [yY][eE][sS] && $ans !~ [nN][oO]) then
        echo -n "Please specify yes or no: "
    else
        break
    endif
end
if ($ans =~ [Nn][Oo]) goto userexit

#    If the tablet driver files are being installed, extract the files from
#    the distribution tape or CD-ROM into a temporary directory.

cd $TMPDIR
while (1)
    if (! $?ALIAS_CDPATH) then
        echo ""
        echo "Positioning tape to extract the tablet driver files from the Alias software"
        echo "tape."
        mt -t $NRTAPE rew
        if ($status != 0) then
            echo ""
            echo "Error rewinding the tape."
            goto tablet_tape_err
        endif
        mt -t $NRTAPE fsf 1
        if ($status != 0) then
            echo ""
            echo "Error advancing the tape to the system support files."
            goto tablet_tape_err
        endif
        echo ""
        echo "Extracting tablet driver files into $TMPDIR."
        echo ""

        tar xvof $NRTAPE $tab_vdmasters $tab_vdobjs
        if ($status == 0) break
    else
        echo ""
        echo "Extracting tablet driver files from the CD-ROM into $TMPDIR."
        echo ""

        tar xvof $ALIAS_CDPATH/$SYSFILES $tab_vdmasters $tab_vdobjs
        if ($status == 0) break
    endif

tablet_tape_err:
    echo ""
    echo "It appears that there was a problem in reading the $almedia. You may have to"
    echo "attempt it 2 or 3 times before you're finally successful."
    echo ""
    echo -n "Do you wish to try reading the $almedia again? (yes/no): "
    while (1)
        set ans = $<
        if ($ans !~ [yY][eE][sS] && $ans !~ [nN][oO]) then
            echo -n "Please specify yes or no: "
        else
            break
        endif
    end
    if ($ans =~ [nN][oO]) then
        rm -f $tab_vdmasters $tab_vdobjs
        goto userexit
    endif
end

#    If the tablet driver files were successfully extracted from the
#    distribution tape or CD-ROM, continue with the installation.
#
#    Verify that the correct versions of the tablet master and object files
#    are installed. Check the master files followed by the object files. The
#    same treatment applies to both sets of files. Foreach file in each
#    group:
#
#    - Check for an existing file in /{usr,var}/sysgen/{master.d,boot}. If the
#      file is found, compare it with the "new" version that was extracted from
#      the distribution media. If the files are the same, remove the version
#      that was extracted from the distribution media.
#    - If the two files are different or the named file does not exist in
#      /{usr,var}/sysgen/{master.d,boot}, the file from the distribution media
#      is moved to the appropriate directory in /{usr,var}/sysgen and renamed
#      by stripping its version designator (irix4 or irix5). If a new file
#      replaces an existing file, the existing file is saved as <filename>= at
#      the end of the script.
#
#    If any files from the distribution media are installed in one of the
#    directories in /{usr,var}/sysgen (boot or master.d), a new IRIX kernel
#    will be built.

unset krebuild
echo ""
echo ""
echo "The script will now verify that your machine has the correct versions of"
echo "the tablet drivers installed."
echo ""
echo "Checking the tablet master files."
cd $pref_tmaster
@ i = 1
while ($i <= $#tab_masters)
    set replacet
    unset old_tmaster
    set fname = $tab_masters[$i]
    if (-f $fname) then
        cmp -s $fname $TMPDIR/$fname.$vsuf
        if ($status == 0) then
            rm $TMPDIR/$fname.$vsuf
            unset replacet
        else
            set old_tmaster
        endif
    endif

    if ($?replacet) then
        if ($?old_tmaster) then
            if (! -f $fname.pre_alias) then
                mv $fname $fname.pre_alias
            endif
            set mod_files = ($mod_files $pref_tmaster/$fname)
        endif
        mv -f $TMPDIR/$fname.$vsuf $fname
        chmod 444 $fname
        chown bin.bin $fname
        set krebuild
    endif
    @ i++
end

echo ""
echo "Now the tablet object files."
echo ""
cd $pref_tobj
@ i = 1
while ($i <= $#tab_objs)
    set replacet
    unset old_tobj
    set fname = $tab_objs[$i]
    if (-f $fname) then
        cmp -s $fname $TMPDIR/$fname.$vsuf
        if ($status == 0) then
            rm $TMPDIR/$fname.$vsuf
            unset replacet
        else
            set old_tobj
        endif
    endif

    if ($?replacet) then
        if ($?old_tobj) then
            if (! -f $fname.pre_alias) then
                mv $fname $fname.pre_alias
            endif
            set mod_files = ($mod_files $pref_tobj/$fname)
        endif
        mv -f $TMPDIR/$fname.$vsuf $fname
        chmod 444 $fname
        chown bin.bin $fname
        set krebuild
    endif
    @ i++
end

#    If the kernel system file (/usr/sysgen/system for IRIX 4,
#    /var/sysgen/system/gfx.sm for IRIX 5) exists, verify that there are
#    entries for the various tablet drivers (aliashitachi aliaskurta aliaswacom
#    altablet) in the "USE: dial ..." line.

echo ""
echo "Verifying that the kernel system file recognizes the tablet drivers."
if (-f $system) then
    if (! -f $system.pre_alias) then
        cp $system $system.pre_alias
    endif
    set mod_files = ($mod_files $system)

    set sline = `grep "^USE: dial " $system`

#    If the pattern `USE: dial ' (start of line which lists tablet modules) is
#    not found in the kernel system file, add the following line to the file:
#
#    USE: aliashitachi aliaskurta aliaswacom altablet
#
#    and indicate that a new IRIX kernel should be built.

    if ("$sline" == "") then
        echo ""
        echo "Modifying the kernel system file to recognize the tablet drivers."
        ex - $system << EOF
        ${DLR}a
USE: $ATABS
.
        w! $system
        q
EOF
        set krebuild
    else

#    If the pattern `USE: dial' (start of line which lists tablet modules) is
#    found in the kernel system file, verify that it has entries for each of
#    the tablet drivers.

        set sysline = ""
        foreach tdriver ($ATABS)
            echo $sline | grep -s " $tdriver " >& /dev/null
            if ($status != 0) then
                echo $sline | grep -s " ${tdriver}${DLR}" >& /dev/null
                if ($status != 0) set sysline = "$sysline $tdriver"
            endif
        end

        if ("$sysline" != "") then
            echo ""
            echo "Modifying the kernel system file to recognize the tablet drivers."

            ex - $system << EOF
            /^USE: dial /c
${sline}${sysline}
.
            w! $system
            q
EOF
            set krebuild
        else
            echo ""
            echo "The kernel system file has been previously modified to recognize the tablet"
            echo "drivers."
        endif
    endif
else
    echo ""
    echo "Cannot find kernel system file ($system:t) in $system:h."
    echo ""
    if ($?krebuild) then
        echo ""
        echo "The tablet drivers have been installed, but a new IRIX kernel cannot be"
        echo "built due to the absence of the kernel system file."
    endif
    unset krebuild
endif

#   If any tablet drivers were added or the kernel system file was modified,
#   build a new IRIX kernel which recognizes the tablet drivers.

unset srb
if ($?krebuild) then
    echo ""
    echo "Building a new IRIX kernel ..."

    cd /
    lboot -u /unix.install >& /dev/null
    if ($status == 0) then
        set srb
        echo ""
        echo "In order to start using the reconfigured operating system, you will have to"
        echo "reboot the machine."
    else
        echo ""
        echo "An error occurred while reconfiguring the operating system. You may have to"
        echo "reboot the machine a few times before the reconfiguration is successful. If the"
        echo 'machine asks you "Automatically reconfigure the operating system (y or n) ?",'
        echo 'make sure you answer "y". When the reconfiguration is successful, the machine'
        echo 'will respond with "Reboot to start using the reconfigured kernel". When the'
        echo "reboot is complete, your machine will be executing the reconfigured kernel."
    endif
else
    if (-f $system) then
        echo ""
        echo ""
        echo "Your machine already has the tablet drivers installed."
    endif
endif

#    In order for the X window system to interpret the tablet data correctly,
#    the correct version of:
#
#        /usr/lib/X11/input/config/tablet
#
#    must be installed.
#
#    If an existing version of the tablet configuration file is found, the
#    configuration file from the ALIAS distribution media is compared with the
#    existing file. If the files are the same, the file from the distribution
#    media is removed. If the files are different, the file from the
#    distribution media replaces the existing file, which is saved.
#
#    There are three versions of the tablet configuration file, one for each of
#    Alias StudioPaint, Alias Eclipse and other Alias products (i.e.: Alias
#    Studio). InstallAlias asks the installer which product they are using and
#    extracts the correct version of the configuration file from the tape or
#    CD-ROM.

echo ""
echo ""
echo "In addition to the installation of the tablet drivers, the X window system must"
echo "be told how to interpret the tablet data. This function is performed by the"
echo "following file:"
echo ""
echo "    $XTABLET"
echo ""
echo "There are three versions of this file, one for each of the following products:"
echo ""
echo "    $NSTPA) Alias StudioPaint"
echo "    $NECL) Alias Eclipse"
echo "    $NDFLT) Other Alias Products (i.e.: Alias Studio)"
echo ""
echo -n "Please indicate which product you are using (1-3): "
while (1)
    set choice = $<
    if ("$choice" !~ [123]) then
        echo -n "Please specify a number between 1 and 3: "
    else
        break
    endif
end

echo ""
echo "This script will now verify that the correct version of the tablet"
echo "configuration file is installed."

#    Based on the type of Alias product being installed, a specific tablet
#    config file will be extracted from the tape or CD-ROM.

if ($choice == $NSTPA) then
    set vers = "Alias StudioPaint"
    set fname = $XTABSTPA
else if ($choice == $NECL) then
    set vers = "Alias Eclipse"
    set fname = $XTABECL
else
    set vers = "Alias"
    set fname = $XTABDFLT
endif

cd $TMPDIR
set xpref = $XTABLET:h

#    If the directory hierarchy /usr/lib/X11/input/config does not exist,
#    create it.

if (! -d $xpref) mkdir -p $xpref

while (1)
    if (! $?ALIAS_CDPATH) then
        echo ""
        echo "Positioning tape to extract the tablet configuration file from the Alias"
        echo "software tape."
        mt -t $NRTAPE rew
        if ($status != 0) then
            echo ""
            echo "Error rewinding the tape."
            goto tablet_tape_err2
        endif
        mt -t $NRTAPE fsf 1
        if ($status != 0) then
            echo ""
            echo "Error advancing the tape to the system support files."
            goto tablet_tape_err2
        endif
        echo ""
        echo "Extracting tablet configuration file into $TMPDIR."
        echo ""
    
        tar xvof $NRTAPE $fname
        if ($status == 0) break
    else
        echo ""
        echo "Extracting tablet configuration file from the CD-ROM into $TMPDIR."
        echo ""
    
        tar xvof $ALIAS_CDPATH/$SYSFILES $fname
        if ($status == 0) break
    endif

tablet_tape_err2:
    echo ""
    echo "It appears that there was a problem in reading the $almedia. You may have to"
    echo "attempt it 2 or 3 times before you're finally successful."
    echo ""
    echo -n "Do you wish to try reading the $almedia again? (yes/no): "
    while (1)
        set ans = $<
        if ($ans !~ [yY][eE][sS] && $ans !~ [nN][oO]) then
            echo -n "Please specify yes or no: "
        else
            break
        endif
    end
    if ($ans =~ [nN][oO]) then
        rm -f $fname
        goto userexit
    endif
end

set replacet
unset old_tconfig
if (-f $XTABLET) then
    cmp -s $XTABLET $TMPDIR/$fname
    if ($status == 0) then
        echo ""
        echo "The correct version of the tablet configuration file is already installed."
        rm $TMPDIR/$fname
        unset replacet
    else
        set old_tconfig
    endif
endif

if ($?replacet) then
    if ($?old_tconfig) then
        if (! -f $XTABLET.pre_alias) then
            mv $XTABLET $XTABLET.pre_alias
        endif
        set mod_files = ($mod_files $XTABLET)
    endif
    echo ""
    echo "Installing new version of the configuration file in $xpref."
    mv -f $TMPDIR/$fname $XTABLET
endif

#    Determine what type of tablet the installer is using:
#
#    Ask the installer to indicate the maker of their tablet (one of Hitachi,
#    Kurta and Wacom). Once they have indicated the maker of their tablet,
#    list all supported tablets manufactured by that maker and ask the installer
#    to indicate the type of tablet they are installing support for.

echo ""
echo "There are a number of different tablet types supported by the $vers"
echo "software. The makers of these tablets are:"
echo ""
echo "    $THIT) Hitachi"
echo "    $TKUR) Kurta"
echo "    $TWAC) Wacom"
echo ""
echo -n "Which type of tablet are you currently using? (1-3): "
while (1)
    set tmake = $<
    if ("$tmake" !~ [123]) then
        echo -n "Please specify a number between 1 and 3: "
    else
        break
    endif
end

if ($tmake == $THIT) then
    set H1111 = 1
    set H1111C = 2
    set H1515 = 3
    set H2222 = 4
    set H2920 = 5
    set H3624 = 6
    set H4030 = 7
    set H4836 = 8
    set H6044 = 9

    echo ""
    echo "The following hitachi tablets are supported by the $vers software:"
    echo ""
    echo -n "    $H1111) "
    echo 'Hitachi 11" by 11" tablet'
    echo -n "    $H1111C) "
    echo 'Hitachi 11" by 11" tablet (HDG-1111C)'
    echo -n "    $H1515) "
    echo 'Hitachi 15" by 15" tablet'
    echo -n "    $H2222) "
    echo 'Hitachi 22" by 22" tablet'
    echo -n "    $H2920) "
    echo 'Hitachi 29" by 20" tablet'
    echo -n "    $H3624) "
    echo 'Hitachi 36" by 24" tablet'
    echo -n "    $H4030) "
    echo 'Hitachi 40" by 30" tablet'
    echo -n "    $H4836) "
    echo 'Hitachi 48" by 36" tablet'
    echo -n "    $H6044) "
    echo 'Hitachi 60" by 44" tablet'
    echo ""
    echo -n "Please indicate which tablet you are currently using (1-9): "
    while (1)
        set tnum = $<
        if ("$tnum" !~ [123456789]) then
            echo -n "Please specify a number between 1 and 9: "
        else
            break
        endif
    end
    if ($tnum == $H1111) then
        set tstr = hitachi1111
    else if ($tnum == $H1111C) then
        set tstr = hitachi1111c
    else if ($tnum == $H1515) then
        set tstr = hitachi1515
    else if ($tnum == $H2222) then
        set tstr = hitachi2222
    else if ($tnum == $H2920) then
        set tstr = hitachi2920
    else if ($tnum == $H3624) then
        set tstr = hitachi3624
    else if ($tnum == $H4030) then
        set tstr = hitachi4030
    else if ($tnum == $H4836) then
        set tstr = hitachi4836
    else if ($tnum == $H6044) then
        set tstr = hitachi6044
    endif
else if ($tmake == $TKUR) then
    set K2418 = 1
    set K2424 = 2
    set K3624 = 3
    set K3630 = 4
    set K4836 = 5
    set K6042 = 6
    set K6648 = 7

    echo ""
    echo "The following kurta tablets are supported by the $vers software:"
    echo ""
    echo -n "    $K2418) "
    echo 'Kurta 24" by 18" tablet'
    echo -n "    $K2424) "
    echo 'Kurta 24" by 24" tablet'
    echo -n "    $K3624) "
    echo 'Kurta 36" by 24" tablet'
    echo -n "    $K3630) "
    echo 'Kurta 36" by 30" tablet"'
    echo -n "    $K4836) "
    echo 'Kurta 48" by 36" tablet'
    echo -n "    $K6042) "
    echo 'Kurta 60" by 42" tablet'
    echo -n "    $K6648) "
    echo 'Kurta 66" by 48" tablet'
    echo ""
    echo -n "Please indicate which tablet you are currently using (1-7): "
    while (1)
        set tnum = $<
        if ("$tnum" !~ [1234567]) then
            echo -n "Please specify a number between 1 and 7: "
        else
            break
        endif
    end
    if ($tnum == $K2418) then
        set tstr = kurta2418
    else if ($tnum == $K2424) then
        set tstr = kurta2424
    else if ($tnum == $K3624) then
        set tstr = kurta3624
    else if ($tnum == $K3630) then
        set tstr = kurta3630
    else if ($tnum == $K4836) then
        set tstr = kurta4836
    else if ($tnum == $K6042) then
        set tstr = kurta6042
    else if ($tnum == $K6648) then
        set tstr = kurta6648
    endif
else
    set W0906 = 1
    set W1212 = 2
    set W1515 = 3
    set W1812 = 4
    set W2518 = 5
    set W3525 = 6
    set W4735 = 7

    echo ""
    echo "The following wacom tablets are supported by the $vers software:"
    echo ""
    echo -n "    $W0906) "
    echo 'Wacom 9" by 6" tablet'
    echo -n "    $W1212) "
    echo 'Wacom 12" by 12" tablet'
    echo -n "    $W1515) "
    echo 'Wacom 15" by 15" tablet'
    echo -n "    $W1812) "
    echo 'Wacom 18" by 12" tablet'
    echo -n "    $W2518) "
    echo 'Wacom 25" by 18" tablet'
    echo -n "    $W3525) "
    echo 'Wacom 35" by 25" tablet'
    echo -n "    $W4735) "
    echo 'Wacom 47" by 35" tablet'
    echo ""
    echo -n "Please indicate which tablet you are currently using (1-7): "
    while (1)
        set tnum = $<
        if ("$tnum" !~ [1234567]) then
            echo -n "Please specify a number between 1 and 7: "
        else
            break
        endif
    end
    if ($tnum == $W0906) then
        set tstr = wacom0906
    else if ($tnum == $W1212) then
        set tstr = wacom1212
    else if ($tnum == $W1515) then
        set tstr = wacom1515
    else if ($tnum == $W1812) then
        set tstr = wacom1812
    else if ($tnum == $W2518) then
        set tstr = wacom2518
    else if ($tnum == $W3525) then
        set tstr = wacom3525
    else if ($tnum == $W4735) then
        set tstr = wacom4735
    endif
endif
set lname = "/dev/input/altablet-$tstr"

#    Determine on which serial port the tablet is connected

echo ""
echo -n "Which serial port is the tablet connected to? (specify the port number): "
while (1)
    unset badpn
    set pname = $<
    set portno = ($pname)

#    If the installer specifies a port identifer, verify that it is numeric.
#    If the identifer is numeric, verify that /dev/ttyd<identifer> exists.
#
#    If the installer does not specify a port identifier or supplies several
#    elements as a port identifer ($#portno != 1) or the port identifier is
#    not numeric, ask the installer to specify a numeric port number.

    if ($#portno == 1) then
        set jnk = `expr $portno : '[0-9]*\(.*\)'`
        if ($#jnk) set badpn
    else
        set badpn
    endif

    if ($?badpn) then
        echo -n "Please specify a serial port number: "
    else
        if (-c /dev/ttyd$portno) then
            break
        else
            echo "There is no serial port $portno listed in /dev."
            echo -n "Please specify an existing serial port number: "
        endif
    endif
end

#    Verify there is no old tablet device file in /dev/input which pertains
#    to the serial port the tablet is connected to:
#
#    Find the major and minor numbers of the serial port the tablet is
#    connected to. Search /dev/input for a device file with the same
#    <major #, minor #> pair. If such a device file exists, inform the
#    installer that the device file is being removed and remove it.

set maj = `ls -l /dev/ttyd$portno | awk -F"," '{print $1}' | awk '{print $5}'`
set min = `ls -l /dev/ttyd$portno | awk -F"," '{print $2}' | awk '{print $1}'`
if ($min < 10) then
    set majmin = "$maj,  $min"
else if ($min < 100) then
    set majmin = "$maj, $min"
else
    set majmin = "$maj,$min"
endif
set otname = `ls -l /dev/input | grep " $majmin " | awk -F"," '{print $2}' | awk '{print $5}'`

if ($#otname) then
    echo ""
    echo "Removing old tablet type hard link ($otname) in /dev/input."
    rm -f /dev/input/$otname
endif

#    Remove all tablet device files in /dev/input. The Alias software can't
#    handle multiple tablets so we have to ensure that only one tablet device
#    exists.

set nonomatch
rm -f /dev/input/altablet-*
unset nonomatch

#    Create a hard link between /dev/ttyd<tablet port number> and
#    /dev/input/altablet-<tablet type>.

echo ""
echo "Creating hard link between serial port $portno and $lname ..."
ln /dev/ttyd$portno $lname

#    After creating the hardlink between the serial port (/dev/ttydX) and
#    /dev/input/altablet-<tablet type>, make sure the device file is readable
#    and writable by everyone.

echo ""
echo "Making the serial port readable and writable by everyone ..."
chmod +rw /dev/ttyd$portno

#    Check if any of the saved system files have been changed. If a file has not
#    been changed, remove the ".pre_alias" file; else rename the ".pre_alias"
#    file to a file ending with '=' and report the renaming [ i.e.: mv
#    /etc/passwd.pre_alias /etc/passwd= ].    

echo ""
echo ""
foreach fname ($mod_files)
    if (-e $fname.pre_alias) then
        cmp -s $fname $fname.pre_alias
        if ($status == 0) then
            rm -f $fname.pre_alias
        else
            mv $fname.pre_alias ${fname}=
            echo "The original version of $fname has been renamed to ${fname:t}=."
        endif
    endif
end

#   Clean up.

if (! $?ALIAS_CDPATH) then
    echo ""
    echo "Rewinding tape ..."
    mt -t $NRTAPE rew
endif

if ($?tfd) then
    rm -f $HOSTS_TMP
endif

#    If the tablet drivers were added to the system, remind the installer
#    about the state of the rebuilt IRIX kernel:
#
#    If the IRIX kernel rebuild was successful, remind the installer to reboot
#    the machine to install the new kernel.
#
#    If the IRIX kernel rebuild was not successful, remind the installer that
#    they may have to reboot the machine a few times before the rebuild
#    succeeds.

if ($?krebuild) then
    echo ""
    echo ""
    if ($?srb) then
        echo "Don't forget to reboot the machine to install the new IRIX kernel (which"
        echo "includes the tablet drivers)."
    else
        echo "Since the IRIX kernel rebuild failed, you may have to reboot the machine"
        echo "a few times before the kernel rebuild is successful. If the system asks"
        echo 'you "Automatically reconfigure the operating system (y or n) ?", make'
        echo "sure you answer yes. When the rebuild is successful, the system will"
        echo "prompt you to reboot the system. Be sure to do so. This will install the"
        echo "new IRIX kernel (which includes the tablet drivers)."
    endif
    echo ""
endif

echo ""
echo "The installation is complete."
echo -n "Press <ENTER> to continue: "
set a = $<

exit 0

userexit:
echo ""
echo "Ok. Installation has been aborted. You can restart the installation"
echo "by running altabletinstall again."
echo ""

baderr:
if ($?errmsg1) then
    echo ""
    echo "There has been an error in the installation procedure ..."
    echo "Please record the error message and contact Alias Assist."
    echo "This situation must be corrected before the installation can continue."
    echo ""
    echo "The error encountered was:"
    echo ""
    echo $errmsg1
    echo ""
endif

if ($?tfd) then
    rm -f $HOSTS_TMP $OUT $OERR
endif

if (! $?ALIAS_CDPATH) then
    if ($?TAPE) then
        echo "Rewinding tape ..."
        mt -t $TAPE rew
    endif
endif
echo -n "Press <ENTER> to continue: "
set a = $<
exit 1
