#! /bin/sh
# card --- smartly produce a printed reference card of a program

# Copyright (c) 1988, 89, 90, 91, 92, 93 Miguel Santana
# Copyright (c) 1995, 96, 97, 98 Akim Demaille, Miguel Santana

# card.in,v 1.1.1.1 1998/03/26 16:45:52 jch Exp

# 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:

# Author: Akim Demaille <demaille@inf.enst.fr>

# In the interest of general portability, some common bourne shell
# constructs were avoided because they weren't guaranteed to be available
# in some earlier implementations.  I've tried to make this program as
# portable as possible.  Welcome to unix, where the lowest common
# denominator is rapidly diminishing.
#
# Among the more interesting lossages I noticed with some bourne shells
# are:
#     * No shell functions.
#     * No `unset' builtin.
#     * `shift' cannot take a numeric argument, and signals an error if
#       there are no arguments to shift.

# Code:

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

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

usage="Usage: $program [OPTION]... PROGRAM...
Print a reference card of the PROGRAMs thanks to their inline help.

Options:
 -h, --help           display this help and exit
 -v, --version        display version information and exit
 -l, --language=LL    print the help in the language LL (default: English)
     --command=CMD    perform pretty-printing on the output of CMD

Unrecognized options are passed to a2ps.  Arguments cannot be
separated from the options."

version="card $card_version (a2ps 4.10)

Copyright (c) 1988, 89, 90, 91, 92, 93 Miguel Santana
Copyright (c) 1995, 96, 97, 98 Akim Demaille, Miguel Santana
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Written by <Akim.Demaille@inf.enst.fr> and <Miguel.Santana@st.com>
News, updates and documentation: visit http://www.inf.enst.fr/a2ps/"

# Initialize variables.
# Don't use `unset' since old bourne shells don't have this command.
# Instead, assign them an empty value.
# Some of these, like A2PS, may be inherited from the environment.
a2ps="${A2PS-a2ps}"
a2ps_options="-Ecard"
commands=
debug=
form_feed=
help="Try \`$program --help' for more information."
LC_ALL="${LC_ALL-C}" export LC_ALL
print_form_feeds=:
RM="/bin/rm -rf"
tmp_dir=${TMPDIR-/tmp}/$program.$$
tmp_file=$tmp_dir/card
success=
verbose=:

# List of the possible ways to get the on line help.
# -flags is used with Solaris' CC.
possible_options="--help -h -\? -flags"

# Create a tmp dir and be ready to clean up
cleanup="$RM $tmp_dir"
trap "$cleanup" 0 1 2 15

# Parse our command line options once, thoroughly.
while test $# -gt 0
do
  arg="$1"
  shift

  case "$arg" in
  -*=*) optarg=`echo "$arg" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) optarg= ;;
  esac

  # If the previous option needs an argument, assign it.
  if test -n "$prevopt"; then
    optarg="$arg"
    arg="$prevopt="
    prevopt=
  fi

  # Have we seen a non-optional argument yet?
  case "$arg" in
  --help)
    echo "$usage"
    exit 0
    ;;

  --version)
    echo "$version"
    exit 0
    ;;

  --language|-l) prevopt="--language" ;;
  --language=*) 
    LC_ALL=$optarg
    ;;

  --command|-c) prevopt="--command" ;;
  --command=*)
    commands="$commands
$optarg"
    ;;

  --no-form-feed|-f)
    print_form_feeds=
    ;;

  --verbose)
    verbose=echo
    ;;

  -D|--debug)  # Delay debugging so that options parsing does not appear
    debug=:;
    ;;

  -*)
    a2ps_options="$a2ps_options $arg"
    ;;

  *)
    nonopt="$nonopt $arg"
    ;;
  esac
done

if test -n "$prevopt"; then
  echo "$program: option \`$prevopt' requires an argument" 1>&2
  echo "$help" 1>&2
  exit 1
fi

if test "X$nonopt" = X && test "X$commands" = X; then
  echo "$program: no program given" 1>&2
  echo "$help" 1>&2
  exit 1
fi

# Make the tmp dir
mkdir $tmp_dir

case $LC_ALL in
  fr) 
    footer="Engendr par card version $card_version (a2ps 4.10)"
    ;;
  *)
    footer="Generated by card version $card_version (a2ps 4.10)"
    ;;
esac

# Set -x now if debugging
test $debug && set -x

for file in $nonopt
do
  success=
  filename=`echo "$file" | sed -e 's/[^\/]*\///g'`
  $verbose "Working on $bq$filename$eq"
  case $LC_ALL in
    fr) 
       title="Carte de rfrence pour $filename"
       ;;
    *)
       title="Reference card of $filename"
       ;;
  esac

  # Try to find the help message
  for opt in $possible_options
  do
    $verbose "Trying $bq$file $opt$eq"
    ($file $opt > $tmp_dir/foo 2>&1) > /dev/null 2> /dev/null && success=: && break
  done

  # If the help message has been found, process it with a2ps
  if test $success; then
    $verbose "Success"
    if test $form_feed; then
       echo ""  >> $tmp_file
    fi
    cat << EOF >> $tmp_file
				card_label($title)
card_title($title)
EOF
    cat $tmp_dir/foo >> $tmp_file
    # Be ready to insert a page break before next argument-program
    form_feed=$print_form_feeds
  else
    echo "$program: could not find help message for $file"
    exit 1
  fi
done

SAVED_IFS="$IFS"
IFS="
"
for command in $commands
do
  IFS="$SAVED_IFS"
  success=
  case $LC_ALL in
    fr) 
       title="Rsultat de \"$command\""
       ;;
    *)
       title="Result of \"$command\""
       ;;
  esac

  (eval $command > $tmp_dir/foo 2>&1) > /dev/null 2> /dev/null && success=:
  # If the help message has been found, process it with a2ps
  if test $success; then
    $verbose "Success"
    if test $form_feed; then
       echo ""  >> $tmp_file
    fi
    cat << EOF >> $tmp_file
				card_label($title)
card_title($title)
EOF
    cat $tmp_dir/foo >> $tmp_file
    # Be ready to insert a page break before next argument-program
    form_feed=$print_form_feeds
  else
    echo "$program: command \"$command\" failed"
    exit 1
  fi
done
IFS="$SAVED_IFS"

# All the programs have been treated.  Call a2ps on the produced file
$a2ps $a2ps_options --footer="$footer" $tmp_file

$RM $tmp_dir
: # exit succesfully
