#!/bin/sh
# Copyright (c) 1994 by Adobe Systems Incorporated.
#
# vi:set ts=40 sw=2:
#
# Shell script for integrating Acrobat with OpenWindow desktop.
#
###################################################################

cmd=acroread
ver=4.0

pdf_icon="pdf.icon"
app_icon="$cmd.prog.icon"
our_cetable="$cmd.cetable"
cetable_orig="/tmp/orig.cetable.$$"
cetable_file="/tmp/new.cetable.$$"
checkstring="<${cmd}-prog>"


#
# Tests the version file in an installation directory.
#
test_install_dir()
{
  if [ "$1" -a -f "$1/AcroVersion" ] \
  && [ "`cat $1/AcroVersion 2>/dev/null`" = "$ver" ] ; then
    return 0
  else
    return 1
  fi
}


#
# Make sure openwin stuff is on user's path so ce_db_merge can be found
#
if [ "$OPENWINHOME" = "" ] ; then
  OPENWINHOME=/usr/openwin
  export OPENWINHOME
fi
PATH="/usr/bin:$PATH:$OPENWINHOME/bin"
export PATH


#
# Process command line args, set mergetype
#
if [ $# = 0 ] ; then
  if [ -x "/usr/ucb/whoami" ] ; then
    whoami="`/usr/ucb/whoami`"
  else
    whoami="`id | sed 's/^[^(]*(\([^)]*\)).*$/\1/'`"
  fi

  if [ "$whoami" = "root" ] ; then
    mergetype=system
  else
    mergetype=user
  fi
elif [ $# = 1 ] ; then
  if [ "$1" = "-user" ] ; then
    mergetype=user
  elif [ "$1" = "-system" ] ; then
    mergetype=system
  elif [ "$1" = "-network" ] ; then
    mergetype=network
  else
    echo "Usage: $0 [ -user | -system | -network ]"
    exit 1
  fi
else
  echo "Usage: $0 [ -user | -system | -network ]"
  exit 1
fi


#
# Setup ACRO_ARG0 to this script
#
arg0="$0"
if [ "$arg0" ] ; then
  case "$arg0" in
     /*) ;;
    ./*) arg0="`pwd`/`expr "$arg0" : '\./\(.*\)'`" ;;
      *) arg0="`pwd`/$arg0" ;;
  esac

  ACRO_ARG0="$arg0"
  export ACRO_ARG0
fi


#
# Try to find the installation directory
#
if ( test_install_dir "$install_dir" ) ; then
  ACRO_INSTALL_DIR="$install_dir"
  export ACRO_INSTALL_DIR
else
  script="$arg0"
  while [ "$script" ] ; do
    install_dir="`dirname "$script"`"
    if ( test_install_dir "$install_dir" ) ; then
      ACRO_INSTALL_DIR="$install_dir"
      export ACRO_INSTALL_DIR
      break
    fi

    install_dir="`dirname "$install_dir"`"
    if ( test_install_dir "$install_dir" ) ; then
      ACRO_INSTALL_DIR="$install_dir"
      export ACRO_INSTALL_DIR
      break
    fi

    install_dir="`dirname "$install_dir"`"
    if ( test_install_dir "$install_dir" ) ; then
      ACRO_INSTALL_DIR="$install_dir"
      export ACRO_INSTALL_DIR
      break
    fi

    if [ -h "$script" ] ; then
      new_script=`ls -l "$script" | sed 's/^.*-> *\(.*\) *$/\1/'`
      if [ "$new_script" -a "`expr "$new_script" : '/.*'`" = 0 ] ; then
        new_script="`dirname "$script"`/$new_script"
      fi
      script="$new_script"
    else
      break
    fi
  done

  if ( test_install_dir "$ACRO_INSTALL_DIR" ) ; then
    :
  elif ( test_install_dir "$ACRO_HOME" ) ; then
    ACRO_INSTALL_DIR="$ACRO_HOME"
    export ACRO_INSTALL_DIR
  else
    echo "ERROR: Cannot find installation directory."
    exit 1
  fi
fi


#
# Check for icon files
#
if [ ! -r "$ACRO_INSTALL_DIR/desktop/olwm/$pdf_icon" \
  -o ! -r "$ACRO_INSTALL_DIR/desktop/olwm/$app_icon" \
  -o ! -r "$ACRO_INSTALL_DIR/desktop/olwm/$our_cetable" ]
then
  echo "ERROR: olwm files missing?"
fi


#
# Check for ce_db commands
#
if [ "`type ce_db_build`" = "ce_db_build not found" ] ; then
  echo "ERROR: Unable to find ce_db_build program."
  exit 1
fi
if [ "`type ce_db_merge`" = "ce_db_merge not found" ] ; then
  echo "ERROR: Unable to find ce_db_merge program."
  exit 1
fi


#
# Determine if database already exists.
#
if ce_db_build $mergetype -to_ascii "$cetable_orig" >/dev/null 2>&1 ; then
  ce_cmd=ce_db_merge
  if grep "$checkstring" "$cetable_orig" >/dev/null 2>&1 ; then
    echo ""
    echo "Icons appear to be already integrated into your desktop."
    echo ""
    echo "If you find that the icons do not appear or work properly,"
    echo "you might want to run binder to remove the old icons and"
    echo "then rerun $0 to install new icons."
    echo ""
    rm -f "$cetable_orig"
    exit 0
  fi
else
  ce_cmd=ce_db_build
fi
rm -f "$cetable_orig"


#
# Set CE_DIR based on $mergetype
#
if [ "$mergetype" = "user" ] ; then
  CE_DIR=$HOME/.cetables
elif [ "$mergetype" = "system" ] ; then
  CE_DIR=/etc/cetables
elif [ "$mergetype" = "network" ] ; then
  CE_DIR=$OPENWINHOME/include/images
else
  echo "ERROR: Invalid mergetype: $mergetype. Exiting."
  exit 1
fi


#
# Create directory, if necessary.
#
if [ ! -d "$CE_DIR" ] ; then
  mkdir -p "$CE_DIR"
  if [ $? != 0 ] ; then
    echo "ERROR: Cannot create directory $CE_DIR"
    exit 1
  fi
fi


#
# Copy icon files into the appropriate place.
#
cp "$ACRO_INSTALL_DIR/desktop/olwm/$pdf_icon" "$CE_DIR"
if [ $? != 0 ] ; then
  echo "ERROR: Unable to copy icon $pdf_icon into $CE_DIR."
  exit 1
fi
cp "$ACRO_INSTALL_DIR/desktop/olwm/$app_icon" "$CE_DIR"
if [ $? != 0 ] ; then
  echo "ERROR: Unable to copy icon $app_icon into $CE_DIR."
  exit 1
fi


#
# Copy cetable file to /tmp, replacing $ICONDIR with $CE_DIR.
#
rm -f "$cetable_file"
cat "$ACRO_INSTALL_DIR/desktop/olwm/$our_cetable" | \
  sed "s%\$ICONDIR%$CE_DIR%" > "$cetable_file"


#
# Integrate into OpenWindows Desktop
#
"$ce_cmd" $mergetype -from_ascii "$cetable_file" >/dev/null 2>&1
if [ $? != 0 ] ; then
  echo ""
  echo "Command $ce_cmd failed."
  echo "Installation onto the OpenWindows desktop failed...exiting."
  echo "You may need to install icons manually using the OpenWindows binder program."
  rm -f "$cetable_file"
  exit 1
fi
rm -f "$cetable_file"


#
# All done
#
echo "Integration with OpenWindows desktop completed."
exit 0

