#!/bin/bash
# #!/usr/bin/ksh
# #!/bin/bash

# +---------------------------------------------------------------------------+ #
# | Last subversion informations :                                      
# | $Revision: 28 $ : $Date: 2010-04-27 04:36:58 -0700 (mar, 27 avr 2010) $                                                     
# | $Author: flyounet $
# +---------------------------------------------------------------------------+ #

# As Prerequisites the Library F01 common.lib is needed
. "$(cd ${0%/*}; pwd)/../common.lib"

#Fxx.01.01
#function : verifyLibs, Verify if needed libraries are well loaded, else quit
#syntaxe  : verifyLibs
#return   : nothing
#notes    : Verify if the F0xVersion exists
#notes    : This function is called just after its definition.
verifyLibs () {
	if [ "F01Version" != "$(command -v "F01Version")" ]
	then
		printf -- "\E[01;31mError !\E[0m Library 'F01common.lib' is not loaded !\n"
		exit 1
	fi
}
verifyLibs
F01Version
myDebugTest() {
	myDebug 5 "myDebugTest(${@})"
	p2s "myDebugTest: this is the test line"
	myDebug 6 "myDebugTest(${@})=..."
}

#Fxx.02.01
#function : menuPrint, Print to screen the menu
#syntaxe  : verifyLibs
#return   : nothing
#notes    : 
menuPrint () {
	if [ "x${BASH_VERSION}" != "x" -a "x${KSH_VERSION}" = "x" ]
	then
		p2s "\E[01;31mMenu\E[0m for Bash (Please select an item) :"
	else
		p2s "\E[01;31mMenu\E[0m for Ksh (Please select an item & hit 'enter') :"
	fi
	p2s "\E[01;31m1\E[0m) #F01.01 -> #F01.06,                                 \E[01;31m2\E[0m) #F01.07 -> #F01.11,"
	p2s "\E[01;31m3\E[0m) #F01.12 -> #F01.21,                                 \E[01;31m4\E[0m) #F01.22 -> #F01.23,"
	p2s "\E[01;31m5\E[0m) #F01.24,                                            \E[01;31m6\E[0m) #F01.99,"
	p2s "\E[01;31m7\E[0m) Print all reported erros,                           \E[01;31m8\E[0m) (de)Activate Debug 6,"
	p2s "\E[01;31mX\E[0m) Exit (also works with x|q|Q)"
}

#Fxx.04.02
#function : menuItem1, Execute commmands related to menu Item 1
#syntaxe  : menuItem1
#return   : nothing
#notes    :
menuItem1 () {
	#F01.01
	p2s "p2s: #F01.01 \E[01;31mto be tested\E[0m"
	p2s "p2s: this is the test line"
	p2s "p2s: #F01.01 \E[01;32mtest passed\E[0m\n"
	#F01.02
	p2s "p2st: #F01.02 \E[01;31mto be tested\E[0m"
	p2st "p2st: this is the test line"
	p2s "p2st: #F01.02 \E[01;32mtest passed\E[0m\n"
	#F01.03
	p2s "oops: #F01.03 \E[01;31mto be tested\E[0m"
	p2s "oops: test to be define"
	p2s "oops: #F01.03 \E[01;32mtest passed\E[0m\n"
	#F01.04
	p2s "oopst: #F01.04 \E[01;31mto be tested\E[0m"
	p2s "oopst: test to be define"
	p2s "oopst: #F01.04 \E[01;32mtest passed\E[0m\n"
	#F01.05
	p2s "myDebug: #F01.05 \E[01;31mto be tested\E[0m"
	p2s "myDebug: Debug value ${F01DEBUG}"
	myDebugTest
	F01DEBUG=5
	p2s "myDebug: Debug value ${F01DEBUG}"
	myDebugTest
	F01DEBUG=6
	p2s "myDebug: Debug value ${F01DEBUG}"
	myDebugTest
	F01DEBUG=0
	p2s "myDebug: Debug value ${F01DEBUG}"
	myDebugTest
	p2s "myDebug: #F01.05 \E[01;32mtest passed\E[0m\n"
	#F01.06
	p2s "myError: #F01.06 \E[01;31mto be tested\E[0m"
	p2s "myError: Error reset & setting error 'this is the test line'"
	export __lastError=''; export __allError=''
	myError "this is the test line"
	p2s "myError: __lastError=${__lastError}"
	p2s "myError: __allError=${__allError}"
	sleep 1
	p2s "myError: setting error 'this is the second test line'"
	myError "this is the second test line"
	p2s "myError: __lastError=${__lastError}"
	p2s "myError: __allError=${__allError}"
	p2s "myError: #F01.06 \E[01;32mtest passed\E[0m\n"
}

