#!/bin/bash

Version=1.11
Myname="${0##*/}"
Instdir=/usr/local

doc=$(cat \
<<'DOC'
= instscript - install a script and its gendoc documentation

= Synopsis
instscript [options] scriptname 	

= Options
-h,--help	print short help and exit
-H,--Help	print full documentation via less and exit
-V,--version	print version and exit
-d,--debug	set debug flag: keep intermediate files for inspection
-e,--extras=X	set list of extra files to be zipped to X (comma separated)
-z,--zip	create zip file for CTAN

= Description
|instscript| installs a (bash, ruby, perl, ...) script, for which
the documentation is maintained in the gendoc format.
The variable |Instdir| tells where things are
installed:

The script is installed in |$Instdir/bin|. Its documentation
is extracted and placed in |$Instdir/html| (for the html
documentation), in |$Instdir/pdf| for the PDF documentation
and in |$Instdir/man/man1| (for the man page). |instscript|
must be executed in the source directory of the script.

The script must have valid gendoc documentation - see
[github](github.com/wybodekker/gendoc). The first
paragraph that is found under the |= Description| header is
saved in a |README.md| file, which should be part of the
source: it is used by github. Bars (|) will be stripped
from that paragraph.

With the |--zip| option, a zip file containing the script,
a README file, a man page, and html and pdf documents is created.

= Author
[Wybo Dekker](wybodekker@me.com)

= Copyright
Released under the [GNU General Public License](www.gnu.org/copyleft/gpl.html)

= See also
gendoc(1) scriptinfo(1) 
DOC
)

# --- colors, help from embedded doc, die ---
[ -t 1 ] && { Red=$'\e[1;31m' Mag=$'\e[1;35m' Nor=$'\e[0m'; } || Red='' Mag='' Nor=''
nv="-$Myname-${Version/./\\.} (press h for help, q to quit)"
helpsrt() { sed -n '/^= Synopsis/,/^= Desc/{//d;p}' <<<"$doc"; exit; }
helpall() { less -P"$nv" <<<"$doc"; exit; }
    die() { printf "%s$Nor\n" "${@/#/$Myname: $Red}" >&2; exit 1; }
   Warn() { printf "%s$Nor\n" "${@/#/$Myname: $Mag}" >&2; }

:<<'DOC' #----------------------------------------------------------------------
= handle_options
synopsis:	 handle_options "$@"
description:	handle the options.
globals used:	 Myname Version
globals  set:	 args zip
DOC
#-------------------------------------------------------------------------------
handle_options() {
   if ! options=$(getopt \
      -n "$Myname" \
      -o hHVzde:I \
      -l help,Help,version,zip,debug,extras: -- "$@"
   ); then exit 1; fi
   eval set -- "$options"
   zip=false debug=false
   while [ $# -gt 0 ]; do
      case $1 in
      (-h|--help)     # print short help and exit
	   helpsrt
	   ;;
      (-H|--Help)     # print full documentation via less and exit
	   helpall
	   ;;
      (-V|--version)  # print version and exit
	   echo $Version
	   exit
	   ;;
      (-d|--debug)    # set debug flag: keep intermediate files for inspection
	   debug=true
	   shift
	   ;;
      (-e|--extras)   # set list of extra files to be zipped to X (comma separated)
	     IFS=, read -ra extras <<<"$2"
	   shift 2
	   ;;
      (-z|--zip)      # create zip file for CTAN
	   zip=true
	   shift
	   ;;
      (-I) $0 "$0" || die "the -I option is for developers only"
	   exit
	   ;;
      (--) shift
	   break
	   ;;
      (*)  break
	   ;;
      esac
   done
   args=( "$@" )
}

[[ -d $Instdir ]] || die "The variable Instdir does not point to a directory"

handle_options "$@"
set -- "${args[@]}"
(( $# == 1 )) || die "I need 1 input file"

S="$(realpath "$1")"
S="${S#"$PWD"/}"
[[ -n $S ]] ||
   die "I need the name of a script in the current directory"
[[ $S =~ / ]] && die 'I need a name, not a path'
[[ -e $S ]] ||
   die "./$S: not found"

# use scriptinfo to gather information from the script:
scriptversion='' short='' type=''
for i in short type scriptversion intro changes; do
   read -r ${i?}
done < <(scriptinfo --brief "./$S")

# is all info there?
[[ -n $scriptversion ]] || die "$S has no version"
[[ -n $short ]]         || die "found no line starting with: = $S - ..."
[[ -n $type ]]          || die "Could not find type of $S in shebang line"

# be sure that the script is executable:
chmod a+x "$S"

# install the script in $Instdir/bin:
cp -af "$S" "$Instdir/bin"

# generate the documentation:
gendoc "$Instdir/bin/$S" || die "gendoc failed"

# generate the README.md file
scriptinfo -m "$S" >README.md

# if a zip is required, generate it:
if $zip; then
   cp -f "$Instdir/pdf/$S.pdf" .
   cp -f "$Instdir/html/$S.html" .
   cp -f "$Instdir/man/man1/$S.1" .
   filestozip=("$S" "$S.pdf" "$S.html" "$S.pdf" "$S.1" README.md)
   filestozip+=("${extras[@]}") 
   cd ..
   zip -Dq "$S/$S-$scriptversion.zip" "${filestozip[@]/#/backup/}"
   $debug || rm "$S/$S".{html,pdf,1}
fi
