# $Id: libsh,v 1.2 2007/03/22 16:51:35 tlaronde Exp $
#
# Miscellanei routines used by scripts
#
#  Copyright 2004 Thierry LARONDE <tlaronde@polynum.com>
#  All rights reserved. 
#  
#  This work is under the KerGIS Public Licence v1.0
# 
#  See the COPYRIGHT file at the root of the source directory or see
#  http://www.kergis.org/licence.txt for complete information.
# 
# !!!THIS SOFTWARE IS PROVIDED ``AS IS'' WITHOUT ANY WARRANTIES!!! 
#                      USE IT AT YOUR OWN RISK 

#========== INITIALIZATIONS

# Name by which this script was invoked.
#
PROGRAM=`basename $0`

TMPDIR=${TMPDIR:-/tmp}
tmpdir=$TMPDIR/$$
! [ -d $tmpdir ] && mkdir $tmpdir

# Remove temporary files on HUP, INT, QUIT, PIPE, TERM.
#
trap " debug F 'Unexpected event. Try -d to debug'; clean_tmp; exit 1;"  HUP INT QUIT PIPE TERM

#========== SUBFUNCTIONS

clean_tmp()
{
	rm -fr $tmpdir
}


#========== SUBFUNCTIONS

# debug writes info (L for LOG), warning (W for WARNING), error
# (E for ERROR) or fatal (F for FATAL) for a programming unrecoverable
# error followed by the message on stderr.
#
debug()
{
	case $1 in
	  A) echo "->" | tr -d '\n' 1>&2;;
	  E) echo "$program ERROR: " | tr -d '\n' 1>&2;;
	  F) echo "$program FATAL: " | tr -d '\n' 1>&2;;
	  L) echo "	" | tr -d '\n' 1>&2;;
	  W) echo "$program WARNING: " | tr -d '\n' 1>&2;;
	  *) echo "$1: " | tr -d '\n' 1>&2;;
	esac
    echo "$2" 1>&2
    return
}

