#!/bin/sh

# Copyright (C) 1996  Bay Networks, Inc.
#
# ACC_ENABLE:  Installs Accelar (Rapid City)  setup into user's shell init files
#
# usage:   ACC_ENABLE
#
# 19 August 1997 Steve Sun -- copied from ANNEX_ENABLE.
#


HOST_OS=`uname`
if [ $HOST_OS = "SunOS" ]; then
  if [ `uname -r` -ge 5 ]; then
    HOST_OS=Solaris
    PATH=/bin:/usr/bin:/etc:/usr/etc
  fi
else
  PATH=/bin:/usr/bin:/usr/ucb:/etc:/usr/etc
fi

USAGE()
{
  echo "$0"
  exit 1
}

updateSNMdefaults()
{
# Add the RC schema file paths to .SNMdefaults, if present.

ACC_SNM=/usr/acc/snm

if [ -f ${HOME}/.SNMdefaults ] ; then
    if [ -w ${HOME}/.SNMdefaults ] ; then

        fgrep $ACC_SNM ${HOME}/.SNMdefaults >/dev/null

        if [ $? -ne 0 ] ; then

                echo ".SNMdefaults exists; copied to .SNMdefaults.$$"
                cp ${HOME}/.SNMdefaults ${HOME}/.SNMdefaults.$$

            ex - ${HOME}/.SNMdefaults <<EOF >/dev/null
1,\$s!^snm.console.iconPath.*\$!&:${ACC_SNM}/icons!
1,\$s!^snm.console.schemaPath.*\$!&:${ACC_SNM}/agents:${ACC_SNM}/struct-base:${ACC_SNM}/struct!
w!
q
EOF

        fi

    else

        echo "ERROR:  \".SNMdefaults\" isn't writable, so it wasn't updated."
        exit 1

    fi
fi
}

check_vars()
{
  file=$1

  if grep DMPATH $file >/dev/null 2>&1; then
    need_dmpath=false
  else
    need_dmpath=true
  fi
  if grep /usr/acc/bin $file >/dev/null 2>&1; then
    need_path=false
  else
    need_path=true
  fi
}

update_csh()
{
  cfile=$1
  check_vars $cfile

  if $need_dmpath || $need_path
  then
     cat >> $cfile <<EOF

# Added by ACC_ENABLE for OPTACC, `date`
#
EOF
     if $need_dmpath
     then
       cat >> $cfile <<EOF
setenv DMPATH /usr/acc

EOF
       echo
       echo "Added OPTACC environment variables to $cfile."
       echo "You must  source $cfile   before starting OPTACC."
     fi
     if $need_path
     then
       cat >> $cfile <<EOF
set path=( \$path $DMPATH $DMPATH/bin )
EOF
       echo
       echo "Added OPTACC Path variables to $cfile."
     fi
  else
    echo
    echo "Nothing to update in $file."
  fi
}

update_sh()
{
  cfile=$1
  check_vars $cfile

  if $need_dmpath || $need_path
  then
     cat >> $cfile <<EOF

# Added by ACC_ENABLE for OPTACC, `date`
#
EOF
     if $need_dmpath
     then
       cat >> $cfile <<EOF
DMPATH=/usr/acc
export DMPATH

EOF
       echo
       echo "Added OPTACC environment variables to $cfile."
       echo "You must   rexecute $cfile   before running OPTACC."
     fi

     if $need_path
     then
       cat >> $cfile <<EOF
PATH=\$PATH:$DMPATH:$DMPATH/bin
export PATH

EOF
       echo
       echo "Added OPTACC Path variables to $cfile."
     fi
  else
    echo
    echo "Nothing to update in $file."
  fi
}

found_none=true

if [ x"$HOME" = x ] || [ ! -d "$HOME" ]; then
  echo "Couldn't find your home directory (no HOME variable)!"
  exit 1
fi

# Look for ~/.cshrc; update it if found
#
if [ -f $HOME/.cshrc ]; then
  if [ ! -w $HOME/.cshrc ]; then
    echo "Can't write to file $HOME/.cshrc... skipped."
  else
    update_csh $HOME/.cshrc
    found_none=false
  fi
fi

# Look for ~/.profile; update it if found
#
no_profile=true
if [ -f $HOME/.profile ]; then
  if [ ! -w $HOME/.profile ]; then
    echo "Can't write to file $HOME/.profile... skipped."
  else
    update_sh $HOME/.profile
    found_none=false
    no_profile=false
  fi
fi

# If we couldn't find a ~/.profile the user might be running ksh or
# bash, so look for an $ENV file; if present update that
#
if $no_profile && [ x"$ENV" != x ]; then
  if [ ! -f "$ENV" ]; then
    echo "Found a \$ENV variable ($ENV), but it doesn't refer to a file..."
    echo "skipped."
  elif [ ! -w "$ENV" ]; then
    echo "Cannot write to ENV file $ENV... skipped."
  else
    update_sh $ENV
    found_none=false
  fi
fi

updateSNMdefaults

# If we didn't find anything, tell the user something's broken!
#
if $found_none; then
  echo
  echo "No shell initialization files could be found; nothing done (\$HOME = \"$HOME\")!"
  echo
  echo "You'll have to update your shell init files by hand by setting these"
  echo "environment variables:"
  echo
  echo "  DMPATH   = /usr/acc"
  echo '  PATH     = ${PATH}:/usr/acc:/usr/acc/bin'
  echo
  exit 1
fi