#Fxx.05.02
#function : menuItem2, Execute commmands related to menu Item 2
#syntaxe  : menuItem2
#return   : nothing
#notes    :
menuItem2 () {
	#F01.08
	p2s "fileCreate: \E[01;31mto be tested\E[0m"
	fileCreate
	p2s "result: [${?}] - lastError: [${__lastError}]"
	p2s "fileCreate: /tmp/filePipo.good"
	fileCreate '/tmp/filePipo.good'
	p2s "result: [${?}] - lastError: [${__lastError}]"
	p2s "fileCreate: \E[01;32mtest passed\E[0m\n"
	#F01.07
	p2s "fileCheck: \E[01;31mto be tested\E[0m"
	p2s "fileCheck: Empty file"
	fileCheck
	p2s "result: [${?}] - lastError: [${__lastError}]"
	p2s "fileCheck: unknown file"
	fileCheck '/tmp/filePipo.pipo' 755
	p2s "result: [${?}] - lastError: [${__lastError}]"
	p2s "fileCheck: Not good rights"
	fileCheck '/tmp/filePipo.good' 755
	p2s "result: [${?}] - lastError: [${__lastError}]"
	p2s "fileCheck: OK"
	fileCheck '/tmp/filePipo.good' 644
	p2s "result: [${?}] - lastError: [${__lastError}]"
	p2s "fileCheck: \E[01;32mtest passed\E[0m\n"
	#F01.09
	p2s "fileDelete: \E[01;31mto be tested\E[0m"
	fileDelete
	p2s "result: [${?}] - lastError: [${__lastError}]"
	p2s "fileDelete: /tmp/filePipo.pipo"
	fileDelete '/tmp/filePipo.pipo'
	p2s "result: [${?}] - lastError: [${__lastError}]"
	p2s "fileDelete: /tmp/filePipo.good"
	fileDelete '/tmp/filePipo.good'
	p2s "result: [${?}] - lastError: [${__lastError}]"
	p2s "fileDelete: \E[01;32mtest passed\E[0m\n"
	#F01.10
	p2s "iniGetValue: \E[01;31mto be tested\E[0m"
	p2s "iniGetValue: Empty file"
	iniGetValue
	p2s "result: [${?}] - lastError: [${__lastError}]"
	p2s "iniGetValue: Non existant or commanted value"
	iniGetValue "$(cd ${0%/*}; pwd)/test_common-lib.conf" "iniToto"
	p2s "result: [${?}] - __iniGetValue: [${__iniGetValue}] - lastError: [${__lastError}]"
	p2s "iniGetValue: Same multiple variable"
	iniGetValue "$(cd ${0%/*}; pwd)/test_common-lib.conf" "iniTata"
	p2s "result: [${?}] - __iniGetValue: [${__iniGetValue}] - lastError: [${__lastError}]"
	p2s "iniGetValue: OK"
	iniGetValue "$(cd ${0%/*}; pwd)/test_common-lib.conf" "iniTiti"
	p2s "result: [${?}] - __iniGetValue: [${__iniGetValue}] - iniTiti: ${iniTiti} - lastError: [${__lastError}]"
	p2s "iniGetValue: OK with setting the variable"
	iniGetValue "$(cd ${0%/*}; pwd)/test_common-lib.conf" "iniTiti" 1
	p2s "result: [${?}] - __iniGetValue: [${__iniGetValue}] - iniTiti: ${iniTiti} - lastError: [${__lastError}]"
	p2s "iniGetValue: \E[01;32mtest passed\E[0m\n"
	#F01.11
	p2s "iniParseFile: \E[01;31mto be tested\E[0m"
	p2s "iniParseFile: Empty file"
	iniParseFile
	p2s "result: [${?}] - lastError: [${__lastError}]"
	p2s "iniParseFile: $(cd ${0%/*}; pwd)/test_common-lib.conf"
	iniParseFile "$(cd ${0%/*}; pwd)/test_common-lib.conf"
	p2s "result: [${?}] - lastError: [${__lastError}]"
	p2s "iniParseFile: isiTata: [${iniTata}] ... __T: [${__T}] ... __Ursulla: [${__Ursulla}]"
	p2s "iniParseFile: \E[01;32mtest passed\E[0m\n"
}

