#!/bin/tcsh
# script to install tecs on an instrument computer
set base=/afs/psi.ch/project/sinq
set tecsdir=$base/common/lib/tecs
if ("$OSTYPE" == "linux") then
  set osdir=$base/linux/stow/tecs
else if ("$OSTYPE" == "osf1") then
  set osdir=$base/tru64/stow/tecs
else
  echo "unknown os: $OSTYPE"
  exit
endif

if (! -d bin) then
  echo no 'bin' directory found
  exit
endif

set inst=${PWD:t:al}
if ($inst == tascom) then
  set inst=rita2
endif
echo -n "enter lowercase instrument name [$inst]: "
set ans=$<
if ("$ans" != "") then
  set inst="$ans"
endif
set sd=${inst}_sics
if (! -d $sd) then
  set sd=bin
endif

set ok=1

# find inst in inilist
set hos=(`grep "${inst}:" $tecsdir/inilist`) || set hos=(0 /)
set hos=$hos[2]
set hos=${hos:s/host=//}
if ("${hos:s,/,,}" != "$hos") then
  set std=n
else
  set std=y
endif

set ans=n
if ($std == y) set $ans=y
echo -n "is tecs the standard environment on $inst [$std] ? "
set ans="$<"
if ("$ans" == "y" || "$ans" == "Y") then
  set ans=y
else if ("ans" == "n" || "$ans" == "N") then
  set ans=n
else
  set ans=$std
endif

echo "--------------------------------------------------------------------------"
if (-e $sd/startsics) then
  grep "keep_running TecsServer" $sd/startsics > /dev/null
  if (! $?) then
    echo "the sequence starting TecsServer should be removed from $sd/startsics"
    set ok=""
  endif
  grep startecs $sd/startsics > /dev/null
  if ($?) then
    if ($ans == y) then
      echo "$sd/startsics should contain 'startecs'"
      set ok=""
    endif
  else
    if ($ans != y) then
      echo "$sd/startsics should NOT contain 'startecs'"
      set ok=""
    endif
  endif
else
  echo "$sd/startsics not found"
  set ok=""
endif

set tecs_tcl_not_found=1
if (-e $sd/${inst}.tcl) then
  grep -i "DefineAlias tt " $sd/${inst}.tcl > /dev/null
  if ($?) then
    echo "$sd/${inst}.tcl should contain 'DefineAlias tt temperature'"
  endif
  grep -i "tecs on" $sd/${inst}.tcl > /dev/null
  if ($?) then
    if ($ans == y) then
      echo "$sd/${inst}.tcl should contain 'tecs on'"
      set ok=""
    endif
  else
    if ($ans != y) then
      echo "$sd/${inst}.tcl should NOT contain 'tecs on'"
      set ok=""
    endif
  endif
  grep "/tecs.tcl" $sd/${inst}.tcl > /dev/null
  set tecs_tcl_not_found=$?
else
  echo "$sd/${inst}.tcl not found"
  set ok=""
endif

if (-e $sd/${inst}com.tcl) then
  grep "/tecs.tcl" $sd/${inst}com.tcl > /dev/null
  if ($? && $tecs_tcl_not_found) then
    set hom='$home'
    echo "$sd/${inst}com.tcl should contain 'source $hom/$sd/tecs.tcl' or similar"
    set ok=""
  endif
else
  echo "$sd/${inst}com.tcl not found"
  set ok=""
endif

if ("$ok" == "") then
  echo ""
  echo "please fix all above, before starting tecsinstall again"
  echo -n "ignore and continue anyway [n] ? "
  set ans="$<"
  if ("$ans" != "y" && "$ans" != "Y") then
    exit
  endif
endif

set til=($sd/tecs.ini* tecs/tecs.ini* bi[n-n])
set tin="$til[1]"
if (-f $tin) then
  set hos1=`grep "host=" $tin > /dev/null` && set hos1="$hos"
else
  set hos1="$hos"
endif

echo "Specification for serial connection"
if ("$hos" == "/") then
  if ($ans == n) then
    echo "  syntax: host=localhost:4000/<channel>"
  else
    echo "  syntax: host=psts<nnn>:<3000+channel>"
  endif
  set hos=""
else
  if ("$hos" != "$hos1") then
    echo "       actual value: host=$hos1"
  endif
  echo   "  recommended value: host=$hos (default)"
endif
echo -n  "                     host="
set hans="$<"
if ("$hans" != "") then
  if ("$hos" != "$hans") then
    echo "if you are sure that this is valid,"
    echo "please update $tecsdir/inilist for $inst"
  endif
  set hos="$hans"
endif

rm -rf bin/tecs
rm -f ${til:s/bin//} \
  bin/TecsClient bin/TecsServer bin/keep_running bin/killtecs bin/startecs \
  $sd/TecsClient $sd/TecsServer $sd/keep_running $sd/killtecs $sd/startecs
mkdir -p tecs
echo "host=$hos" > tecs/tecs.ini
ln -s $PWD/tecs/TecsClient bin/tecs
ln -s $PWD/tecs/TecsClient bin/TecsClient
ln -s $PWD/tecs/startecs bin/
ln -s $PWD/tecs/killtecs bin/
rsync -vrCt --delete $tecsdir/cfg tecs/ | grep -v speedup | grep -v wrote
rsync -vt $tecsdir/startecs tecs/ | grep -v speedup | grep -v wrote
rsync -vt $tecsdir/killtecs tecs/ | grep -v speedup | grep -v wrote
rsync -vt $osdir/bin/TecsClient tecs/ | grep -v speedup | grep -v wrote
rsync -vt $osdir/bin/TecsServer tecs/ | grep -v speedup | grep -v wrote
rsync -vt $osdir/bin/keep_running tecs/ | grep -v speedup | grep -v wrote
mkdir -p tecs/log/
