#!/sbin/sh

PGM=$(basename $0)
CMAKE=/usr/atria/bin/clearmake
. /etc/rc.config.d/netconf
HOSTS="feanor sirion mahtan anvil spade farin sinkku"

# sirion's vob root is "vobs"
typeset VOBROOT=/vob
if [ "$(/sbin/uname -n)" = "sirion" ]; then
    VOBROOT=/vobs
fi

vob[0]="$VOBROOT/hp"

LINKS_EXCLUDE_CC5="
    /usr/lib/libstd.2
    /usr/lib/libstream.2
    /usr/lib/libCsup.2"
LINKSCOM="
    /opt/aCC
    /opt/ansic
    /opt/blinklink
    /opt/dce
    /opt/imake
    /opt/langtools
    /usr/ccs
    /usr/include
    /usr/lib/libCsup.a
    /usr/lib/librwtool.1
    /usr/lib/librwtool.a
    /usr/lib/libstd.a
    /usr/lib/libstream.a
    /usr/lib/nls/msg/C/chatr.cat
    /usr/lib/nls/msg/C/fastbind.cat
    /usr/lib/nls/msg/C/ld.cat
    /usr/share/man/man1.Z/chatr.1
    /usr/share/man/man1.Z/fastbind.1
    /usr/share/man/man1.Z/ld.1
    /usr/contrib/bin/odump"
LINKS10="
    /opt/CC
    /usr/newconfig/usr/ccs/bin/ld"
LINKS11="
    /usr/lib/libCsup_v2.a
    /usr/lib/libCsup_v2.1
    /usr/lib/libCsup_v2.2
    /usr/lib/librwtool.2
    /usr/lib/libstd_v2.1
    /usr/lib/libstd_v2.2
    /usr/lib/libstd_v2.a
    /usr/lib/nls/msg/C/as.cat
    /usr/old/usr/include"
if [ "$(uname -r)" = "B.10.20" ]; then
    links[0]="$LINKS10 $LINKSCOM"
else
    if [ "$(/sbin/uname -n)" = "sirion" ]; then
      links[0]="$LINKS11 $LINKSCOM"
    else
      links[0]="$LINKS11 $LINKSCOM $LINKS_EXCLUDE_CC5"
    fi
fi
vob[1]="$VOBROOT/nnm"
links[1]="/opt/OV"
vob[2]="$VOBROOT/iona"
links[2]="/opt/iona"
vob[3]="$VOBROOT/emtools"
links[3]="/opt/java"
vob[4]="$VOBROOT/emtools"
links[4]="/opt/java1.2"
vob[5]="$VOBROOT/emtools"
links[5]="/opt/java1.3"
vob[6]="$VOBROOT/nnmsc/tools10/compiler_tools"
links[6]="/opt/perl5"

function vobnr {
  case "$HOSTNAME" in
    mahtan|sinkku) echo 7;;
    sirion|anvil|spade) echo 6;;
    *)      echo 1;;
  esac
}
RDIRS="
    /opt
    /usr/ccs
    /usr/include
    /usr/lib
    /usr/share
    /usr/contrib
    /usr/newconfig
    /usr/old/usr"
PATH=/sbin

function usage {
cat <<EOF
$PGM:
  Use only:
    1) through a link selecting the requested function:
         setvoblinks
         resetvoblinks
         checkvoblinks
       or as:
         voblinks [set|reset|check]
    2) in one host from the following list:
         $HOSTS
EOF
exit 1
}

function case_expression {
  echo case $HOSTNAME in $1; shift
  while [ -n "$1" ]; do echo "|$1"; shift; done
  echo ') ;; *) usage;; esac'
}

[ -x /sbin/uname ] || usage
eval $(case_expression $HOSTS)

function disable_clearmake {
  chmod -x $CMAKE
}

function enable_clearmake {
  chmod +x $CMAKE
}

function setvoblinks {
  let i=0
  while [ $i -lt $(vobnr) ]; do
    for l in ${links[$i]}; do
      if [ ! -L $l ]; then
        mv $l $l.bak; ln -s ${vob[$i]}$l $l
      else
        echo $l not found or is already a soft link
      fi
    done
    let i=$i+1
  done
  enable_clearmake
}

function resetvoblinks {
  disable_clearmake  
  let i=0
  while [ $i -lt $(vobnr) ]; do
    for l in ${links[$i]}; do
      if [ -L $l ]; then
        rm $l
      else
        echo $l not a soft link
      fi
      if [ -r $l.bak ]; then
        mv $l.bak $l
      else
        echo $l.bak not found
      fi
    done
    let i=$i+1
  done
}

function checkvoblinks {
  if [ ! -x /usr/bin/find ]; then
    echo This function requires access to /usr/bin
    exit 0
  fi
  for d in $RDIRS; do
    /usr/bin/find $d -type l | (
      while read l; do
        if [ -r $l.bak ]; then echo $l
        fi
      done
    )
  done 2>/dev/null | /usr/bin/sort > /tmp/voblinks
  /usr/bin/chmod a+rw /tmp/voblinks 2>/dev/null
  if [ ! -s /tmp/voblinks ]; then
    echo The links to the vob are not in place: standard configuration
  else
    ( let i=0
      while [ $i -lt $(vobnr) ]; do
        for l in ${links[$i]}; do echo $l; done
        let i=$i+1
      done ) | /usr/bin/sort | /usr/bin/diff /tmp/voblinks -
    if [ $? -eq 0 ]; then
      echo The links to the vob are in place and
      echo the list of links in is up-to-date in $PGM.
    else
      echo "The links marked as \">\" are expected by $PGM, but missing."
      echo "The ones marked as \"<\" were found, but are unknown to $PGM."
    fi
  fi
}

case $PGM in
    setvoblinks)   setvoblinks;;
    resetvoblinks) resetvoblinks;;
    checkvoblinks) checkvoblinks;;
    voblinks)      case "$1" in
                       set)   setvoblinks;;
                       reset) resetvoblinks;;
                       check) checkvoblinks;;
                       *)     usage;;
                   esac;;
    *)             usage;;
esac