#Fxx.06.02
#function : menuItem3, Execute commmands related to menu Item 3
#syntaxe  : menuItem3
#return   : nothing
#notes    :
menuItem3 () {
	#F01.12
	p2s "trim: \E[01;31mto be tested\E[0m"
	p2s "trim: '      test line	      '"
	trim '      test line	      '
	p2s "result: [${?}] - __trim: [${__trim}]"
	p2s "trim: \E[01;32mtest passed\E[0m\n"

	#F01.13
	p2s "logRotate: \E[01;31mto be tested\E[0m"
	p2s "logRotate: \E[01;32mtest passed\E[0m\n"
	#F01.14
	export __logFileSize=10
	p2s "logMe: \E[01;31mto be tested\E[0m"
	logMe "logMe: this is the test line"
	logMe "logMe: this is the second test line"
	logMe "logMe: this is the third test line"
	p2s "logMe: \E[01;32mtest passed\E[0m\n"
	#F01.15
	p2s "logMeV: \E[01;31mto be tested\E[0m"
	logMeV "logMeV: this is the test line"
	p2s "logMeV: \E[01;32mtest passed\E[0m\n"

	#F01.16
	p2s "isFunction: \E[01;31mto be tested\E[0m"
	p2s "isFunction: isFunction(toto)"
	isFunction "toto"
	p2s "isFunction: isFunction(toto)=${?}"
	p2s "isFunction: isFunction(F01Version)"
	isFunction "F01Version"
	p2s "isFunction: isFunction(F01Version)=${?}"
	p2s "isFunction: isFunction(perl)"
	isFunction "perl"
	p2s "isFunction: isFunction(perl)=${?}"
	p2s "isFunction: isFunction(perl, 1)"
	isFunction "perl" 1
	p2s "isFunction: isFunction(perl, 1)=${?}"
	p2s "isFunction: \E[01;32mtest passed\E[0m\n"

	#F01.17
	p2s "sprintf: \E[01;31mto be tested\E[0m"
	text1="my %s trut %s smells %s"
	text2="big"
	text3="never ever"
	text4="like %s spirit..."
	text5="teen"
	echo "sprintf: text1='${text1}'"
	p2s "sprintf: text2='${text2}'"
	p2s "sprintf: text3='${text3}'"
	echo "sprintf: text4='${text4}'"
	p2s "sprintf: text5='${text5}'"
	p2s "sprintf: sprintf(text1 text2 text3 text4 text5)"
	#export F01DEBUG=5
	sprintf "${text1}" "${text2}" "${text3}" "${text4}" "${text5}"
	p2s "sprintf: sprintf(text1 text2 text3 text4 text5)=${__sprintf}"
	p2s "sprintf: \E[01;32mtest passed\E[0m\n"

	#F01.18
	p2s "ltrim: \E[01;31mto be tested\E[0m"
	p2s "ltrim: '      test    my line	      '"
	ltrim '      test    my line	      '
	p2s "result: [${?}] - __ltrim: [${__ltrim}]"
	p2s "ltrim: \E[01;32mtest passed\E[0m\n"

	#F01.19
	p2s "rtrim: \E[01;31mto be tested\E[0m"
	p2s "rtrim: '      test    my line	      '"
	rtrim '      test    my line	      '
	p2s "result: [${?}] - __rtrim: [${__rtrim}]"
	p2s "rtrim: \E[01;32mtest passed\E[0m\n"

	#F01.20
	p2s "_str2lower: \E[01;31mto be tested\E[0m"
	_string2er='TireLipimpon SUR le CHIWAwa !'
	p2s "_str2lower: _str2lower '${_string2er}'"
	_str2lower ${_string2er}
	p2s "result: [${?}] - __str2lower: [${__str2lower}]"
	p2s "_str2lower: \E[01;32mtest passed\E[0m\n"

	#F01.21
	p2s "_str2upper: \E[01;31mto be tested\E[0m"
	p2s "_str2upper: _str2lower '${_string2er}'"
	_str2upper ${_string2er} 
	p2s "result: [${?}] - __str2upper: [${__str2upper}]"
	p2s "_str2upper: \E[01;32mtest passed\E[0m\n"
}

