#! /bin/sh
# -*- ksh -*-
# 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

# $Id: card.in,v 1.10 1998/08/05 15:04:42 demaille 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.

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

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

# 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=:
version_short="card $card_version (GNU a2ps 4.12)"

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.

Report bugs to <bug-a2ps@gnu.org>"

version="$version_short

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: http://www.inf.enst.fr/~demaille/a2ps/"

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

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

# Parse command line arguments.
while test $# -gt 0; do
  
  # Handle --option=value by splitting apart and putting back on argv.
  case "$1" in
    --*=*)
      opt=`echo "$1" | sed -e 's/=.*//'`
      val=`echo "$1" | sed -e 's/[^=]*=//'`
      shift
      set - "$opt" "$val" "$@"
      ;;

  esac
  
  # This recognizes --heltch as --help.  So what.
  case "$1" in
    --h*|-h)  echo "$usage";   exit 0  ;;
    --v*|-v)  echo "$version"; exit 0  ;;
    -s|-q|--q*|--s*) verbose=: ;;
    -D|--debug) debug=: ;;
    
    --lan*|-l) shift; LC_ALL=$1                     ;;
    --com*|-c) shift; commands="$commands
$optarg" ;;
    --no-*|-f) print_form_feeds=                    ;;
    -*)        a2ps_options="$a2ps_options $1"      ;;
    *)         nonopt="$nonopt $1"                  ;;
  esac
  shift
done

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 $version_short" ;;
  *)  footer="Generated by card version $version_short" ;;
esac

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

for file in $nonopt
do
  success=
  filename=`echo "$file" | sed -e 's/[^\/]*\///g'`
  $verbose "Working on \`$filename'"
  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 \`$file $opt'"
    ($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 -n "$success"; then
    $verbose "Success"
    if test -n "$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 -n "$success"; then
    $verbose "Success"
    if test -n "$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 || exit 1

exit 0
