#!/bin/sh 

PATH=/bin:/usr/bin:/usr/ucb:/etc:/usr/etc:/usr/5bin:.
S_IFS="$IFS"
umask 0022

#------------------------------------------------------------------------------
# Make sure that we were called with the correct args
#------------------------------------------------------------------------------
if [ $# -ne 3 ]; then
    echo > /dev/tty
    echo "    ASSERT ERROR:" > /dev/tty
    echo "    Incorrect number of args passed to `basename \"$0\"`" > /dev/tty
    echo >/dev/tty
    exit 1
fi

#------------------------------------------------------------------------------
# Initialize globals
#------------------------------------------------------------------------------
file_info='.file_info'                            # Checksum and size info file
prog="$0"                                         # Name of this program
dest="$1"                                         # Target dir for software package
msgs="$2"                                         # Name of message passing file
verb="$3"                                         # Verb (install, verify, remove)
launch_dir="`/bin/dirname \"$prog\"`"             # Directory where installer program lives
name="`/bin/expr \"$prog\" : '\(.*\)\.INS'`"      # Name of package (including path)
package="${name}.PKG"                             # Full path to package file
pkg_name="`/bin/basename \"$name\"`"              # Name of package (no path)
id="`/usr/bin/id`"
uid="`/bin/expr  \"$id\" : 'uid=\(.*\)(.*) gid=.*''`"
gid="`/bin/expr  \"$id\" : 'uid=.* gid=\(.*\)(.*)''`"

#------------------------------------------------------------------------------
# Find the correct strings file
#------------------------------------------------------------------------------
catalog_dir="${LC_ALL:-${LC_MESSAGES:-${LANG:-C}}}"
eval catalog_dir=\"`echo $catalog_dir | tr "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ"`\"

if [ ! -r "${launch_dir}/LOCALE/${catalog_dir}/${pkg_name}.STR" ]; then
    echo "Cannot read strings in  '${launch_dir}/LOCALE/${catalog_dir}'" > "$msgs"
    catalog_dir="C"                     # Try finding English strings
    if [ ! -r "${launch_dir}/LOCALE/${catalog_dir}/${pkg_name}.STR" ]; then
        echo "Cannot read strings in  '${launch_dir}/LOCALE/${catalog_dir}'" >> "$msgs"
        exit 1
    fi
fi

. "${launch_dir}/LOCALE/${catalog_dir}/${pkg_name}.STR"  # Load localized strings

#------------------------------------------------------------------------------
# MakeDir - Make directory(s) from supplied path - always absolute path
#------------------------------------------------------------------------------
MakeDir()
{
    _mk_dir_target="${1:-/}"

    if [ -d "$_mk_dir_target" ]; then             # Already exists
        return 0              
    fi

    /bin/mkdir -p "$_mk_dir_target" >&2
    if [ $? -ne 0 ]; then
        return 1                                  # Return Error
    fi

    return 0                                      # Return Success
}


case "$verb" in
    install)
        echo "$installing_in_msg" 
        if [ ! -f /etc/devlink.tab -o ! -w /etc/devlink.tab ]; then
            echo "$cannot_write_file_msg    [ /etc/devlink.tab ]" > "$msgs"
            exit 1
        elif [ ! -d /usr/kernel/drv -o ! -w /usr/kernel/drv ]; then
            echo "$cannot_access_dir_msg    [ /usr/kernel/drv ]" > "$msgs"
            exit 1
        elif [ ! -d /kernel/strmod -o ! -w /kernel/strmod ]; then
            echo "$cannot_access_dir_msg    [ /kernel/strmod ]" > "$msgs"
            exit 1
        fi
    
        cd /usr/kernel/drv
        if /bin/cpio -idu $file_info atalk atalk.conf < "$package"; then
            :
        else
            echo "$copy_failed_msg" > "$msgs"
            exit 1                 # Error
        fi
    
        cd /kernel/strmod
        if /bin/cpio -idu -f atalk atalk.conf < "$package"; then
            :
        else
            echo "$copy_failed_msg" > "$msgs"
            exit 1                 # Error
        fi
        ;;
    verify)
        echo "$verifying_in_msg"
        cd /usr/kernel/drv
        IFS='
