#!/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)
        if MakeDir "$dest"; then
            :
        else
            echo "$cannot_create_dir_msg    [ $dest ]" > "$msgs"
            exit 1                                # Error
        fi
        if [ ! -d "$dest" -o ! -w "$dest" -o ! -x "$dest" ]; then
            echo "$cannot_access_dir_msg    [ $dest ]" > "$msgs"
            exit 1                                # Error
        fi

        cd "$dest"
        # If MAN package is already installed, update it. Don't overwrite.
        if [ -f "mae.1" ]; then 
            echo "$updating_in_msg"
            if [ -r "${pkg_name}.SAV" ]; then  # Previous update attempt failed
                echo "    A previous update attempt failed."
                if [ -f inst_lock ]; then    # Backup never completed
                    echo "$cleaning_up_in_msg"
                    /bin/rm -f inst_lock     # Clean up an try again
                else
                    echo "$restoring_old_files_in_msg"
                    /bin/cpio -idum < "${pkg_name}.SAV" # Restore backup and try again.
                fi
                /bin/rm -f "${pkg_name}.SAV"
            fi

            # Save all files before attempting install
            echo "$backing_up_to_msg"
            IFS='
'
            > inst_lock                           # Will indicate that SAV in progress
            /bin/cpio -o < "$launch_dir/${pkg_name}.LST" > "${pkg_name}.SAV" 2> /dev/null
            /bin/rm -f inst_lock                  # We're done
            IFS="$S_IFS"
            if [ $? -ne 0 ]; then
                echo "    Error during file backup" > "$msgs"
                exit 1             # Error
            fi

            # Remove the old files (ones just backed up)
            echo "$removing_old_files_msg"
            IFS='
'
            for file in `/bin/cat "$launch_dir/${pkg_name}.LST"`; do
                if [ -f "$file" ]; then
                    /bin/rm -f "$file" 2> /dev/null
                elif [ -d "$file" ]; then
                    /bin/rmdir "$file" 2> /dev/null
                fi
            done
            IFS="$S_IFS"
            echo "$installing_new_files_in_msg"
        else
            echo "$installing_in_msg"
        fi

        # Install the files for this package
        if /bin/cpio -idu < "$package"; then
            exit 0
        else
            echo "$copy_failed_msg" > "$msgs"
            exit 1                                # Error
        fi
        ;;
    verify)
        echo "$verifying_in_msg"
        if [ ! -d "$dest" -o ! -x "$dest" ]; then
            echo "$cannot_access_dir_msg    [ $dest ]" > "$msgs"
            exit 1                                # Error
        fi
        if [ ! -r "$dest/$file_info" ]; then
            echo "$file_missing_msg    [ $dest/$file_info ]" > "$msgs"
            exit 1                                # Error
        fi

        cd "$dest"
        IFS='
'
        for line in `/bin/cat "$file_info"`; do
            checksum=`echo $line | cut -d' ' -f1`
            file="`echo $line | cut -c13-`"
            if [ "$checksum" = "ISDIR" ]; then
                if [ ! -d "$file" ]; then
                    echo "$cannot_locate_dir_msg    [ $dest/$file ]" > "$msgs"
                    exit 1
                fi
            else
                if [ ! -f "$file" ]; then
                    echo "$cannot_locate_file_msg    [ $dest/$file ]" > "$msgs"
                    exit 1
                fi
                targetsum="`/bin/sum -r "$file" 2> /dev/null`"
                targetsum=`echo $targetsum | cut -d' ' -f1`
                if [ "$targetsum" -ne "$checksum" ]; then
                    echo "$checksum_incorrect_msg    [ $dest/$file ]" > "$msgs"
                    exit 1
                fi
            fi
        done
        IFS="$S_IFS"

        # Finish
        echo "$finishing_in_msg"
        /bin/rm -f "$dest/${pkg_name}.SAV"  # Okay to remove now
        /bin/rm -f "$dest/$file_info"

        exit 0
       ;;
    cleanup)
        echo "$cleaning_up_in_msg"
        if [ ! -d "$dest" -o ! -x "$dest" ]; then
            echo "$cannot_access_dir_msg    [ $dest ]" > "$msgs"
            exit 1                                # Error
        fi
        if [ ! -r "$launch_dir/${pkg_name}.LST" ]; then
            echo "$file_missing_msg    [ $launch_dir/${pkg_name}.LST ]" > "$msgs"
            exit 1                                # Error
        fi

        cd "$dest"
        if [ -r "$file_info" ]; then              # First file extracted
            IFS='
'
            for file in `/bin/cat "$launch_dir/${pkg_name}.LST"`; do
                if [ -f "$file" ]; then
                    /bin/rm -f "$file" >&2
                elif [ -d "$file" ]; then
                    /bin/rmdir "$file" >&2
                fi
            done
            IFS="$S_IFS"
            /bin/rm -f "$file_info"
        fi

        if [ -f inst_lock ]; then                 # Incomplete backup of files
            /bin/rm -f "${pkg_name}.SAV"
            /bin/rm -f inst_lock
        fi

        if [ -r "${pkg_name}.SAV" ]; then
            echo "$restoring_old_files_in_msg"
            /bin/cpio -idum < "${pkg_name}.SAV"
            if [ $? -ne 0 ]; then
                echo "    Could not restore original files" > "$msgs" 
                exit 1                 # Error
            fi
            /bin/rm -f "${pkg_name}.SAV"
        fi

        exit 0
       ;;
    remove)
        echo "$removing_from_msg"
        if [ ! -d "$dest" -o ! -x "$dest" ]; then
            echo "$cannot_access_dir_msg    [ $dest ]" > "$msgs"
            exit 1                                # Error
        fi
        if [ ! -r "$launch_dir/${pkg_name}.LST" ]; then
            echo "$file_missing_msg    [ $launch_dir/${pkg_name}.LST ]" > "$msgs"
            exit 1                                # Error
        fi

        exit_status=0
        _rem_file_cnt=0
        _not_rem_file_cnt=0
        _missing_file_cnt=0

        cd "$dest"
        IFS='
'
        for file in `/bin/cat "$launch_dir/${pkg_name}.LST"`; do
            if [ -f "$file" ]; then
                /bin/rm -f "$file" >&2
                if [ -f "$file" ]; then
                    _not_rem_file_cnt=`expr $_not_rem_file_cnt + 1`
                    exit_status=1
                else
                    _rem_file_cnt=`expr $_rem_file_cnt + 1`
                fi
            elif [ -d "$file" ]; then
                /bin/rmdir "$file" >&2
            else
                _missing_file_cnt=`expr $_missing_file_cnt + 1`
            fi
        done
        echo "        $_rem_file_cnt    $files_removed_msg"
        if [ "$_not_rem_file_cnt" -gt 0 ]; then
            echo "        $_not_rem_file_cnt    $files_not_removed_msg"
        fi
        if [ "$_missing_file_cnt" -gt 0 ]; then
            echo "        $_missing_file_cnt    $files_missing_msg"
        fi

        exit $exit_status
        ;;
esac
