#!/bin/sh
# tag: Tom Lord Tue Dec  4 15:01:37 2001 (arch/arch.sh.in)
#
# arch.sh - the arch system front-end program
################################################################
# Copyright (C) 2001, 2002 Tom Lord
# 
# See the file "COPYING" for further information about
# the copyright and warranty status of this work.
# 

set -e 

################################################################
# archdir
# 
# See Makefile.in
# 

prog="`basename \"$0\"`"
sub_cmd_dir="/usr/lib/arch"

ARCH_SUBCMD_ROOT="$sub_cmd_dir"
export ARCH_SUBCMD_ROOT

################################################################
# Set the PATH, if it is not the one we inherited
# 

if test -e "$HOME/.arch-params/path" ; then
  PATH="`cat \"$HOME/.arch-params/path\"`"
fi

################################################################
# Make Sure that the arch libexec Directory is on the Path
#
# 

PATH="$PATH:$sub_cmd_dir/arch"

# superstition:
export PATH

################################################################
# Set the logging option for daemon processes.
# 
if test -e "$HOME/.arch-params/log-option" ; then
  ARCH_LOG_OPTION="`cat \"$HOME/.arch-params/log-option\"`"
else
  ARCH_LOG_OPTION="--log $HOME/.arch-params/log-file"
fi
export ARCH_LOG_OPTION

################################################################
# Tell Commands What Configured Commands to Use
# 

ARCH_SHELL="/bin/sh"
export ARCH_SHELL

ARCH_MAILER="mailx"
export ARCH_MAILER

ARCH_SENDMAIL="sendmail"
export ARCH_SENDMAIL

################################################################
# Generating Help Messages
# 

help()
{
  all="$1"


  title="$prog sub-commands"
  width=${#title}

  if test $width -gt 64 ; then
    width=$width
  else
    width=$((32 + $width / 2))
  fi

  underscores="`printf %s \"$title\" | sed -e s/./-/g`"

  printf "\\n"
  printf "%*s\\n" "$width" "$title"
  printf "%*s\\n" "$width" "$underscores"
  printf "\\n"

  categories="`cd \"$sub_cmd_dir\" ; cat */Help.category | grep -v -E -e "^[[:space:]]*#" | tsort`"

  for cat in $categories ; do

    printf "* "
    cat "$sub_cmd_dir/$cat/Help.category_title" | grep -v -E -e "^[[:space:]]*#"
    printf "\\n"

    if test -z "$all" ; then
      cmd_list="`cat \"$sub_cmd_dir/$cat/Help.list\" | grep -v -E -e \"^[[:space:]]*#\"`"
    else
      cmd_list="`find \"$sub_cmd_dir/$cat\" -type f -exec test -x \"{}\" \";\" -print | xargs -n 1 need-args basename | sort`"
    fi

    for c in $cmd_list ; do

      if test "$c" = "-" ; then
        printf "\\n"
      else
        summ="`\"$sub_cmd_dir/$cat/$c\" --help | head -1`"
        printf "  %26s : %s\\n" "$c" "$summ"
      fi

    done

    printf "\\n"
    printf "\\n"

  done


  printf "For help with a command, try: %s command --help\\n" "$prog"
  printf "\\n"
}



################################################################
# special options
# 
# Some options are special:
# 
#	--version | -V
#	--help | -h
#	--help-commands | -H
# 
while test $# -gt 0 ; do

  case "$1" in

    --version|-V)
              printf "arch %s from regexps.com\\n"  "arch--release-arch--1.0--patch-15"
              printf "\\n"
              printf "Copyright 2001, 2002, Tom Lord\\n"
              printf "\\n"
              printf "This is free software; see the source for copying conditions.\\n"
              printf "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\\n"
              printf "PARTICULAR PURPOSE.\\n"
              printf "\\n"
	      printf "Report bugs to <bug-arch@regexps.com>.\n"
              printf "\\n"
              exit 0
              ;;


    --help|-h)
              printf "Invoke a sub-command of %s.\\n" "$prog"
              printf "usage: %s [options] command [arguments]\\n" "$prog"
              printf "\\n"
              printf " -V --version                  print version info\\n"
              printf " -h --help                     display help\\n"
              printf "\\n"
              printf " -H --help-commands            display a list of subcommands\\n"
              printf "\\n"
              printf "    --all-commands             display a list of all subcommands\\n"
              printf "                                 including internal commands\\n"
              printf "\\n"
              printf "Invoke the named sub-command with the indicated arguments.\\n"
              printf "\\n"
              printf "\\n"
              exit 0
              ;;

    --help-commands|-H)
              help
              exit 0
              ;;

    --all-commands)
              help all
              exit 0
              ;;

    *)        break
              ;;
  esac
done

################################################################
# Ordinary Arguments
# 

if test $# -lt 1 ; then
  printf "usage: %s [options] command [arguments]\\n" "$prog" 1>&2
  printf "try --help\\n" 1>&2
  exit 1
fi

command="$1"
shift

command_bin="$sub_cmd_dir/arch/$command"

if test ! "(" -x "$command_bin" -a -f "$command_bin" ")" ; then
  printf "%s: no such command (%s)\\n" "$prog" "$command_bin" 1>&2
  printf "try --help-commands\\n" 1>&2
  exit 1
fi

LC_COLLATE=C
export LC_COLLATE

LC_CTYPE=C
export LC_CTYPE

if test ! -z "$LC_ALL" ; then
  LANG="$LC_ALL"
  unset LC_ALL
  export LANG
fi

if test ! -z "$CDPATH" ; then
  unset CDPATH
fi

exec "$command_bin" "$@"

