LC_ALL=en_US
export LC_ALL

LANG=de_DE.UTF-8
export LANG

LC_COLLATE=C
export LC_COLLATE

#==============================================================================
function mkConfigErr() # creates an error file under the given file name
#------------------------------------------------------------------------------
{
	CONFIG_ERR=$1
	USER=$2

	if [ ! -e $CONFIG_ERR ]; then
		CONFIG_DIR=`dirname $CONFIG_ERR`
		if [ ! -d $CONFIG_DIR ]; then
			mkdir $CONFIG_DIR
			chown $USER:${USER:0:5} $CONFIG_DIR
		fi
	fi

	if [ -n "$USER" ]; then
		if [ -f $CONFIG_ERR.gz ]; then
			chown $USER:${USER:0:5} $CONFIG_ERR.gz
			chmod 600 $CONFIG_ERR.gz
		fi
		chown $USER:${USER:0:5} $CONFIG_ERR
		chmod 600 $CONFIG_ERR
	fi
}

#==============================================================================
function date2de() # date conversion YYYY-MM-DD -> DD.MM.YYYY
# converts sortable date (YYYY-MM-TT) to German date (TT.MM.YYYY) 
#------------------------------------------------------------------------------
{
	aDate=$1
	date -d "$aDate" +"%d.%m.%Y"
}

#==============================================================================
function convGermanToSortableDate() # date conversion
# converts German date (TT.MM.YYYY) to sortable date (YYYY-MM-TT)
#------------------------------------------------------------------------------
{
	aDate=$1
	echo "${aDate:6:4}-${aDate:3:2}-${aDate:0:2}"
}


#==============================================================================
function calcUntilDate() # calculates until date due to notice period
# calcUntilDate NOTICE_RECEIVED_DATE DAYS SERVICE_PERIOD_START_DATE
{
	aReceivedDate=$1
	nDays=$2
	aStartDate=$3

	aUntilDate=`date -d"$aReceivedDate + ${nDays}days" +%Y-%m-%d`
	while [ "${aUntilDate:8:2}" != "${aStartDate:8:2}" ]; do
		let nDays=nDays+1
		aUntilDate=`date -d"$aReceivedDate + ${nDays}days" +%Y-%m-%d`
	done

	echo $aUntilDate
}

#==============================================================================
function chkInteractive() # checks for interactive session running on a user term
# (where debug info might be useful). Returns 1 iff interactive.
#------------------------------------------------------------------------------
{
	tty | grep 'not a tty' >/dev/null 2>&1
	echo $?
}

#==============================================================================
function getHomeDir() # returns the home directory of a user
#------------------------------------------------------------------------------
{
	aUser=$1
	aHomeDir=`grep "^$aUser:" /etc/passwd | cut -d':' -f 6`
	echo $aHomeDir
}

#==============================================================================
function convISOtoIDN() # converts ISO-8859-1 domain-name to IDN
#------------------------------------------------------------------------------
{
	CHARSET=ISO-8859-1
	echo "$1" | idn --quiet -a 2>/dev/null
}

#==============================================================================
function convIDNtoISO() # converts IDN domain-name to ISO-8859-1
#------------------------------------------------------------------------------
{
	aDom=$1
	if [ "${aDom:0:4}" != "xn--" ]; then
		echo $aDom
	else
		aDom=${aDom:4}
		aSub=`echo $aDom | cut -d'.' -f1`
		aTLD=`echo $aDom | cut -d'.' -f2`

		echo `CHARSET=ISO-8859-1; echo "$aSub" | idn --quiet -d 2>/dev/null`.$aTLD
	fi
}

#==============================================================================
function getFileSystem4Dir() # gets the filesystem name for a directory in lower case
#------------------------------------------------------------------------------
{
	mount -l | grep "on $1" | cut -d' ' -f5 | tr A-Z a-z
}

#==============================================================================
function getDomSOA() # get DNS master from SOA or 'NXDOMAIN' if not connected
#------------------------------------------------------------------------------
{
	aSOA=$(host -t SOA $1)
	if [ -n "$(echo "$aSOA" | fgrep ' SOA ')" ]; then
		echo "$aSOA" | cut -d' ' -f3
	else
		echo "$(echo "$aSOA" | cut -d' ' -f5)"
	fi
}

#==============================================================================
function getDomRegistrarStatus() # get status for a domain from its registrar
#------------------------------------------------------------------------------
{
	# get arg
	aDom=$1
	if [ "${aDom:0:4}" = "xn--" ]; then
		aDom=`convIDNtoISO $aDom`
	fi

	# get status
	aStatus=`ssh hsh00-robot@hsh00 cat var/inetwire-domstatus.csv | grep "^$aDom;" | cut -d';' -f3-`
	echo "$aStatus"
}

#==============================================================================
function getDomLevel() # get the level (number of elements) of a domain
#------------------------------------------------------------------------------
{
	local dom dots level

	dom="$1"

	dom="${dom/%.}"		# ignore (remove) trailing dots
	dots="${dom//[^.]}"	# remove everything but dots
	level="${#dots}"	# count the number of dots

	if [ -n "${dom}" ]; then
		# differentiate between the root domain "." and first level
		# domains like "com" and "arpa."
		
		let level=level+1
	fi
	
	echo "${level}"
}

