#! /bin/sh
# frobme --- xdm login banner hack
# Author: Noah Friedman <friedman@prep.ai.mit.edu>
# Created: 1992-04-20

# $Id: frobme,v 1.4 1996/09/15 08:39:42 friedman Exp $

# Copyright (C) 1996 Noah S. Friedman

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can either send email to this
# program's maintainer or write to: The Free Software Foundation,
# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.

# Commentary:
# Code:

# Name by which this script was invoked.
progname=`echo "$0" | sed -e 's/[^\/]*\///g'`

# To prevent hairy quoting and escaping later.
bq='`'
eq="'"

usage="Usage: $progname {options}

Options are:
-D, --debug                  Turn on shell debugging ($bq${bq}set -x$eq$eq).
-h, --help                   You're looking at it.
-f, --frobdir     DIR        Templates located in DIR.
-v, --verbose                Be verbose.
-y, --yow                    I'm ANN LANDERS!!  I can SHOPLIFT!!
"

# Initialize variables.
# Don't use `unset' since old bourne shells don't have this command.
# Instead, assign them an empty value.
debug=
verbose=
FROBDIR="${FROBDIR-$HOME/lib/frobdir}"
local_xresources_file="$HOME/lib/Xresources.local"
stderr_file=
error_reports=
command_line_args="$*"

# Usage: eval "$getopt"; value=$optarg
# or     optarg_optional=t; eval "$getopt"; value=$optarg
#
# This function automatically shifts the positional args as appropriate.
# The argument to an option is optional if the variable `optarg_optional'
# is non-empty.  Otherwise, the argument is required and getopt will cause
# the program to exit on an error.  optarg_optional is reset to be empty
# after every call to getopt.  The argument (if any) is stored in the
# variable `optarg'.
#
# Long option syntax is `--foo=bar' or `--foo bar'.  2nd argument
# won't get used if first long option syntax was used.
#
# Note: because of broken bourne shells, using --foo=bar syntax can
# actually screw the quoting of args that end with trailing newlines.
# Specifically, most shells strip trailing newlines from substituted
# output, regardless of quoting.
getopt='
  {
    optarg=
    case "$1" in
      --*=* )
        optarg=`echo "$1" | sed -e "1s/^[^=]*=//"`
        shift
       ;;
      * )
        case ${2+set} in
          set )
            optarg="$2"
            shift
            shift
           ;;
          * )
            case "$optarg_optional" in
              "" )
                case "$1" in
                  --*=* ) option=`echo "$1" | sed -e "1s/=.*//;q"` ;;
                  * ) option="$1" ;;
                esac
                exec 1>&2
                echo "$progname: option $bq$option$eq requires argument."
                echo "$progname: use $bq--help$eq to list option syntax."
                exit 1
               ;;
           esac
           shift
         ;;
        esac
     ;;
    esac
    optarg_optional=
  }'

# Parse command line arguments.
# Make sure that all wildcarded options are long enough to be unambiguous.
# It's a good idea to document the full long option name in each case.
# Long options which take arguments will need a `*' appended to the
# canonical name to match the value appended after the `=' character.
while : ; do
  case $# in 0) break ;; esac
  case "$1" in
    -D | --debug | --d* )
      debug=t
      shift
     ;;
    -h | --help | --h* )
      echo "$usage" 1>&2
      exit 0
     ;;
    -e | --errors-to* | --e* )
      eval "$getopt"
      error_reports="$optarg"
      stderr_file=/tmp/$progname.stderr$$
     ;;
    -f | --frobdir* | z--frobd* )
      eval "$getopt"
      FROBDIR="$optarg"
     ;;
    --frobme-baby* | --frobme_baby* | --frobm* )
      eval "$getopt"
      frobme_baby="$optarg"
     ;;
    -v | --verbose | --v* )
      verbose=t
      shift
     ;;
    -y | --yow | --y* )
      frobme_baby=yowza
      shift
     ;;
    -- )     # Stop option processing
      shift
      break
     ;;
    -? | --* )
      case "$1" in
        --*=* ) arg=`echo "$1" | sed -e 's/=.*//'` ;;
        * )     arg="$1" ;;
      esac
      exec 1>&2
      echo "$progname: unknown or ambiguous option $bq$arg$eq"
      echo "$progname: Use $bq--help$eq for a list of options."
      exit 1
     ;;
    -??* )
      # Split grouped single options into separate args and try again
      optarg="$1"
      shift
      set fnord `echo "x$optarg" | sed -e 's/^x-//;s/\(.\)/-\1 /g'` ${1+"$@"}
      shift
     ;;
    * )
      break
     ;;
  esac
done

case "$debug" in t ) set -x ;; esac

case "$stderr_file" in
  '' ) : ;;
  * )
    exec 2> "$stderr_file"
    trap 'rm -f "$stderr_file"' 0 1 2 3 15
   ;;
esac

frobfile="$frobme_baby"
case "$frobfile" in
  '' )
    if cd "$FROBDIR" ; then
      # find all files but autosaves and backup files.
      files="`ls -1 | sed -e '/^#/d' -e '/~$/d'`"
      set fnord $files
      shift

      case "${RANDOM-unset}" in
        unset )
          RANDOM=`perl -e 'srand (time());
                           printf ("%ld\n", rand() * 100000);'`
         ;;
      esac

      arg=`expr \( \( $RANDOM + $$ \) % $# \) + 1`

      frobfile="$FROBDIR/"`echo "$files" | sed -ne "${arg}p"`
    fi
   ;;
  /* | ./* | */* ) : ;;
  * ) frobfile="$FROBDIR/$frobfile" ;;
esac

case "$frobfile" in
  '' ) frobfile="$FROBDIR" ;;
  "$FROBDIR/yowza" )
    for resource in greeting namePrompt passwdPrompt fail; do
      while : ; do
        yowza=`yow | sed -ne 'H
                              ${x
                                s/\n/ /g
                                s/[ 	][ 	]*/ /g
                                s/^ //g
                                s/ $//g
                                p
                               }'`
        case "$yowza" in
          # If > 60 chars, repeat.
          ????????????????????????????????????????????????????????????? ) : ;;
          * ) break ;;
        esac
      done

      echo "xlogin*${resource}: $yowza"
    done > "${FROBDIR}/yowza"
   ;;
esac

if test -f "$frobfile" ; then
  rm -f "$local_xresources_file"
  ln -s "$frobfile" "$local_xresources_file"
  test -n "$verbose" && echo "$frobfile"
else
  echo "$progname: $frobfile: can't access file." 1>&2
fi

if test -n "$stderr_file" && test -s "$stderr_file" ; then
  sendmail -oi -t <<- __EOF__
From: ${USER-`whoami`} ($progname script)
To: $error_reports
Subject: $progname stderr output
Precedence: bulk

This is an automated report from host `hostname`.
Program "$progname" ran with the following arguments:

        $command_line_args

and generated the following output on stderr:

`cat $stderr_file`
__EOF__
fi

:

# frobme ends here
