#!/bin/sh

set -a
set -u

#
#   This script creates CDE actions for HP X station:
#   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 "Hpxt...."
#   whereas the utility actions do not start with "Hpxt.... ".
#

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

# This is the "standard" ENWARE vue action file 
# that normally lives in /usr/vue/types/hpxt.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
STUB_DIR_UT=$APPCONFIG/appmanager/C/X_Terminals/Utilities
STUB_DIR_LC=$APPCONFIG/appmanager/C/X_Terminals/Local_Clients

DIRS="$APPCONFIG/icons/C $STUB_DIR_UT $STUB_DIR_LC $APPCONFIG/types/C"
for DIR in $DIRS
do
  if [ ! -d $DIR ]
  then
    mkdir -p $DIR
  fi
done


HPXT_ACTION_DST=$APPCONFIG/types/C/hpxt.dt

if [ -f $HPXT_ACTION_SRC ]
then
  rm -f $HPXT_ACTION_DST

  #
  # Re-use VUE action file but modify incompatible parts (vueaction/timeout)
  # (insert braces, replace L-ICON and S-ICON with ICON)
  #
  #cat $HPXT_ACTION_SRC \
  #  | sed 's!/usr/vue/bin/vueaction!/usr/dt/bin/dtaction!g'  \
  #  | sed 's!-timeout [0-9]!!g'  \
  #  | sed '/L-ICON.*enware/d' \
  #  | sed '/S-ICON/s/enware.s/enware/g' | sed '/S-ICON/s/S-ICON/ICON/g' \
  #  | awk '{if ($1 == "ACTION") {printf ("%s\n{\n\tLABEL\t\t%s\n",$0,$2)} \
  #                         else {print $0}}' \
  #  | sed -e '/^END/s/END/}/1' -e 's/NO-STDIO/NO_STDIO/1' \
  #  | sed -e 's/EXEC-STRING/EXEC_STRING/1' -e 's/WINDOW-TYPE/WINDOW_TYPE/1' \
  #  | sed -e 's/PERM-TERMINAL/PERM_TERMINAL/1' \
  #  > $HPXT_ACTION_DST
  cp $HPXT_ACTION_SRC 		$HPXT_ACTION_DST
  chmod 555                     $HPXT_ACTION_DST

  #
  # Get a list of local clients and a list of utilities
  # as they are defined in the action file 
  # Local client action names begin with an "Hpxt" except "HpxtAdmin"
  # which is a utility regardless.
  #
  EXCPT1="HpxtAdmin"
  EXCPT2="SelectVUEwm"
  EXCPT3="XtAdm"
  LCS=`cat $HPXT_ACTION_DST | awk '$1 == "ACTION" {print $2}' \
	| grep Hpxt | grep -v $EXCPT1`
 

  UTS=`cat $HPXT_ACTION_DST | awk '$1 == "ACTION" {print $2}' \
	| grep -v Hpxt | grep -v $EXCPT2 | grep -v $EXCPT3`
  UTS="$UTS $EXCPT1"

  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/$LC
  done


  # Create utility action files
  #
  for UT in $UTS
  do
    write_stub $STUB_DIR_UT/$UT
  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
  cp $FP_ACTIONFILE $FP_ACTIONDST
  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/enware/usr/vue/icons

if [ ! -d "$LINKDST" ]
then
  LINKDST=/usr/vue/icons
fi

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.l.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.t.pm
  ln -s $LINKDST/Color/enware.s.pm $LINKSRC/enware.t.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.l.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.s.bm $LINKSRC/enware.t.bm
fi


exit 0
