#!/bin/bash

# HSBC specific KSH/BASH aliases

# ------------------------------------------------------------------------------
# printing
# ------------------------------------------------------------------------------

# destination printers on 8th floor
export PRINTER1=hp4000tnt818a
export PRINTER2=hp4100nt8106
export PRINTER=$PRINTER1

textprint ()
    {
    local printCmd="${printCmd:-textprint}"
    local err="${printCmd}: specify printer using '-P'"
    [[ " $* " != *" -P"* ]] && printhelp "$err" 1>&2 && return 1
    enscript -fCourier7 -M Letter "$@"
    }

landscape ()
    {
    local printCmd="${printCmd:-landscape}"
    textprint -r "$@"
    }

text2print ()
    {
    local printCmd="${printCmd:-text2print}"
    textprint -2r "$@"
    }
alias 2col=text2print

cppprint ()
    {
    local printCmd="${printCmd:-c++print}"
    text2print -Ecpp "$@"
    }
alias c++print=cppprint
javaprint ()
    {
    local printCmd="${printCmd:-c++print}"
    text2print -Ejava "$@"
    }

printhelp ()
    {
    [ -n "$*" ] && echo "$*" 1>&2 && echo "" 1>&2
    cat 1>&2 <<-EoF
	Print command functions:
	  textprint -P printer args     enscript -fCourier7 -M Letter ...
	  landscape -P printer args     textprint -r ...
	  text2print -P printer args    textprint -2r ... (2 column landscape)
	  2col -P printer args          textprint -2r ... (2 column landscape)
	  ccodeprint -P printer args    text2print -Ecpp ...
	  c++print -P printer args      text2print -Ecpp ...
	
	Printers (Tower 8, 452 Fifth Avenue, New York, NY):
	  \$PRINTER1 = hp4000tnt818a
	  \$PRINTER2 = hp4100nt8106
	  \$PRINTER  = $PRINTER1
	EoF
    }

# ------------------------------------------------------------------------------
# opening new xterm windows (with su or ssh to another machine)
# these rely on the xwin script
# ------------------------------------------------------------------------------

# run su in an xterm
suwin ()
  {
  [[ $# == 0 ]] && echo "Usage: suwin <user> [xwin-args...]" 1>&2 && return
  local user="$1"
  local col
  shift
  case "$user" in
    fidev)      col="-colset darkblue"  ;;
    fi_view)    col="-colset cinnamon"  ;;
    *)          col="-colset darkgreen" ;;
  esac
  xwin $col -T "$user@`hostname`" -n "($user@`hostname`)" -e su - $user
  }

# function used by others to open an xterm running ssh
_nodeusercolorwin ()
  {
  local x
  local node="$1"
  local user="$2"
  local color="$3"
  local title="$user@$node"
  [[ -n $DISPLAY ]] && x=+x
  set -- $x -l "$user" "$node"
  xwin -colset "$color" -T "$title" -n "($title)" -e ssh "$@"
  }

# various accounts
fstprod () { _nodeusercolorwin nysuntrade03 fstprod gold; }
fi_view () { echo fiview123; _nodeusercolorwin nysuntrade03 fi_view cinnamon; }
fidev   () { echo fidev01; _nodeusercolorwin nysuntradedev01 fidev darkblue; }
fstqa   () { echo fstqa123; _nodeusercolorwin njsuntrade03 fstqa darkgreen; }
fstuat  () { echo fstuat123; _nodeusercolorwin njsuntrade05 fstuat green; }

# attaching to various machines: you need a user account
nysuntrade03 ()
    {
    [[ -z "$1" ]] && { echo "nysuntrade03 <user>" 1>&2 && return 1; }
    echo "nysuntrade03: Forest applications main server" 1>&2
    _nodeusercolorwin nysuntrade03 "$1" green
    }