'
        for line in `/bin/grep atalk "$file_info"`; do    # atalk, atalk.conf
            checksum=`echo $line | /bin/cut -d' ' -f1`
            file="`echo $line | /bin/cut -c13-`"
            if [ ! -f "$file" ]; then
                echo "$cannot_locate_file_msg    [ /usr/kernel/drv/$file ]" > "$msgs"
                exit 1
            fi
            targetsum="`/bin/sum -r "$file" 2> /dev/null`"
            targetsum=`echo $targetsum | /bin/cut -d' ' -f1`
            if [ "$targetsum" -ne "$checksum" ]; then
                echo "$checksum_incorrect_msg    [ /usr/kernel/drv/$file ]" > "$msgs"
                exit 1
            fi

            /bin/chown root "$file" >&2 
            /bin/chgrp sys "$file" >&2 
        done
        IFS="$S_IFS"

        /bin/chmod 755 "/usr/kernel/drv/atalk" >&2 
        /bin/chmod 644 "/usr/kernel/drv/atalk.conf" >&2 
        /bin/rm -f /usr/kernel/drv/$file_info

        IFS='
'
        cd /kernel/strmod
        for line in `/bin/grep -v atalk "$file_info"`; do   # adsp, at_atp, at_sig
            checksum=`echo $line | /bin/cut -d' ' -f1`
            file="`echo $line | /bin/cut -c13-`"
            if [ ! -f "$file" ]; then
                echo "$cannot_locate_file_msg    [ /kernel/strmod/$file ]" > "$msgs"
                exit 1
            fi
            targetsum="`/bin/sum -r "$file" 2> /dev/null`"
            targetsum=`echo $targetsum | /bin/cut -d' ' -f1`
            if [ "$targetsum" -ne "$checksum" ]; then
                echo "$checksum_incorrect_msg    [ /kernel/strmod/$file ]" > "$msgs"
                exit 1
            fi
        
            /bin/chown root "$file" >&2 
            /bin/chgrp sys "$file" >&2 
            /bin/chmod 755 "$file" >&2 
        done
        IFS="$S_IFS"

        /bin/rm -f /kernel/strmod/$file_info

        echo "$finishing_in_msg"
        ##
        ## Edit the devlink file
        ##
        if /bin/grep '##MAE_AppleTalk_begin##' /etc/devlink.tab >/dev/null 2>&1 ; then
            :    # The file has already been edited; 
        else
            /bin/cp /etc/devlink.tab /etc/devlink.tab.BAK >&2
            /bin/cp /etc/devlink.tab /tmp/devlink.tab.$$ >&2
            if [ $? -ne 0 ]; then
                echo $no_tmp_space_msg > "$msgs"
                exit 1
            fi
            /bin/chmod +w /tmp/devlink.tab.$$
    
            if /bin/ed -s /tmp/devlink.tab.$$ <<PED > /dev/null 2>&1 
