#! /bin/sh
# instcmds - extract shell commands from the Makefile
# Copyright 1999  Jochen Voss

version="0.3"

usage()
{
  cat <<EOF
Usage: instcmds [options] ARGS

Extract (un-)installation commands from the Makefile.
This script may only be interesting to maintainers and
requires GNU make.

The options are
  --help            show this message
  --install         Show the install commands (default)
  --type=TYPE       One of "pre", "normal" (default), and "post"
  --uninstall       Show the uninstall commands
  --version         show the version information

The ARGS are passed directly to the make command.

Please report bugs to <voss@mathematik.uni-kl.de>.
EOF

  exit $1
}

type=normal
while test $# -gt 0; do
  case "$1" in
  -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  -*) optarg= ;;
  *) break ;;
  esac

  case "$1" in
    --version)
      cat <<EOF
Instcmds $version
Copyright (C) 1999 Jochen Voss
Instcmds comes with NO WARRANTY, to the extent permitted by law.
EOF
      exit 0
      ;;
    --help)
      usage 0
      ;;
    --install)
      un=
      UN=
      ;;
    --uninstall)
      un=un
      UN=UN
      ;;
    --type=*)
      type=$optarg
      ;;
    *)
      usage 1
      ;;
  esac
  shift
done

unique=47272088
pre_label=$unique-ignore
normal_label=$unique-ignore
post_label=$unique-ignore
case "$type" in
  pre)    pre_label=$unique-fetch ;;
  normal) normal_label=$unique-fetch ;;
  post)   post_label=$unique-fetch ;;
esac

make "$@" --no-print-directory -n ${un}install -o all \
  PRE_${UN}INSTALL=$pre_label \
  NORMAL_${UN}INSTALL=$normal_label \
  POST_${UN}INSTALL=$post_label \
| sed -n -e "/^[ \t]*$unique-fetch[ \t]*\$/, /^[ \t]*$unique-ignore[ \t]*\$/! d" \
	-e "/^[ \t]*$unique-\\(fetch\\|ignore\\)[ \t]*\$/ d" \
	-e "p" -
