#!/bin/bash
Version=1.06
# (lower case version here, in order not to interfere with Version in other scripts using me)

:<<'DOC'
= wdbashfunctions - my Bash extra's for coloring, help, installation
= Synopsis
source wdbashfunctions		for use in scripts or ~/.bashrc
wdbashfunctions -[h|H|V]	for help on the command line

== Options
-h	print short help and exit
-H	display full documentation with less and exit
-V	print version and exit

= Description
wdbashfunctions defines functions and environment variables which I
use in many, if not all, of my Bash scripts. It is meant to be sourced in
|~/.bashrc|.

= Color functions
There are 32 3-letter commands which print their arguments, each on a new line, in color.
The commands |Bla|, |Red|, |Gre|, |Yel|, |Blu|, |Mag|, |Cya|, and |Whi| print
their arguments, separated with newlines, in black, red, yellow, blue,
magenta, cyan and white respectively.
The same commands with their second character capitalized print the
same colors in a lighter variant.
By capitalizing the third character, the arguments are printed in bold face.
By captializing all characters, the arguments are printed in bold face
in the light color variant.

If the first argument is |on|, a second 3-letter argument is expected
which specifies the background color. Capitalizing the third (bold
face) character has no significance here of course. Instead, it will
cause the argument to blink.

In short:
  Red -> red
  REd -> light red
  RED -> light bold red
  Red on Yel -> red on yellow
  Red on YEl -> red on light yellow
  Red on YEL -> blinking red on light yellow
  RED on YeL -> blinking light bold red on yellow

= Color variables
For each of the color functions, an environment variable with the same
name is defined and exported. You use these when you want to different
colors in the same string. For example, the following prints several
color names, each in their own color:

   echo $Red red $REd light red $RED bold light red $BlU bold blue

= Utility functions

== die
prints its arguments in light red to standard error, each on a new
line, and exits.

== Warn
prints its arguments in magenta, each on a new line, to standard error.

== warn
prints its arguments in magenta, each on a new line, to standard
error, but only if |$verbose| is defined and has the value |true|.

== helpsrt
prints short help for the script

== helpall
prints full documentation of the script

== instscr
installs the script in $PREFIX/bin, with html documentation in
|$PREFIX/bin/html| and a man page in |$PREFIX/bin/man/man1|. This function
is not for use by normal users, the administrator call it /via/ the
undocumented |-I| option. It needs two other scripts: |instscript|
and |gendoc|.

== docolor
Enable coloring; this is the default.

== nocolor
Disable all coloring. Sets all coloring escape sequences to the empty string.

= Author
[Wybo Dekker](wybo@dekkerdocumenten.nl)

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

_colnames=(Bla Red Gre Yel Blu Mag Cya Whi)
_setxtcolor(){ # color (0-7), light, bold
   local name=${_colnames[$1]} l=$1 b='' foo
   if $2; then name=$(foo=${name:1};echo "${name:0:1}${foo^}"); ((l+=8)); fi
   if $3; then name=$(foo=${name:2};echo "${name:0:2}${foo^}"); b='1;'; fi
   eval "$name() {
     local on='' i
     if [[ \$1 == on ]]; then
        eval on=\\\$\$2
        on=\${on/38/48}
        on=\${on/1;48/5;48}
        shift 2
     fi
     for i; do 
        printf \"\$$name\$on%s[0m\n\" \"\$i\"
     done
   }
   $name=\"[${b}38;5;${l}m\";"
   # shellcheck disable=SC2163
   export "$name" &&  export -f "$name"
}
    die() { local i; for i; do RED "$i"; done 1>&2; exit 1; }
   Warn() { local i; for i; do Mag "$i"; done 1>&2; }
   warn() { $verbose && Warn "$@"; }
helpsrt() { sed -n '/^= Syno/,/^= Desc/p' "$0"|sed 's/^=* //;1d;$d'; exit; }
            # shellcheck disable=SC2154
helpall() { sed -n "/^:<<'DOC'/,/^DOC/p" "$0"|sed -n '1d;$d;p'|
            less -Ps"$Myname-${Version/./·} documentation - type h for help, q to quit."
            exit
          }
instscr() { instscript --zip --pdf "$Myname"; exit; }
nocolor() { local i p q r
   for i in "${_colnames[@]}"; do
      p=${i:0:1} q=${i:1:1} r=${i:2:1}
      eval "$i='' $p${q^}$r='' $p$q${r^}='' $p${q^}${r^}=''" 
   done
   Nor=''
}
docolor() {
   local l b c
   for l in false true; do
      for b in false true; do
         for c in {0..7}; do
            _setxtcolor $c $l $b
         done
      done
   done
   Nor="[0m"
}

if [[ ${0##*/} == "${0##*/}" ]]; then
   while getopts "hHVI" opt; do
      case $opt in
      (V) echo "$Version"
          exit
          ;;
      (h) helpsrt
          ;;
      (H) helpall
          ;;
      (I) Myname=${0##*/} instscr
          ;;
      (*) break
      esac
   done
fi

export -f die Warn warn helpsrt helpall instscr nocolor
export Nor
verbose=false
docolor

unset version l b c opt