#Fxx.07.02
#function : menuItem4, Execute commmands related to menu Item 4
#syntaxe  : menuItem4
#return   : nothing
#notes    :
menuItem4 () {
	#F01.22
	p2s "textFill: \E[01;31mto be tested\E[0m"
	p2s "textFill: textFill '=' 14"
	textFill '=' 14
	p2s "result: [${?}] - __textFill: [${__textFill}]"
	p2s "textFill: \E[01;32mtest passed\E[0m\n"

	#F01.23
	p2s "textBox: \E[01;31mto be tested\E[0m"
	text4textBox='
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur.
'
	p2s "textBox: \$text4textBox '*'"
	textBox "${text4textBox}" '*'
	p2s "${__textBox}"
	p2s "textBox: \$text4textBox '#' 'cEnter'"
	textBox "${text4textBox}" '#' 'cEnter'
	p2s "${__textBox}"
	p2s "textBox: \$text4textBox '+' 'RIGHT'"
	textBox "${text4textBox}" '+' 'RIGHT'
	p2s "${__textBox}"
	p2s "textBox: \E[01;32mtest passed\E[0m\n"
}

#Fxx.08.02
#function : menuItem5, Execute commmands related to menu Item 5
#syntaxe  : menuItem5
#return   : nothing
#notes    :
menuItem5 () {
	#F01.24
	p2s "parseHeadersForHelp: \E[01;31mto be tested\E[0m"
	p2s "parseHeadersForHelp common.lib fn"
	parseHeadersForHelp "$(cd ${0%/*}; pwd)/../common.lib" fn
	p2s "parseHeadersForHelp common.lib s parseHead"
	parseHeadersForHelp "$(cd ${0%/*}; pwd)/../common.lib" s parseHead
	p2s "parseHeadersForHelp: \E[01;32mtest passed\E[0m\n"
}

#Fxx.09.02
#function : menuItem6, Execute commmands related to menu Item 6
#syntaxe  : menuItem6
#return   : nothing
#notes    :
menuItem6 () {
	#F01.99
	p2s "F01Version: \E[01;31mto be tested\E[0m"
	p2s "Calling F01Version with an argument"
	F01Version 1
	p2s "result: [${?}] - __F01Version: [${__F01Version}]"
	p2s "Calling F01Version without any argument"
	F01Version
	p2s "F01Version: \E[01;32mtest passed\E[0m\n"
}

#Fxx.10.01
#function : menuItem7, Execute commmands related to menu Item 7
#syntaxe  : menuItem7
#return   : nothing
#notes    :
menuItem7 () {
	p2s "myError: __lastError=${__lastError}"
	p2s "myError: __allError=${__allError}"
}

#Fxx.11.01
#function : menuItem8, Execute commmands related to menu Item 8
#syntaxe  : menuItem8
#return   : nothing
#notes    :
menuItem8 () {
	if [ ${F01DEBUG} -eq 0 ]
	then
		p2s "Debug Activated !"
		F01DEBUG=6
	else
		p2s "Debug De-activated !"
		F01DEBUG=0
	fi
}

while true
do
	menuPrint
	if [ "x${BASH_VERSION}" != "x" -a "x${KSH_VERSION}" = "x" ]
	then
		read -n 1 -p "Your choice : " _answer
	else
		read  _answer?"Your choice : "
	fi
	clear
	case "${_answer}" in
		'1') menuItem1 ;;
		'2') menuItem2 ;;
		'3') menuItem3 ;;
		'4') menuItem4 ;;
		'5') menuItem5 ;;
		'6') menuItem6 ;;
		'7') menuItem7 ;;
		'8') menuItem8 ;;
		'9') menuItem9 ;;
		'0') menuItem0 ;;
		[xXqQ]) exit 0 ;;
	esac
done



