#!/bin/bash
# 
# banner.sh, useful for drawing colored banners across the screen.
#
# usage: 
#
#  banner.sh [-b] [-B] [-d] [-r NUM] [-nc] [-fg CLR] [-bg CLR] [-l] \
#            [-m NUM] [-u] [-U] [-c]  TEXT [TEXT2...]
#
# example usage:
#  
#  banner.sh -U -m 2 -b -fg 38  -c "Welcome to $(hostname -s)"  ""  \
#    "Login for: $USER   $(date)"
#
# Note: You must have a space between any options and the corresponding 
# value!!! Be sure to quote any white space. Text to be drawn should 
# come last on command line, though multiple strings can be used (each 
# will be a separate line).
#
# The behavior of underlining, bold and brightness attributes depend on the
# capabilities of the terminal, and may do something else entirely in some
# situations, or nothing at all.
#
#
# TODO: Use getopts. Error testing for valid colors, etc. Document 
# command line options better.
#
#
# Hal Burgiss <hal@foobox.net>  Wed 07/24/02 10:16:12 PM
#
# License: GPL (see http://gnu.org)
#
#######################################################################


# BRIGHT='^[[01m'
# NORMAL='^[[0m'
# sample bash-prompt
#PS1=$BRIGHT$YELLOW'\u:'$NORMAL'/\t\w\$ '


debug=false

# Default foreground color. Over-ride on command line.
fg=30

# Default background color. Over-ride on command line.
bg=46

# Where to put first row (can be blank of course).
banner_row=0

# What row to start with.
top=0

# Left margin offset for non-centered text. Use -c to center text and
# over-ride left justification.
margin=1

# Underline. Set this via command line, -u or -U.
underline=0

# Get command line stuff. There is *no* testing for validity.
for i in $*; do

     if [ "$i" = "-b" ]; then
     # Turn on Bold text.
       bold=1 ; shift 
     elif [ "$i" = "-B" ]; then
     # Turn on Bright text.
       bright=1 ; shift 
     elif [ "$i" = "-d" ]; then
     # Turn on debugging.
       debug=true ; shift
     elif [ "$i" = "-r" ]; then
     # Row to start coloring. Default is first row.
       top=$2 ; shift ;shift
       ((banner_row+=$top))
     elif [ "$i" = "-nc" ]; then
     # Don't clear the screen, which is the default.
       noclear=1 ; shift
     elif [ "$i" = "-fg" ]; then
     # Set foreground color.
       fg=$2 ; shift ; shift
     elif [ "$i" = "-bg" ]; then
     # Set background color.
       bg=$2 ; shift ; shift
     elif [ "$i" = "-m" ]; then
     # Set left margin.
       margin=$2 ; shift ; shift
     elif [ "$i" = "-U" ]; then
     # Underline the last row only, for effect.
       underline=2 ; shift 
     elif [ "$i" = "-u" ]; then
     # Underline all rows for a jailbird effect (ugly).
       underline=1 ; shift 
     elif [ "$i" = "-l" ]; then
     # List colors and exit, because I forget :) From ABS HOWTO.
       echo "\
     Color   Foreground Background 
                                
     black   30         40         
     red     31         41         
     green   32         42         
     yellow  33         43         
     blue    34         44         
     magenta 35         45         
     cyan    36         46         
     white   37         47 "
       exit
     fi

done

$debug && echo num args: $# && echo $* && read

# Make sure there is *something* left on command line.
[ -z "$*" ] && echo "cat /dev/null >/dev/brain" && exit 1

# Get terminal window sizes.
size=( $(stty size) )
ht=${size[0]}
col=${size[1]}

$debug && \
echo comp: $comp &&\
echo columns: $col &&\
echo len: $strlen &&\
echo pad: $pad &&\
echo padded len: $(($col/2-$pad)) &&\
exit

# Function used to draw our pretty colored rows.
drawline() {
  printf "%+${col}s" '' 
}

# Clear screen unless -nc is specified.
[ -z "$noclear" ] && clear

# Turn on the color.
echo -en "\033[${fg};${bg}m"

# Bold? (-b)
[ -n "$bold" ] && echo -en "\033[1m"

# Underline everything (-u).
[ $underline -eq 1 ] && echo -en "\033[4m"

# Bright text? (-B)
[ -n "$bright" ] && echo -en "\033[7m"

# Set the top row, if specified on command line.
[ -n "$top" ] && tput cup $top 0


# Main loop to parse the remainder of the command line, and draw it.
while [ -n "$*" ]; do
  
     # offset from left margin
     offset=$margin
     
     # -c on command line = 1, then this row's text will be centered.
     center=0
     [ "$1" = "-c" ] && center=1 && shift

     # Length of text to be displayed, needed for centering.
     strlen=$(echo -n $1 |wc -c)

     # offset from center of screen, needed for centering (-c).
     pad=$(($strlen/2))

     # Reset offset and set the col start pos, if we are centering our 
     # text.
     [ $center -eq 1 ] && offset=$(($col/2-$pad)) 
     
     # Underscore the last line only if -U option specified.
     [ $underline -eq 2 ] && [ $# -eq 1 ] && echo -en "\033[4m"

     # Draw with color.
     drawline
     
     # Reset cursor pos. for next text string.
     tput cup $banner_row $offset
     
     # Now draw text.
     printf "$1\n"
     shift || break
     ((banner_row++))

done

# Reset.
echo -en "\033[0m"

#$debug && echo \
#.........1.........2.........3.........4.........5.........6.........7.........8

# Stick cursor on last row.
((--ht)) ; tput cup $ht 0

#--- eof banner.sh
