#!/bin/sh 
# $Id: mini-lisa,v 1.9 1998/08/18 14:10:47 ray Exp $

#
# Init setup
#
DIR_INIT_CONF=/etc/rc.d
DIR_INFO=/usr/info
CMD_INSTALL_INFO=/usr/bin/install-info
CMD_INETDCONF=/usr/lib/LST/inetdconf.pl

unset GZIP

Debug()
{
  echo "$@" 1>&2
}

Print_Usage()
{
  cat <<'EOF' 1>&2
mini-lisa <area> command parameters...
Areas:
  --SysVinit   reads further information from STDIN

  --SysV-init
    install <subsys> S<ss> <level>[:<level>]... K<kk> <level>[:<level>]...
    remove <subsys> <trigger>

  --info
    install <topic> [--section "<string>"] [--entry "<string>"]
    remove <topic> <trigger>

  --inetd
    install <service name> <socket type> <protocol> \
	(wait|nowait[.<max>]) <user>[.<group>] \
	<server program> <server program arguments> ...
    install <service name>/<version> <socket type> rpc/<protocol> \
	(wait|nowait[.<max>]) <user>[.<group>] \
	<server program> <server program arguments> ...
    remove <service name>[/<version>] <trigger>
    enable <service name>[/<version>] <trigger>
    disable <service name>[/<version>] <trigger>

  --user
    create <name> <uid> <gid> <gcos> <home> <shell>
    revoke <uname> <trigger>

  --group
    create <gname> <gid> [<initial_member> ...]
    join <gname> <uname>
    leave <gname> <uname> <trigger>
    revoke <gname> <trigger>

EOF
  return 1
}

Print_Usage_Exit()
{
  Print_Usage
  exit 1
}

Do_SysVinit()
{
  local script=
  local callname=
  local levels=

  while read script callname levels; do
    for i in `echo $levels | sed -e 's/:/ /g'`; do
      mkdir -p $DIR_INIT_CONF/rc$i.d >/dev/tty8 2>&1
      ln -sf ../init.d/$script $DIR_INIT_CONF/rc$i.d/$callname >/dev/tty8 2>&1
    done
  done
}

Do_SysVinit_install()
{
  local subsys="$1"
  local start="$2$1"
  local slevels="$3"
  local stop="$4$1"
  local klevels="$5"

  # consistency check
  #FIXME!?

  # cleanup
  Do_SysVinit_remove $subsys 0

  # create start- and stoplinks
  local name=$start
  for l in $slevels $klevels; do
    for i in `echo $l | sed -e 's/:/ /g'`; do
      [ -d $DIR_INIT_CONF/rc$i.d ] || mkdir -p $DIR_INIT_CONF/rc$i.d
      ln -sf ../init.d/$subsys $DIR_INIT_CONF/rc$i.d/$name
    done
    name=$stop
  done
}

Do_SysVinit_remove()
{
  local subsys="$1"
  local trigger="$2"

  [ "$trigger" != "0" ] && return 0

  # consistency check
  #FIXME!?

  rm -f $DIR_INIT_CONF/rc?.d/[SK][0-9][0-9]$subsys
}

Info_FindFile()
{
  local topic=$1
  local file

  if [ -n "`echo $topic | sed -n 's:^/.*$:OK:p'`" ]; then
    if [ -r $topic ]; then
      file=$topic
    elif [ -r $topic.info ]; then
      file=$topic.info
    elif [ -r $topic.gz ]; then
      file=$topic.gz
    elif [ -r $topic.info.gz ]; then
      file=$topic.info.gz
    fi
  elif [ -r $DIR_INFO/$topic ]; then
    file=$DIR_INFO/$topic
  elif [ -r $DIR_INFO/$topic.info ]; then
    file=$DIR_INFO/$topic.info
  elif [ -r $DIR_INFO/$topic.gz ]; then
    file=$DIR_INFO/$topic.gz
  elif [ -r $DIR_INFO/$topic.info.gz ]; then
    file=$DIR_INFO/$topic.info.gz
  fi
  echo $file
}

Do_Info_install()
{
  Check_Info_Not_OLD || { Do_Info_install_OLD "$@"; return $?; }

  local topic="$1"; shift
  local file

  # consistency check
  #FIXME!?

  file=`Info_FindFile $topic`

  if [ -z "$file" ] || [ ! -r "$file" ]; then
    Debug "Do_Info_install $topic not found"
    return 1
  fi

  $CMD_INSTALL_INFO "$@" $file $DIR_INFO/dir
  return 0
}

