#!/sbin/sh

#---------------------------------------------------------------------------#
# Merge app-defaults directories for ENWARE and Netstation local clients.
#
# 1) copy app-defaults files from ENWARE into Netstation app-defaults directory
# 2) link ENWARE app-defaults directory to Netstation app-defaults directory
#
# 3) Undo changes in 1) and 2)      (called during deinstall of Netstation SW
#---------------------------------------------------------------------------#

set -a

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

echo  $1 | egrep -i '(cre)|(add)' > /dev/null 
if [ $? -eq 0 ]
then
  FUNC="create"
else
  echo  $1 | egrep -i '(res)|(del)|(rm)|(rem)' > /dev/null 
  if [ $? -eq 0 ]
  then
    FUNC="restore"
  else
    echo "usage: $0 create | restore"
    exit 1
  fi
fi


OLDAPPDEF=/usr/lib/X11/700X/lib/app-defaults
NEWAPPDEF=/opt/hpxt/enware2/xthome/lib/app-defaults

if [ -d "$OLDAPPDEF" -a -d "$NEWAPPDEF" -a $FUNC = "create" ]
then
  if [ ! -h "$OLDAPPDEF" ]
  then
    echo "INFO:    Linking $OLDAPPDEF to $NEWAPPDEF"
    (  
      cd $OLDAPPDEF
      FILES=`ls`
      for FILE in $FILES
      do
        if [ ! -f "$NEWAPPDEF/$FILE" ]
        then
          cp $FILE $NEWAPPDEF
        fi
      done
    )
    mv $OLDAPPDEF $OLDAPPDEF.orig.$$
    ln -s $NEWAPPDEF $OLDAPPDEF
  fi
fi


if [ -h "$OLDAPPDEF" -a -d "$NEWAPPDEF" -a $FUNC = "restore" ]
then
  PARENTDIR=`echo $OLDAPPDEF | sed 's!/app-defaults!!'`
  if [ -d "$PARENTDIR" ]
  then
    (
      cd $PARENTDIR
      APPDIR=`ls | egrep 'app-defaults.orig'`
      echo APPDIR =$APPDIR
      echo $APPDIR | wc -w | egrep '^1$' > /dev/null
      if [ $? -eq 0 ]
      then
        echo "INFO:    Restoring saved $OLDAPPDEF directory "
        rm $OLDAPPDEF
        mv $APPDIR app-defaults
      fi
    )
  fi
fi
