#!/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 MakeDir "/usr/lib/fs/macfs"; then
            :
        else
            echo "$cannot_create_dir_msg    [ /usr/lib/fs/macfs ]" > "$msgs"
            exit 1                 # Error
        fi
        if [ ! -d "/usr/lib/fs/macfs" -o ! -w "/usr/lib/fs/macfs" -o ! -x "/usr/lib/fs/macfs" ]; then
            echo "$cannot_access_dir_msg    [ /usr/lib/fs/macfs ]" > "$msgs"
            exit 1
        fi
    
        if MakeDir "/usr/lib/rmmount"; then
            :
        else
            echo "$cannot_create_dir_msg    [ /usr/lib/rmmount ]" > "$msgs"
            exit 1                 # Error
        fi
        if [ ! -d "/usr/lib/rmmount" -o ! -w "/usr/lib/rmmount" -o ! -x "/usr/lib/rmmount" ]; then
            echo "$cannot_access_dir_msg    [ /usr/lib/rmmount ]" > "$msgs"
            exit 1
        fi
    
        cd "/usr/lib"
        if /bin/cpio -idu < "$package"; then
            :
        else
            echo "$copy_failed_msg" > "$msgs"
            exit 1                 # Error
        fi
        ;;
    verify)
        echo "$verifying_in_msg"
        if [ ! -d "/usr/lib" -o ! -x "/usr/lib" ]; then
            echo "$cannot_access_dir_msg    [ /usr/lib ]" > "$msgs" 
            exit 1
        fi
        IFS='
'
        for line in `/bin/cat "/usr/lib/$file_info"`; do
            checksum=`echo $line | /bin/cut -d' ' -f1`
            file="`echo $line | /bin/cut -c13-`"
            if [ "$checksum" = "ISDIR" ]; then
                if [ ! -d "/usr/lib/$file" ]; then
                    echo "$cannot_locate_dir_msg    [ /usr/lib/$file ]" > "$msgs"
                    exit 1
                fi
            else
                if [ ! -f "/usr/lib/$file" ]; then
                    echo "$cannot_locate_file_msg    [ /usr/lib/$file ]" > "$msgs"
                    exit 1
                fi
                targetsum="`/bin/sum -r "/usr/lib/$file" 2> /dev/null`"
                targetsum=`echo $targetsum | /bin/cut -d' ' -f1`
                if [ "$targetsum" -ne "$checksum" ]; then
                    echo "$checksum_incorrect_msg    [ /usr/lib/$file ]" > "$msgs"
                    exit 1
                fi
            fi
        done
        IFS="$S_IFS"
    
        echo "$finishing_in_msg"
        #
        # Munge the vold files
        #
        cp /etc/rmmount.conf /tmp/rmmount.conf.$$ >&2
        if [ $? -ne 0 ]; then
            echo $no_tmp_space_msg > "$msgs"
            exit 1
        fi
        chmod +w /tmp/rmmount.conf.$$

        if /bin/grep "ident macfs ident_macfs.so floppy" /tmp/rmmount.conf.$$ >/dev/null 2>&1 ; then
            :
        else
        ## Please be very carefull if you edit the following lines!
            if /bin/ed -s /tmp/rmmount.conf.$$ <<PED > /dev/null 2>&1
?^ident?
a
ident macfs ident_macfs.so floppy
.
w
q
PED
            then
                :
            else
                echo "$edit_failed_msg    [ /tmp/rmmount.conf.$$ ]" > "$msgs"
                exit 1
            fi
        fi
        if /bin/grep "action floppy action_macfs.so" /tmp/rmmount.conf.$$ >/dev/null 2>&1 ; then
            :
        else
        ## Please be very carefull if you edit the following lines!
            if /bin/ed -s /tmp/rmmount.conf.$$ <<PED > /dev/null 2>&1