Do_Info_remove()
{
  Check_Info_Not_OLD || { Do_Info_remove_OLD "$@"; return $?; }

  local topic="$1"; shift
  local trigger="$1"; shift
  local file

  [ "$trigger" != "0" ] && return 0

  # consistency check
  #FIXME!?

  file=`Info_FindFile $topic`

  if [ -z "$file" ] || [ ! -r "$file" ]; then
    Debug "Do_Info_remove $topic not found"
    return 1
  fi

  $CMD_INSTALL_INFO --remove $file $DIR_INFO/dir
  return 0
}

# backward-compatibility ...
Check_Info_Not_OLD()
{
  local v=`$CMD_INSTALL_INFO --version | sed -n '1s/^[^0-9]*//p'`
  [ "$v" != "3.11" ] && return 0
  return 1
}

Info_GetCmd()
{
  local file="$1"
  local cmd=cat

  [ -n "`echo $file | sed -n 's/^.*\.gz$/OK/p'`" ] &&
    cmd=zcat
  echo $cmd
}

Do_Info_install_OLD()
{
  local topic="$1"; shift
  local file
  local cmd

  # consistency check
  #FIXME!?

  file=`Info_FindFile $topic`
  cmd=`Info_GetCmd $file`

  if [ -z "$file" ] || [ ! -r "$file" ]; then
    Debug "Do_Info_install $1 not found"
    return 1
  fi

  $cmd $file | $CMD_INSTALL_INFO "$@" - $DIR_INFO/dir
  return 0
}

Do_Info_remove_OLD()
{
  local topic="$1"
  local trigger="$2"
  local file
  local cmd

  [ "$trigger" != "0" ] && return 0

  # consistency check
  #FIXME!?

  file=`Info_FindFile $topic`
  cmd=`Info_GetCmd $file`

  if [ -z "$file" ] || [ ! -r "$file" ]; then
    Debug "Do_Info_remove $1 not found"
    return 1
  fi

  $cmd $file | $CMD_INSTALL_INFO --remove - $DIR_INFO/dir
  return 0
}

Do_Inetd_install()
{
  # consistency check in inetdconf!?

  $CMD_INETDCONF --add "$@"
}

Do_Inetd_remove()
{
  local service="$1"
  local trigger="$2"

  [ "$trigger" != "0" ] && return 0

  # further consistency check in inetdconf!?

  $CMD_INETDCONF --remove "$service"
}

Do_Inetd()
{
  local command="$1"; shift

  case "$command" in
  "install"|"remove")
    Do_Inetd_${command} "$@"
    return $?
    ;;
  "enable"|"disable")
    [ $# -gt 0 ] && { local service=$1; shift; }
    [ $# -gt 0 ] && { local trigger=$1; shift; }
    [ "$trigger" != "0" ] && return 0
    # further consistency check in inetdconf!?
    $CMD_INETDCONF --$command "$service" "$@"
    return $?
    ;;
  *)
    Print_Usage
    ;;
  esac
}

Do_User()
{
  local command="$1"; shift

  case "$command" in
  "create")
    false
    ;;
  "revoke")
    false
    ;;
  *)
    Print_Usage
    ;;
  esac
}

Do_Group_Join()
{
  local gname="$1"; shift
  local i

  for i in "$@"; do
    false
  done
  false
}

Do_Group_Leave()
{
  false
}

Do_Group()
{
  local command="$1"; shift
  local gname="$1"; shift

  case "$command" in
  "create")
    false
    ;;
  "join")
    Do_Group_Join $gname "$@"
    ;;
  "leave")
    [ "$1" != "0" ] && return 0; shift

    Do_Group_Leave $gname "$@"
    ;;
  "revoke")
    [ "$1" != "0" ] && return 0; shift

    false
    ;;
  *)
    Print_Usage
    ;;
  esac
}

if [ $# -eq 0 ]; then
  Print_Usage_Exit
fi

Area=""
case "$1" in
"--SysVinit")
  Do_SysVinit
  exit $?
  ;;
"--SysV-init")
  shift
  Area="SysVinit"
  ;;
"--info")
  shift
  Area="Info"
  ;;
"--inetd")
  shift
  Area="Inetd"
  ;;
"--user")
  shift
  Area="User"
  ;;
"--group")
  shift
  Area="Group"
  ;;
*)
  Print_Usage_Exit
  ;;
esac

[ -z "$Area" ] && Print_Usage_Exit

[ $# -gt 0 ] && { Command="$1"; shift; }
case "$Area" in
"User"|"Group"|"Inetd")
  Do_${Area} $Command "$@"
  exit $?
  ;;
esac

case "$Command" in
"install"|"remove")
  Do_${Area}_${Command} "$@"
  exit $?
  ;;
esac


Print_Usage_Exit


