#!/bin/sh
# 
# XMayday
# XMayday script
# 
# Copyright (c)1996. All Rights reserved Axene.
# EMail: <xcalibur@axene.com>
# 
# Started on  Thu Nov 14 16:21:01 1996 Xibios 2
# Last update Fri Nov 15 15:51:34 1996 Xibios 2
#

#--- Init global variables
SkipLine() { echo " "; }
Fatal() { echo "${progname}: error: ${1}" 1>&2 ; exit 1; }

progname=`basename $0`
bin_list=
name_base=XMayday

# ---------------------------------------------------------------------- # 
# AskOneFromList - Let the user choose an item among a list              # 
#                  <text> <default-index> <var > <choice-1>...<choice-n> #
# ---------------------------------------------------------------------- #
AskOneFromList()
{
  _ask_text=$1
  _ask_default=$2
  _ask_var=$3
  shift 3
  _arg_all=$@
  _arg_num=$#

  while :; do
    if [ "$force_flag" = "yes" ]; then
      return $_ask_default
    else
      echo $_ask_text
      _ask_count=0
      while [ $_ask_count -lt $_arg_num ]; do
        if [ $_ask_count -eq $_ask_default ]; then
          _ask_reply="(DEFAULT)"
        else
          _ask_reply=
        fi
        _ask_count=`expr $_ask_count + 1`
        eval "echo \"  \$_ask_count. \$1 $_ask_reply\""
	shift 1
      done
      read _ask_reply
      if [ -z "$_ask_reply" ]; then
	_arg_cnt=0
        for _ask_phrase in $_arg_all; do
         if [ $_arg_cnt -eq $_ask_default ]; then
          eval "$_ask_var=\"\$_ask_phrase\""
          break
        fi
        _arg_cnt=`expr $_arg_cnt + 1`
        done        
	return $_ask_default
      fi
      if [ $_ask_reply -gt 0 -a $_ask_reply -le $_arg_num ]; then
        SkipLine
	_arg_cnt=1
        for _ask_phrase in $_arg_all; do
         if [ $_arg_cnt -eq $_ask_reply ]; then
          eval "$_ask_var=\"\$_ask_phrase\""
          break
         fi
         _arg_cnt=`expr $_arg_cnt + 1`
        done
        return `expr $_ask_reply - 1`
      fi
      echo "ERROR: You must enter a number between 1 and $_arg_num"
      exit 1
    fi
  done
} 

Count()
{
 Glob_count=$#
}

# ---------------------------------------------------------------------- # 
# ---------------------------------------------------------------------- #
TestInHostList()
{
 _Host=`hostname`
 echo testing for host $_Host

 _count_exec=0 
 for _file in $ZHOME/bin/${name_base}*; do
  if [ -x $_file ]; then
   _count_exec=1
   break;
  fi
 done

 if [ "$_count_exec" -eq "0" ]; then
   Fatal "No executable found in $ZHOME/bin/"
 fi

 _No_Host=0
 _Bin_ok=0
 test -r $ZHOME/lib/locale/xmayday.hosts || _No_Host=1
 if [ "$_No_Host" -eq "0" ]; then
  exec 5<$ZHOME/lib/locale/xmayday.hosts
  while read _v_host <&5 ; do
    if [ "$_v_host" = "$_Host" ]; then
     read bin_list <&5
     echo "host $_Host is already associated with program $bin_list"
     _Bin_ok=1
     while read _v_host <&5 ; do
     echo flush > /dev/null
     done
    else
     read _v_burk <&5
     read _v_burk <&5
    fi
  done
  exec 5<&-
 else
  echo "No xmayday.hosts defined."
  if [ -w $ZHOME/lib/locale/ ]; then
   touch $ZHOME/lib/locale/xmayday.hosts
  else
   echo "Not authorized to write to directory $ZHOME/lib/locale/"
  fi
 fi
	
 if [ "$_Bin_ok" -eq "0" ]; then
  Count $ZHOME/bin/${name_base}*
  if [ $Glob_count -eq "1" ]; then
   bin_list=$ZHOME/bin/${name_base}*
   echo $bin_list "is the only executable in this directory"
   if [ -w $ZHOME/lib/locale/xmayday.hosts ]; then
    echo $_Host >> $ZHOME/lib/locale/xmayday.hosts
    echo $bin_list >> $ZHOME/lib/locale/xmayday.hosts
    echo ----- >> $ZHOME/lib/locale/xmayday.hosts
   fi
  else 
   AskOneFromList "Please select the program to execute on $_Host" 0 bin_list $ZHOME/bin/${name_base}*
   if [ ! -w $ZHOME/lib/locale/xmayday.hosts ]; then
    echo "Could not add this entry to $ZHOME/lib/locale/xmayday.hosts"
   else
    echo "$_Host is now associated with program $bin_list"
    echo "(if error, edit or delete $ZHOME/lib/locale/xmayday.hosts)" 
    echo $_Host >> $ZHOME/lib/locale/xmayday.hosts
    echo $bin_list >> $ZHOME/lib/locale/xmayday.hosts
    echo ----- >> $ZHOME/lib/locale/xmayday.hosts
   fi
  fi
 fi
 echo "Execute ${bin_list}"
}

ZHOME=.
AXENE_LIB=$ZHOME/lib
XMAYDAY_LIB=$ZHOME/lib
XAPPLRESDIR=$ZHOME/app-defaults
export XMAYDAY_LIB XAPPLRESDIR

TestInHostList
$bin_list $@

