#!/bin/bash
# Copyright (C) 2013 Ben Asselstine
# 
# 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 3 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, see <http://www.gnu.org/licenses/>.

if [[ "$BASH_ENV" == *\.lu-shrc* ]]; then
  progname=$(basename $0)
else
  progname="licensing $(basename $0)"
fi

showusage() {
  cat <<EOF
Usage: ${progname} [-?] [-l LICENSE] [-s COMMENTING-STYLE] [-c NAME-AND-YEAR] FILE...
EOF
}

showhelp() {
 styles=`licensing --list-styles`
 licenses=`licensing --list-licenses`
  cat <<EOF
Usage: ${progname} [OPTION...] FILE...
A simple wrapper script for writing license notices to source files.

Supported Licenses: $licenses no-license

Supported Comment Styles: $styles no-style

  -c NAME-AND-YEAR           specify the copyright holder
  -l LICENSE                 specify the license
  -s COMMENTING-STYLE        specify the comment style
  -n                         don't retain a backup file
  -?, --help                 give this help list

This wrapper script is for users who want to do everything on single line.
When greater control is required, try running the licensing program and the
choose, copyright, and apply commands individually.
EOF
}

if [[ "$#" = 1 && "x$1" = x--help ]]; then
  showhelp
  exit 0
fi

if [[ "$#" = 0 ]]; then
  showhelp
  exit 0
fi
license=""
style=""
copyright=""
backup=""

readopt='getopts $opts opt;rc=$?;[ $rc$opt == 0? ]&&exit 1;[ $rc == 0 ]||{ shift $[OPTIND-1];false; }'

opts=n?l:s:c:

# Enumerating options
declare -a copyright_holders
count=0
while eval $readopt
do
  case "$opt" in
  \?)
    showhelp
    exit;;
  n)
    backup="--no-backup"
    ;;
  l)
    license="$license $OPTARG"
    ;;
  s)
    style=$OPTARG
    ;;
  c)
    licensing copyright --dry-run -- $OPTARG 2>/dev/null >/dev/null
    retval=$?
    if [ "x$retval" == "x0" ]; then
      copyright_holders[$count]=`echo $OPTARG`
      count=`expr $count + 1`
    else
      licensing copyright --dry-run -- $OPTARG
      exit 1
    fi
    ;;
  esac
done

# Validating arguments
for arg
do
  if [ ! -f $arg ]; then
    echo ${progname}: cannot open $arg for writing
    exit 1
  fi
done

if [ "x$license" == "x" ]; then
  echo "${progname}: you must supply a license with -l"
  echo "Try '${progname} --help' for more information."
  exit 1
fi
if [ "x$style" == "x" ]; then
  echo "${progname}: you must supply a commenting style with -s"
  echo "Try '${progname} --help' for more information."
  exit 1
fi
if [ "x$count" == "x0" ]; then
  echo "${progname}: you must supply a copyright holder with -c"
  echo "Try '${progname} --help' for more information."
  exit 1
fi
if [ "$#" == "0" ]; then
  echo "${progname}: no files specified"
  echo "Try '${progname} --help' for more information."
  exit 1
fi

#copy the old files away
oldconf=`mktemp -d`
cp ~/.licenseutils/* $oldconf 2>/dev/null

rm -f ~/.licenseutils/top-line  2> /dev/null && \
rm -f ~/.licenseutils/copyright-holders 2> /dev/null && \
rm -f ~/.licenseutils/project-line  2> /dev/null && \
rm -f ~/.licenseutils/extra-line  2> /dev/null && \
rm -f ~/.licenseutils/selected-licenses 2>/dev/null && \
rm -f ~/.licenseutils/license-notice 2>/dev/null

licensing new-boilerplate --quiet 

count=`expr $count - 1`
for i in `seq 0 $count`; do
  licensing copyright --quiet -- ${copyright_holders[$i]} 
done
licensing choose --quiet -- $license $style && licensing apply $backup -- $* 
retval=$?
licensing new-boilerplate --quiet

#put the old files back
mv $oldconf/* ~/.licenseutils/ 2>/dev/null
rmdir $oldconf 2>/dev/null

exit $retval
