#!/usr/bin/env bash

# Copyright (C) 2008-2013 Bob Hepple
#
# 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 of the License, 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# http://bhepple.freeshell.org
# $Id: boilerplate,v 1.1 2008/08/15 12:23:04 bhepple Exp $

# A tutorial for argp.sh and a boilerplate for bash scripting

usage() {
    echo "Usage: $PROG [ OPTIONS ] $ARGUMENTS"
    echo "$USAGE"
    echo
    echo "Options:"
}

initialise() {
    PROG=$(basename $0)
    PROGDIR=$(dirname $0)
    VERSION="1.1"
    VERBOSE=""
    ARGUMENTS="COMMANDS"
    SHORT_DESC="short one-liner for man page"
    USAGE="\
$SHORT_DESC.
Longer description for the help page and as a starting point for the man page.
Try running this with the --help or --print-man-page options."

    TMP="/tmp/tmp$$.tmp"
    trap "/bin/rm -f $TMP" EXIT 

    # In case we're running through sudo (which resets the PATH),
    # temporarily push back the PWD and the actual location that this
    # script was found:
    OLDPATH="$PATH"
    PATH="$PATH:$PROGDIR:."

    ARGS="
ARGP_DELETE=quiet
ARGP_VERSION=$VERSION
ARGP_PROG=$PROG
##############################################################   
#OPTIONS:
#name=default sname arg       type range   description
##############################################################   
CD=''         c     ''        b    ''      foobar
SLOT=''       s     'n'       s    ''      option that takes a value
TOKEN=''      t     'number'  s    ''      option that takes a value
LONG=''       ''    'long'    s    ''      a long option without a short one
SILENT=''     ''    ''        b    ''
##############################################################   
ARGP_ARGS=[--] $ARGUMENTS
ARGP_SHORT=$SHORT_DESC
ARGP_USAGE=$USAGE"

    exec 4>&1
    eval $(echo "$ARGS" | argp.sh "$@" 3>&1 1>&4 || echo exit $? )
    exec 4>&-
    PATH="$OLDPATH"
    NEW_ARGS=( "$@" )
}

main() {
    # MAIN PROCESSING
    echo "Remaining args:"
    for arg in "$@"; do echo "\"$arg\""; done

    EXPR="echo $PROG operating on these: $@"
    [ "$VERBOSE" ] && echo $EXPR
    eval $EXPR
}

initialise "$@"
main "${NEW_ARGS[@]}"
 