$
a
##MAE_AppleTalk_begin##
type=ddi_pseudo;name=atalk;minor=elap	appletalk/lap/ethertalk0/control
type=ddi_pseudo;name=atalk;minor=ddp	appletalk/ddp/socket
##MAE_AppleTalk_end##
.
w
q
PED
            then
                :
            else
                echo "$edit_failed_msg    [ /tmp/devlink.tab.$$ ]" > "$msgs"
                exit 1
            fi

            /bin/cp /tmp/devlink.tab.$$ /etc/devlink.tab >&2
            if [ $? -ne 0 ]; then
                echo $cannot_write_file_msg    [ /etc/devlink.tab ] > "$msgs"
                if [ -f /etc/devlink.tab.BAK ]; then
                    /bin/mv /etc/devlink.tab.BAK /etc/devlink.tab >&2
                fi
                exit 1
            fi
        fi

        ##
        ## Dynamically add the drivers to the running system
        ##
        /usr/sbin/rem_drv atalk > /dev/null 2>&1     # Blindly remove old driver

        if /usr/sbin/add_drv -m '* 0666 root sys' atalk; then
            :
        else
            echo "
    ERROR: Could not dynamically add AppleTalk to the system."
            exit 1
        fi

        ##
        ## Automatically create the device special files
        ##
        if /usr/sbin/devlinks ; then
            :
        else
            echo "
    ERROR: Could not create the AppleTalk device files."
            exit 1
        fi

        ##
        ## Change permissions on device special files
        ##
        if [ -c "/devices/pseudo/clone@0:le" ]; then
            /bin/chmod 666 "/devices/pseudo/clone@0:le"
            if [ $? -ne 0 ]; then
                echo "
    ERROR: Could not change permissions on /devices/pseudo/clone@0:le"
                exit 1
            fi
        else
            echo "
    ERROR: Could not locate device file /devices/pseudo/clone@0:le"
            exit 1
        fi

        ##
        ## Tell them what to do next
        ##
        echo "
    WARNING: You must REBOOT THIS WORKSTATION before the AppleTalk
    drivers will be fully installed.
    "
        echo "
    WARNING: You must REBOOT THIS WORKSTATION before the AppleTalk
    drivers will be fully installed.
    "  >&2

        /bin/rm -f /tmp/devlink.tab.$$ 
        exit 0
       ;;
    cleanup|remove)
        if [ "$verb" = cleanup ]; then
            echo "$cleaning_up_in_msg"
        else
            echo "$removing_from_msg"
        fi

        /bin/rm -f /usr/kernel/drv/atalk
        /bin/rm -f /usr/kernel/drv/atalk.conf
        /bin/rm -f /kernel/strmod/adsp
        /bin/rm -f /kernel/strmod/at_atp
        /bin/rm -f /kernel/strmod/at_sig
        
        ##
        ## Edit the devlink file
        ##
        if /bin/grep '##MAE_AppleTalk_begin##' /etc/devlink.tab >/dev/null 2>&1 ; then
            /bin/cp /etc/devlink.tab /etc/devlink.tab.BAK >&2
            /bin/cp /etc/devlink.tab /tmp/devlink.tab.$$ >&2
            if [ $? -ne 0 ]; then
                echo "
    ERROR: Remove FAILED" > "$msgs"
                exit 1
            fi
            /bin/chmod +w /tmp/devlink.tab.$$
    
            if /bin/ed -s /tmp/devlink.tab.$$ <<PED > /dev/null 2>&1 
1
/^##MAE_AppleTalk_begin##$/
ka
/^##MAE_AppleTalk_end##$/
'a,.d
w
q
PED
            then
                :
            else
                echo "
    ERROR: Remove FAILED" > "$msgs"
                exit 1
            fi

            /bin/cp /tmp/devlink.tab.$$ /etc/devlink.tab >&2
            if [ $? -ne 0 ]; then
                echo "
    ERROR: Remove FAILED" > "$msgs"
                if [ -f /etc/devlink.tab.BAK ]; then
                    /bin/mv /etc/devlink.tab.BAK /etc/devlink.tab >&2
                fi
                exit 1
            fi
        fi

        ##
        ## Dynamically remove the drivers from the running system
        ##
        if /usr/sbin/modinfo | grep atalk | grep MAE > /dev/null 2>&1 ; then
            if /usr/sbin/rem_drv atalk; then
                :
            else
                echo "
    ERROR: Could not dynamically remove AppleTalk drivers." > "$msgs"
                exit 1
            fi
        fi

        ##
        ## Automatically create the device special files
        ##
        if /usr/sbin/devlinks ; then
            :
        else
            echo "
    ERROR: Could not remove the AppleTalk device files." > "$msgs"
            exit 1
        fi

        ##
        ## Tell them what to do next
        ##
        echo "
    WARNING: You must REBOOT THIS WORKSTATION before the AppleTalk
    drivers will be fully removed.
    "
        echo "
    WARNING: You must REBOOT THIS WORKSTATION before the AppleTalk
    drivers will be fully removed.
    "  >&2

        /bin/rm -f /tmp/devlink.tab.$$ 
        exit 0
        ;;
esac
