#!/bin/bash
#<pre><b>
# Script     : shdr
# Version    : 1.0
# Author     : Roland Rashleigh-Berry
# Date       : 28-Nov-2003
# Purpose    : To create a standard shell script header
# SubScripts : getname
# Notes      : This shell script generates a shell script header for a script
#              in the current directory and places it at the top of the code if
#              the script exists. If invoked without a following script name 
#              then the user will be prompted to enter the name. 
# Usage      : shdr
#              shdr scriptname
#===============================================================================
# PARAMETERS:
#-pos- -------------------------------description-------------------------------
#  1   script file name (optional)
#===============================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description------------------------
# 
#===============================================================================
# This is public domain software. No guarantee as to suitability or accuracy is
# given or implied. User uses this code entirely at their own risk.
#===============================================================================

# if no script name supplied then prompt for it
if [ $# -lt 1 ]
then
  echo -n "Enter script name: "
  read scripname
else
  scripname=$1
fi

# make sure the file name has no extension
scripname=`echo $scripname | awk -F. '{print $1}'`

# prompt for the purpose of the script
echo -n "Enter script purpose: "
read purpose

# find out the name of the person invoking this script
author=`getname`

# format today's date in the form DD-Mon-YYYY
date=`date '+%d-%b-%Y'`


# copy the following lines out to a file until "FINISH" is encountered
cat > $HOME/shdr.tmp << FINISH
#!/bin/bash
# Script     : $scripname
# Version    : 1.0
# Author     : $author
# Date       : $date
# Purpose    : $purpose
# SubScripts : none
# Notes      : none
# Usage      : $scripname xxxxxxxx
#              $scripname 
#===============================================================================
# PARAMETERS:
#-pos- -------------------------------description-------------------------------
#  1   
#===============================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description------------------------
# 
#===============================================================================

# Edit the test below for the number of expected parameters and put out
# a usage note if an incorrect number supplied. Remove if not required.
if [ \$# -lt 1 ] ; then
  echo "Usage: $scripname xxxxxx" 1>&2
  exit 1
fi

FINISH

# if the script already exists then add the header to the top,
# otherwise create the script out of the generated header
# and set it to be an executable file
if [ -f $scripname ]
then
  cat $HOME/shdr.tmp $scripname > $HOME/shdr2.tmp
  rm -f $scripname
  cat $HOME/shdr2.tmp > $scripname
  rm -f $HOME/shdr2.tmp
else
  cat $HOME/shdr.tmp > $scripname
  echo "created $scripname"
fi

chmod +x $scripname

# delete the temporary file
rm -f $HOME/shdr.tmp