njsuntrade03 ()
    {
    [[ -z "$1" ]] && { echo "njsuntrade03 <user>" 1>&2 && return 1; }
    echo "njsuntrade03: Forest applications BACKUP server" 1>&2
    _nodeusercolorwin njsuntrade03 "$1" blue
    }

njsuntrade05 ()
    {
    [[ -z "$1" ]] && { echo "njsuntrade05 <user>" 1>&2 && return 1; }
    echo "njsuntrade05: ???" 1>&2
    _nodeusercolorwin njsuntrade05 "$1" blue
    }

nysuntradedev01 ()
    {
    [[ -z "$1" ]] && { echo "nysuntradedev01 <user>" 1>&2 && return 1; }
    echo "nysuntradedev01: Forest applications DEVELOPMENT" 1>&2
    _nodeusercolorwin nysuntradedev01 "$1" white
    }

# ------------------------------------------------------------------------------
# FTPing to production machines as different users
# ------------------------------------------------------------------------------

# used by other functions to initiate an FTP session with a set user name
# it will create then delete the file $HOME/.netrc (it fails if it's there)
_ftp_cmd_hst_usr ()
    {
    local err
    err="bad args $*"
    [[ $# == 3 ]] || { echo "_ftp_cmd_hst_usr: $err" 1>&2 && return 1; }

    local cmd="$1" hst="$2" usr="$3"
    err="$cmd: must run from a tty"
    tty -s || { echo "$err" 1>&2 && return 1; }

    local _net="$HOME/.netrc"
    err="$cmd: $_net exists - won't overwrite"
    [[ -f $_net ]] && { echo "$err" 1>&2 && return 1; }

    touch $_net
    chmod 600 $_net
    echo "machine $hst login $usr" > $_net
    ( trap "echo '$cmd: [trap] deleting $_net'; rm -f '$_net' 1>&2" TERM
      ftp $hst )

    [[ -f $_net ]] && echo "$cmd: deleting $_net" 1>&2 && rm -f $_net
    }

# attach to nysuntrade03 as fi_view
ftp_fi_view ()
    {
    _ftp_cmd_hst_usr ftp_fi_view nysuntrade03 fi_view
    }

# attach to nysuntrade03 as fstprod
ftp_fstprod ()
    {
    _ftp_cmd_hst_usr ftp_fstprod nysuntrade03 fstprod
    }

# ------------------------------------------------------------------------------
# Fixing databases
# ------------------------------------------------------------------------------

isqlCall ()
    {
    local DB="$1" User="$2" Pass="$3"
    local isqlCmd="isql -S$DB -U$User -P$Pass -w1000"
    local yesno=n
    local tty=`( # test stdin and stderr for terminal access
                 # since this command may be using stdin for command input
                 if tty -s; then echo y; else exec 0<&2; tty -s && echo y; fi
               )`
    shift 3
    if [[ $tty = y ]]; then
      echo "You want to change the $DB database with command" > /dev/tty
      echo "  $isqlCmd $*" > /dev/tty
      printf "Are you sure (y/n)? " > /dev/tty
      read yesno < /dev/tty
    else
      yesno=y
    fi
    if [[ $yesno = [Yy]* ]]; then
      # echo $isqlCmd "$@"              # -- debugging
      # tty -s || head -5               # -- debugging
      $isqlCmd "$@"
    else
      [[ $tty = y ]] && echo "Command not executed" > /dev/tty
    fi
    }

alias idev='isqlCall FIDEV fi_dev fi_dev01'
alias istgdbo='isqlCall FSTSTAGENJ tempdbo tempdbo01'

# log directories
_btsfeed=$FORESTLOGHOME/BTSFeed
_ticketprocessor=$FORESTLOGHOME/TicketProcessor
_newtfd=$FORESTLOGHOME/NewTFD
_bblink=$FORESTHOME/bblink
_cfg=$FORESTCFGHOME
_exe=$FORESTEXEHOME
_dal=$FORESTHOME/log/iris
