#!/bin/sh

# genpkgfiles
# - generate a driver .info file and copy over any package scripts
#

[ "$1" = "" -o "$2" = "" -o "$3" = "" ] && {
    echo "Usage: `basename $0` <driver project name> <driver source root> <package output dir>"
    exit 1
}

DRIVER_PROJECT_NAME=$1
INPUT_ROOT=$2
OUTPUT_ROOT=$3
DEFAULT_TABLE=$INPUT_ROOT/Default.table
DRIVER_INFO=$INPUT_ROOT/DriverInfo
PROG_NAME=`basename $0`

INFO_FILE=${OUTPUT_ROOT}/${DRIVER_PROJECT_NAME}.info

[ -d "$INPUT_ROOT" ] || {
    echo "$PROG_NAME: driver input root directory '$INPUT_ROOT' does not exist"
    exit 1
}

[ -d "$OUTPUT_ROOT" ] || {
    echo "$PROG_NAME: driver output root directory '$OUTPUT_ROOT' does not exist"
    exit 1
}

[ -f "$DEFAULT_TABLE" ] || {
    echo "$PROG_NAME: driver default table '$DEFAULT_TABLE' does not exist"
    exit 1
}

[ -f "$DRIVER_INFO" ] || {
    echo "$PROG_NAME: $DRIVER_INFO does not exist"
    exit 1
}

. $DRIVER_INFO # source it in

DRIVER_VERSION=`sed -n 's/.*"Version"[       ]*=[    ]*"\([0-9].[0-9][0-9]\)".*/\1/p' $DEFAULT_TABLE`

if [ "$DRIVER_VERSION" = "" ]
then
    echo "$PROG_NAME: '$DEFAULT_TABLE' contains either a bad version string or none at all"
    exit 1
fi

NEXTSTEP_VERSION=`echo $DRIVER_VERSION | sed 's/.$//'`


# create driver long names
DRIVER_LONG_NAMES=""
DRIVER_SHORT_NAMES=""
for i in ${INPUT_ROOT}/English.lproj/*.strings
do
    [ ! "$DRIVER_LONG_NAMES" = "" ] && {
	DRIVER_LONG_NAMES="${DRIVER_LONG_NAMES}, "
    }
    DRIVER_LONG_NAMES=${DRIVER_LONG_NAMES}`sed -n 's/.*"Long Name"[       ]*=[    ]*"\([^"]*\)";/\1/p' $i`
    [ ! "$DRIVER_SHORT_NAMES" = "" ] && {
	DRIVER_SHORT_NAMES="${DRIVER_SHORT_NAMES}, "
    }
    DRIVER_SHORT_NAMES=${DRIVER_SHORT_NAMES}`sed -n "s/\"${DRIVER_PROJECT_NAME}\"[       ]*=[    ]*\"\([^\"]*\)\";/\1/p" $i`
done

DATE=`date`

echo "$PROG_NAME: generating '$INFO_FILE'"
sed "s/<<DRIVER_VERSION>>/$DRIVER_VERSION/g; s/<<NEXTSTEP_VERSION>>/$NEXTSTEP_VERSION/g; s/<<DRIVER_PROJECT_NAME>>/$DRIVER_PROJECT_NAME/g; s|<<DRIVER_LONG_NAMES>>|$DRIVER_LONG_NAMES|g; s|<<DRIVER_SHORT_NAMES>>|$DRIVER_SHORT_NAMES|g; s|<<DRIVER_NAME>>|$DRIVER_NAME|g; s|<<DATE>>|$DATE|g;" > $INFO_FILE <<EOT
#
# <<DRIVER_PROJECT_NAME>>.info:
# - auto-generated on <<DATE>>
# - driver supports <<DRIVER_SHORT_NAMES>>
#

# These fields will be displayed in the Info View
Title		<<DRIVER_NAME>>
Version		NEXTSTEP Release <<NEXTSTEP_VERSION>>, Driver Version <<DRIVER_VERSION>>
Description	This package contains a driver bundle supporting the following devices: <<DRIVER_LONG_NAMES>>.  After installing, use Configure.app to add and configure the device to work with NEXTSTEP.


# These fields are used for the installed and floppy locations
DefaultLocation		/
Relocatable		NO
Diskname		<<DRIVER_PROJECT_NAME>> #%d

# Other fields
Application		NO
LibrarySubdirectory	Standard
InstallOnly 		YES
DisableStop		YES
LongFilenames		YES
EOT

#
# install the package scripts into the package area
#
for i in pre_install post_install pre_delete post_delete
do
    scriptname=$DRIVER_PROJECT_NAME.$i
    if [ -f $INPUT_ROOT/$scriptname ] 
    then
	echo "$PROG_NAME: copying '$INPUT_ROOT/$scriptname' to '$OUTPUT_ROOT/$scriptname'"
	cp $INPUT_ROOT/$scriptname $OUTPUT_ROOT/$scriptname
	chmod 755 $OUTPUT_ROOT/$scriptname
    fi
done

exit 0
