#!/bin/sh

#------------------------------------------------------------------------------
# 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
#------------------------------------------------------------------------------
prog="$0"                                         # Name of this program
dest="$1"                                         # Target dir for software package
msgs="$2"                                         # Name of message passing file
verb="$3"                                         # Verb (preflight, destination)
launch_dir="`/bin/dirname \"$prog\"`"             # Directory where installer program lives
name="`/bin/expr \"$prog\" : '\(.*\)\.VAL'`"      # Name of package (including path)
pkg_name="`/bin/basename \"$name\"`"              # Name of package (no path)
S_IFS="$IFS"                                      # Save "good" copy of IFS

#------------------------------------------------------------------------------
# Find the correct strings file
#------------------------------------------------------------------------------
catalog_dir="${LC_ALL:-${LC_MESSAGES:-${LANG:-C}}}"
eval catalog_dir=\"`echo $catalog_dir | 
    /usr/bin/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

case $verb in
    preflight)     
        if [ ! -r "$launch_dir/${pkg_name}.LST" ]; then
            echo "$package_file_missing_msg    [ $launch_dir/${pkg_name}.LST ]" > "$msgs"
            exit 1
        fi
        exit 0
        ;;
    ask_dest)
        exit 0                                    # Ask user for destination
        ;;
    ask_rem)
        exit 0                                    # Ask if they want to remove pkg
        ;;
    mk_alias)
        exit 0                                    # Create alias in home directory
        ;;
    destination) 
        if [ -d "$dest" -a ! -w "$dest" ]; then
            echo "$cannot_access_dir_msg    [ $dest ]" > "$msgs"
            exit 1
        fi
        if [ ! -r "$launch_dir/${pkg_name}.LST" ]; then
            echo "$package_file_missing_msg    [ $launch_dir/${pkg_name}.LST ]" > "$msgs"
            exit 1
        fi

        if [ -d "$dest" -a -x "$dest" ]; then
            IFS='
'
            cd "$dest"
            for file in `/bin/cat "$launch_dir/${pkg_name}.LST"`; do
                if [ -d "$file" -o -f "$file" ]; then
                    if [ ! -w "$file" ]; then
                        echo "$no_write_privs_msg    [ $dest/$file ]" > "$msgs"
                        exit 2                        # Exit 2
                    fi
                fi
            done
            IFS="$S_IFS"
        fi

        exit 0                                    # Destination is fine
        ;;
esac
