#!/bin/bash
function die { echo "FATAL! $*"; exit 1; }

test "$P"	||
  case $(uname) in
    AIX)	P=aix			;;
    HP-UX)	P=hp			;;
    CYGWIN*)	P=w32; exe=.exe
      case $(uname -m) in
	x86_64)	P=w64			;;
      esac				;;
    Linux)	P=lnx
      case $(uname -m) in
	x86_64)
	  P=lnx64k2
	  #k=$(uname -r)
	  #k=${k%%.*}
	  #case $k in
	  #  [234])P=lnx64k$k		;;
	  #  *)	die "unsupported Linux kernel $k"
	  #esac
      esac				;;
    SunOS)	P=sun			;;
    Darwin)	P=mac			;;
    *)	die "unsupported platform"
  esac

test "$target"	||
  if test "$1"
    then
      target="$1"
    else
      x=$(type -p pths)
      if test "$x"
        then
	  target=${x%/pths}
	else
	  target=/usr/ml
      fi
  fi

case $P in
  w??)	# windows
    exe=.exe
    let isAdmin=0
    for gid in $(id -G)
    do
      ((gid==114||gid==544)) && let isAdmin=1 && break
    done
    ((isAdmin)) ||echo "WARNING! administrator rights are missing"
    ;;
  *)	# UNIX
    test $(id -u) -eq 0 ||die "root privileges are missing"
esac
mkdir $$;	cd $$ ||die "can't cd into $$"

echo "install or update liberix"

for p in $P any
do
  if test -r ../$p.tgz
    then
      echo "extracting $p.tgz"
      gzip -dc ../$p.tgz |tar xpf -
    else
      echo "can't read $p.tgz, skipping"
  fi
done

if test -d "$target"
  then
    echo "updating $target"
    PATH=$PATH:$target
    comm7 -cp12 -upd12 . "$target"
  else
    echo "installing into $target"
    mkdir -p "$target"
    mv * "$target"/
    ln -sf "$target"/kcr$exe "$target"/kce$exe /bin/
fi

cd ..
rm -rf $$

for s in "$@"
do
  echo "enabling service $s"
  case $P in
    w??)	# windows
      ((isAdmin))	||die "administrator rights are missing"
      type -p cygrunsrv >/dev/null					||
        die "can't find cygrunsrv in PATH"
      case $s in
        rxs)
	  cygrunsrv -I rxx -p "$target"/plisten -a "-f $target/rxs"
	  cygrunsrv -S rxx
	  ;;
	lrm)
	  cygrunsrv -I lrm -p "$target"/lrm -a -f
	  cygrunsrv -S lrm
	  ;;
#	cofs)
#	  cygrunsrv -I cofs -p "$target"/plisten -a "-f -P59 $target/cofs"
#	  cygrunsrv -S cofs
#	  ;;
      esac
      ;;
    *)		# UNIX
      test "$rcloc" ||{
	rcloc=$(find /etc -name rc.local -type f)
	test "$rcloc"	||die "can't find rc.local in /etc"
	test -f "$rcloc" -a -w "$rcloc"					||
	  die "can't write file $rcloc"
      }
      case $s in
        rxs)
	  $target/plisten $target/rxs &&
	    echo "$target/plisten $target/rxs" >>"$rcloc"
	  ;;
	lrm)
	  $target/lrm
	  echo "$target/lrm" >>"$rcloc"
	  ;;
#	cofs)
#	  $target/plisten -P59 $target/cofs &&
#	    echo "$target/plisten -P59 $target/cofs" >>"$rcloc"
#	  ;;
      esac
  esac
  case $s in
    rxs)	test -f /etc/rxs.conf	||{
      echo >/etc/rxs.conf "\
#logfile=/tmp/rxs.%Y-%m-%d.log
# rxs configuration file, (c) M.Liberi, 2001
#
# each line contains the following fields:
# 0. server hostname (optional)
# 1. action {pwdck|allow|deny}
#    pwdck: allow command execution only if the correct password was entered
#    allow: unconditionally allow command execution
#    deny:  deny command execution and stop parsing
#
# optional fields, if present always restrict rule
#
# 2. client OS (A,L,S,I,H,W)
# 3. client ip address (multi RE),      '=' means internal connection
# 4. client hostname (multi RE),        '' means unknown host
# 5. remote username (multi RE)
# 6. local username (multi RE),         '=' means local user = remote user
# 7. command (multi RE)
#
# a multi RE is a pipe separated list of ksh-like regular expression
# i.e. aa|bb|=|*

# allow local root to do anything
allow   *       =       *       root

# allow any local user to execute commands with self
allow   *       =       *       *       =

# deny access to unknown hosts
deny    *       *       |

# satisfy any request if user,password pair is correct
pwdck"
      chmod 600 /etc/rxs.conf
    } ;;
  esac
done

exit 0