1
/^action/
i
action floppy action_macfs.so
.
w
q
PED
            then
                :
            else
                echo "$edit_failed_msg    [ /tmp/rmmount.conf.$$ ]" > "$msgs"
                exit 1
            fi
        fi
        /bin/cat /tmp/rmmount.conf.$$ > /etc/rmmount.conf
        if [ $? -ne 0 ]; then
            echo $cannot_write_file_msg    [ /etc/rmmount.conf ] > "$msgs"
            rm -f /tmp/rmmount.conf.$$
            exit 1
        fi
        #---------------------------------------------------------
        # Set permissions
        #---------------------------------------------------------
        chmod 444 /etc/rmmount.conf                   >&2
        chown root /etc/rmmount.conf                  >&2
        chmod 555 /usr/lib/fs/macfs/ident_macfs.so.1  >&2
        chown bin /usr/lib/fs/macfs/ident_macfs.so.1  >&2
        chmod 555 /usr/lib/fs/macfs/mount             >&2
        chown bin /usr/lib/fs/macfs/mount             >&2
        chmod 555 /usr/lib/rmmount/action_macfs.so.1  >&2
        chown bin /usr/lib/rmmount/action_macfs.so.1  >&2

        # Restart the vold process
        vold_line="`/bin/ps -e | /bin/grep \"vold\$\"`"
        if [ $? -eq 0 ]; then
            vold_pid="`echo $vold_line | awk '{ print $1 }'`"
            # SIGHUP will cause the vold process to recheck its config files.
            kill -HUP $vold_pid >&2
        else
            if [ -x "/usr/sbin/vold" ]; then
                /usr/sbin/vold &
            fi
        fi
        /bin/rm -f "/usr/lib/$file_info"
        /bin/rm /tmp/rmmount.conf.$$ 
        exit 0
       ;;
    cleanup|remove)
        if [ "$verb" = cleanup ]; then
            echo "$cleaning_up_in_msg"
        else
            echo "$removing_from_msg"
        fi
        if /bin/rm /usr/lib/rmmount/action_macfs.so.1 >&2; then
            :
        else
            echo "$cannot_remove_file_msg    [ /usr/lib/rmmount/action_macfs.so.1 ]" > "$msgs" 
            exit 1
        fi
        if /bin/rm -r /usr/lib/fs/macfs >&2; then
            :
        else
            echo "$cannot_remove_dir_msg    [ /usr/lib/fs/macfs ]" > "$msgs" 
            exit 1
        fi

        #
        # Clean up the vold files
        #
        cp /etc/rmmount.conf /tmp/rmmount.conf.$$ >&2
        if [ $? -ne 0 ]; then
            echo $no_tmp_space_msg > "$msgs"
            exit 1
        fi
        chmod +w /tmp/rmmount.conf.$$

        if /bin/grep "ident macfs ident_macfs.so floppy" /tmp/rmmount.conf.$$ >/dev/null 2>&1 ; then
            if /bin/ed -s /tmp/rmmount.conf.$$ <<PED > /dev/null 2>&1
?ident_macfs.s?
d
1
/action_macfs.so/
d
w
q
PED
            then
                :
            else
                echo "$edit_failed_msg    [ /tmp/rmmount.conf.$$ ]" > "$msgs"
                exit 1
            fi
        fi
        /bin/cat /tmp/rmmount.conf.$$ > /etc/rmmount.conf
        if [ $? -ne 0 ]; then
            echo $cannot_write_file_msg    [ /etc/rmmount.conf ] > "$msgs"
            /bin/rm -f /tmp/rmmount.conf.$$
            exit 1
        fi

        # Restart the vold process
        vold_line="`/bin/ps -e | /bin/grep \"vold\$\"`"
        if [ $? -eq 0 ]; then
            vold_pid="`echo $vold_line | awk '{ print $1 }'`"
            # SIGHUP will cause the vold process to recheck its config files.
            kill -HUP $vold_pid >&2
        fi
        /bin/rm /tmp/rmmount.conf.$$ 
        exit 0
        ;;
esac
