#! /bin/bash -e

# Warning:  This script deletes potentially important files.  By design,
# it fails and does nothing unless invoked with the '-f' force option.
# Invoke the script with discretion.
#
# This shell script cleans the debian package files from the parent of
# the source's top directory, including
#
#     source-version.orig/
#     source_version[.-]*

SELF=cleandeb

function usage {
  cat <<END
Warning:  This script deletes potentially important files.  By design,
it fails and does nothing unless invoked with the '-f' force option.
Invoke the script with discretion.

usage: debian/$SELF [-fh]
    -f  force (this option is required against accidental invocation)
    -h  print this help message and exit

END
}

# Require that the script be invoked from the top source directory.
if [[ ! ( $PWD/debian/$SELF -ef $0 ) ]] ; then
  echo 1>&2 "$0: must be invoked from the top source directory"
  exit 1
fi

# Read options from the command line.
FORCE=0
HELP=0
while getopts 'fh' OPT ; do
  [[ $OPT == 'f' ]] && FORCE=1
  [[ $OPT == 'h' ]] && HELP=1
  if [[ $OPT == '?' ]] ; then
    usage 1>&2
    exit 1
  fi
done

if [[ $HELP == 1 ]] ; then
  usage
elif [[ $FORCE == 0 ]] ; then
  usage 1>&2
  exit 1
else

  DIRNAME=$(     dirname  $PWD                    )
  BASENAME=$(    basename $PWD                    )
  DEBBASENAME=$( echo $BASENAME | sed -e 's/-/_/' )

  # Delete files!
  rm -Rf $DIRNAME/$BASENAME.orig $DIRNAME/$DEBBASENAME[.-]*

fi

