#!/bin/bash

info_message="/afs/fnal.gov/files/code/e898/code/6/info-message/info-message"

while getopts :i: OPT; do
  case $OPT in
    i)
      info_message="$OPTARG"
      ;;
    *)
      echo "usage: ${0##*/} [-i ARG]"
      exit 2
  esac
done
shift $[ OPTIND - 1 ]
TMP=/tmp/boone_info_config.$$
trap "rm $TMP* 2>/dev/null" EXIT

if [ "`echo ~products`" != "/local/ups" ] ; then
  echo "~products is NOT /local/ups!"
  exit 2
fi

if [ ! -d ~products ]; then
  echo "~products does not appear to exist!"
  exit 2
fi

config_file="/local/ups/db/.upsfiles/dbconfig"

if [ ! -w $config_file ]; then
  echo "$config_file does not exist or is not writable!"
  exit 2
fi

sed -e '/INFO_TARGET_DIR/c \
INFO_TARGET_DIR = /afs/fnal.gov/files/code/e898/code/6/BooNE_Info
' $config_file > $TMP

if [ ! -s $TMP ]; then
  echo "Replacement dbconfig file has zero length!"
  exit 2
fi

if cmp $config_file $TMP > /dev/null 2>&1; then
  :
else
  cp $TMP $config_file
fi

if [ ! -r "$info_message" ]; then
  echo "info-message X startup script is not accessible!"
  exit 2
fi

install -C -m 755 $info_message /etc/X11/xinit/xinitrc.d/
exit $?
