#!/sbin/sh

set -a
set -u

#
#   This script creates CDE actions for HP Netstations:
#   1) local clients
#   and 
#   2) associated utilites in the 2 associated directories.
#
#   The action are defined in the $HPXT_ACTION_SRC input file.
#   Local client action names are typically "Hpns...."
#   whereas the utility actions do not start with "Hpns.... ".
#

if [ $# -ne 1 ]
then
  echo "usage: $0 actionfile_name"
  exit 1
fi

# This is the "standard" HP Netstation vue action file 
# that normally lives in /usr/vue/types/hpns.vf
#	
HPXT_ACTION_SRC=$1

write_stub ()
{
rm -f $1

cat <<EOF > $1
# This file represents a Common Desktop Environment (CDE) action.  The contents
# of the file do not matter; the action will work even if the file is empty.
# If this file is executable and the name of the file matches an entry in the
# action database (*.dt files), the CDE File Manager will treat this file as an
# action.
#
# For more information on actions, see the CDE manuals.
#
#
echo "Common Desktop Environment (CDE) action.  Invoke using the File Manager"
EOF
chmod 555 $1
}
  

#
# Create CDE dirs if necessary
#

DTDIR=/etc/dt
APPCONFIG=$DTDIR/appconfig
DIRS="$APPCONFIG/icons/C $APPCONFIG/appmanager/C/Netstations $APPCONFIG/types/C"

for DIR in $DIRS
do
  if [ ! -d $DIR ]
  then
    mkdir -p $DIR
  fi
done


HPXT_ACTION_DST=$APPCONFIG/types/C/hpns.dt
STUB_DIR=$APPCONFIG/appmanager/C/Netstations

if [ -f $HPXT_ACTION_SRC ]
then
  
  rm -f                         $HPXT_ACTION_DST
  cp $HPXT_ACTION_SRC 		$HPXT_ACTION_DST
  chmod 555                     $HPXT_ACTION_DST

  #
  # Get a list of local clients and utilities
  #
  # Actions include the string "Hpns" are assumed to be 
  # local clients or utilities
  #
  # The java actions are excluded,
  # mainly because it's a very big chunk of files and in case they
  # are not installed because of this we want to create the actions
  # in the JRE customize script instead
  #
  LCS=`cat $HPXT_ACTION_DST | awk '$1 == "ACTION" {print $2}' \
	| grep Hpns | egrep -v 'java'`
 

  if [ "$LCS" = "" ]
  then
    echo "could not find local client actions in $HPXT_ACTION_SRC"
    exit 1
  fi


  # Create local client action files
  #
  for LC in $LCS
  do
    write_stub $STUB_DIR/$LC
  done

else
  echo "cannot find $HPXT_ACTION_SRC"
  exit 1
fi


#----------------------------------------------------#
# Install action into front panel
#
# The fp file is assumed to originate from the same 
# source directory as the action file
#----------------------------------------------------#
#
FP_ACTIONFILE="`dirname $HPXT_ACTION_SRC`/hpxt.fp"
FP_ACTIONDST="$APPCONFIG/types/C/hpxt.fp"

if [ -f $FP_ACTIONFILE ]
then
  rm -f $FP_ACTIONDST
  #
  # If Video_Play local client is there 
  # then leave it in the front panel
  #
  if [ -x /usr/bin/X11/700X/hpxt.Video_Play ]
  then
    cp $FP_ACTIONFILE $FP_ACTIONDST
  else
    cat $FP_ACTIONFILE | sed '/CONTROL Video_Play/,/^}/d' > $FP_ACTIONDST
  fi

  chmod 555 $FP_ACTIONDST
fi

#---------------------------------------------------------#
# Install links to Netstation icons for 
# the local clients defined in dt/appconfig/types/hpxt.dt
#
#---------------------------------------------------------#

LINKSRC=$APPCONFIG/icons/C
LINKDST=/opt/hpxt/enware2/usr/vue/icons

if [ -d "$LINKDST" -a -d "$LINKSRC" ]
then
  rm -f $LINKSRC/enware.l.pm
  ln -s $LINKDST/Color/enware.l.pm $LINKSRC/enware.l.pm

  rm -f $LINKSRC/enware.m.pm
  ln -s $LINKDST/Color/enware.m.pm $LINKSRC/enware.m.pm

  rm -f $LINKSRC/enware.s.pm
  ln -s $LINKDST/Color/enware.s.pm $LINKSRC/enware.s.pm

  rm -f $LINKSRC/enware.l.pm
  ln -s $LINKDST/Color/enware.l.pm $LINKSRC/enware.l.pm



  rm -f $LINKSRC/enware.l.bm
  ln -s $LINKDST/enware.l.bm $LINKSRC/enware.l.bm

  rm -f $LINKSRC/enware.m.bm
  ln -s $LINKDST/enware.m.bm $LINKSRC/enware.m.bm

  rm -f $LINKSRC/enware.s.bm
  ln -s $LINKDST/enware.s.bm $LINKSRC/enware.s.bm

  rm -f $LINKSRC/enware.t.bm
  ln -s $LINKDST/enware.t.bm $LINKSRC/enware.t.bm
fi

exit 0
