#! /bin/bash
##-l:	Licence
##-l:	-------
##-l:	#=================================================================
##-l:	# This program is Free Software; you can redistribute it and/or
##-l:	# modify it under the terms of the GNU General Public License
##-l:	# as published by the Free Software Foundation; either version 2
##-l:	# of the License, or (at your option) any later version.
##-l:	#
##-l:	# This program is distributed in the hope that it will be useful,
##-l:	# but WITHOUT ANY WARRANTY; without even the implied warranty of
##-l:	# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##-l:	# GNU General Public License for more details.
##-l:	#
##-l:	# You should have received a copy of the GNU General Public License
##-l:	# along with this program; if not, write to the Free Software
##-l:	# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##-l:	#
##-l:	#=================================================================
##-l:	# 2022 - Copyright SISalp www.sisalp.fr
##-l:	# par dominique.chabord@sisalp.org

Version="0.1 2-june-2022"
DownloadSource="http://download.sisalp.net/scripts/csv2proteus"
MyName=`basename $0`
#Myself=`realpath $0`
Myself="$MyName"
MyLog="/tmp/$MyName.log"
MyUser=`whoami`
AllParameters="$*"
OptionList="syntax create demo prepare populate set_models"
Csv2proteusConfDir="/usr/local/etc/csv2proteus"
populate_type="csv2proteus"
a_parameter_is_wrong="no"
standard_IFS="$IFS" ;
line_IFS=$'\n' ;
FOR_LINE () { IFS="$line_IFS" ; }
DO () { IFS="$standard_IFS" ; }
DONE_LINE () { IFS="$line_IFS" ; }
DONE () { IFS="$standard_IFS" ; }
#example
#FOR_LINE ; for line in `cat $file` ; do DO
#	....
#DONE_LINE ; done ; DONE
#echo "Activation $MyName $AllParameters"
Option="$1"
if [ -z "$Option" ] ; then
	Option="--howto"
fi
shift
case "$Option" in
-h|help|--help)
	
		echo "
Usage:
------
$MyName -h|help|--help		print this help and exit
$MyName -v|version|--version	print version and exit
$MyName -l|license|--license	print license and exit
$MyName -u|update|--update	update this script


$MyName -p[populate|--populate populate_type data_file.csv template_file.template default_values.defaults proteus_script.py
"
	exit 0
	;;
-v|version|--version)
	echo "Version :	$Version"
	exit 0
	;;
-l|license|--license)
	cat $0 | grep "^##-l:" | cut -d: -f2-
	exit 0
	;;
-u|update|--update)
	case "$MyUser" in
	root)
		OldVersion=`$0 --version`
		cd /usr/local/bin
		mv $0 $0.old
		if wget -q $DownloadSource ; then
			chmod 755 $0
			diff $0.old $0
			rm $0.old
			NewVersion=`$0 --version`
			case "$NewVersion" in
			$OldVersion)
				echo "$0 is uptodate $NewVersion"
				;;
			*)
				echo "$0 downloaded new version from $DownloadSource"
				;;
			esac
		else
			echo "cannot download new version of $0 from $DownloadSource"
			rm $0
			mv $0.old $0
		fi
		if [ ! -d $Csv2proteusConfDir ] ; then
			mkdir $Csv2proteusConfDir
			chmod 777 $Csv2proteusConfDir
		fi
		exit 0
		;;
	*)
		echo "$0 update: You must get administration priviledges root to succeed, trying sudo"
		sudo $0 --update
		exit 0
		;;
	esac
	
	;;
-p|populate|--populate)
	;;
*)
	echo "this option $Option is not supported by $Myname"
	$Myself --help
	exit 1
	;;
esac

#-------------------------------------------------------------------------
REFRESH_DEFAULTS ()
#-------------------------------------------------------------------------
{

if [ -f $Csv2proteusConfDir/$populate_type.defaults ] ; then
	. $Csv2proteusConfDir/$populate_type.defaults
	case "$1" in
	-v)
		echo "defaults values of $populate_type.defaults"
		cat $Csv2proteusConfDir/$populate_type.defaults | xargs
		;;
	esac
fi
}

#-------------------------------------------------------------------------
PARAMETERS ()
#-------------------------------------------------------------------------
{
echo "$*: populate_type $populate_type data_file $data_file template_file $template_file default_values $default_values proteus_script $proteus_script"
}
#-------------------------------------------------------------------------
GUESS_FILE_NAME ()
#-------------------------------------------------------------------------
{
	#data_file.csv template_file.template default_values.defaults proteus_script.py
for parameter in $* ; do
	case "$parameter" in
	*.csv)
		data_file="$parameter"
		;;
	*.template)
		template_file="$parameter"
		;;
	*.defaults)
		default_values="$parameter"
		;;
	*.py)
		proteus_script="$parameter"
		;;
	*)
		populate_type="$parameter"
		;;
	esac
done
}
#-------------------------------------------------------------------------
CHECK_PARAMETERS ()
#-------------------------------------------------------------------------
{
	#data_file.csv template_file.template default_values.defaults proteus_script.py
	if [ -z "$populate_type" ] ; then
		echo "$LINENO WARNING type not found"
		a_parameter_is_wrong="yes"
		exit 1
	fi

	if [ -z "$data_file" ] ; then
		data_file="$populate_type.csv"
	fi
	if [ -z "$template_file" ] ; then
		template_file="$populate_type.template"

	fi
	if [ -z "$default_values" ] ; then
		default_values="$populate_type.fields.defaults"
	fi
	if [ ! -f $data_file ] ; then
		echo "$LINENO WARNING data_file $data_file not found"
		a_parameter_is_wrong="yes"
	fi
	if [ ! -f $template_file ] ; then
		echo "$LINENO WARNING template_file $template_file not found"
		a_parameter_is_wrong="yes"
	fi
	if [ ! -f $default_values ] ; then
		echo "#$LINENO $default_values is created" 	> $default_values
	fi
	if [ -z "$proteus_script" ] ; then
		echo "$LINENO result on standard output"
	else
		if [ -f $proteus_script ] ; then
			echo "$LINENO WARNING proteus_script $proteus_script already exists, will be overwritten"
		fi
	fi
}
#-------------------------------------------------------------------------
TOGGLE ()
#-------------------------------------------------------------------------
{
	tog_option="$1"
	case "$tog_option" in
	-notnull)
		shift
		;;
	-ifnew)
		shift
		;;
	esac
	tog_file="$1"
	shift
	tog_line="$*"
	tog_variable=`echo "$tog_line" | cut -d= -f1`
	case "$tog_line$tog_option" in
	*'=""'-notnull)
		echo "$tog_line : null content is not added"
		;;
	*-ifnew)
		if [ -f $tog_file ] ; then
			if ! cat $tog_file | grep "^$tog_variable=" > /dev/null 2>&1 ; then
				before_content=`cat $tog_file | grep -v "^$tog_line"`
				echo "$before_content" > $tog_file
				echo "$tog_line" >> $tog_file
			else
				echo "$tog_line : $tog_variable already assigned is not added "
			fi
		else
			echo "$tog_line" >> $tog_file
		fi
		;;
	*)
		if [ -f $tog_file ] ; then
			before_content=`cat $tog_file | grep -v "^$tog_line"`
			echo "$before_content" > $tog_file
		fi
		echo "$tog_line" >> $tog_file
		;;
	esac
}
#-------------------------------------------------------------------------
REMIND_PARAMETERS ()
#-------------------------------------------------------------------------
{
	#data_file.csv template_file.template default_values.defaults proteus_script.py
	case "$a_parameter_is_wrong" in
	yes)
		exit 1
		;;
	esac
	TOGGLE $Csv2proteusConfDir/csv2proteus.defaults populate_type=\"$populate_type\"
	TOGGLE $Csv2proteusConfDir/$populate_type.defaults populate_type=\"$populate_type\"
	for parameter in $* ; do
		case "$parameter" in
		*.csv)
			TOGGLE $Csv2proteusConfDir/$populate_type.defaults data_file=\"$parameter\"
			;;
		*.template)
			TOGGLE $Csv2proteusConfDir/$populate_type.defaults template_file=\"$parameter\"
			;;
		*.defaults)
			TOGGLE $Csv2proteusConfDir/$populate_type.defaults default_values=\"$parameter\"
			;;
		*.py)
			TOGGLE $Csv2proteusConfDir/$populate_type.defaults proteus_script=\"$parameter\"
			;;
		esac
	done
}
#-------------------------------------------------------------------------
ASCII_STRING ()
#-------------------------------------------------------------------------
{
	# punct ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~. 
	ascii_converted=`echo "$*" | iconv -f utf-8 -t US-ASCII//TRANSLIT | tr '[:upper:]' '[:lower:]' | tr ';' 'A' | tr '[:punct:]' '_' | tr ' ' '_' | tr 'A' ' '`
	echo "$ascii_converted"
}
#-------------------------------------------------------------------------
GET_TITLE_LIST ()
#-------------------------------------------------------------------------
{
	TitleLine=`head -n 1 $data_file | sed 's/\r$//'`
	echo "$TitleLine"
	#simple case
	TitleList=`ASCII_STRING $TitleLine`
	echo "$TitleList"
	for field in $TitleList ; do
		echo "$field"
		#echo "$field=\"\"" >> $default_values
		TOGGLE -ifnew $default_values default_$field=\"\"
	done
	cat -v $default_values
	echo "--------------------"
	
}
#-------------------------------------------------------------------------
GET_TEMPLATES ()
#-------------------------------------------------------------------------
{
echo "GET_TEMPLATES"

}
#-------------------------------------------------------------------------
PROCESS_HEAD ()
#-------------------------------------------------------------------------
{
echo "PROCESS_HEAD"
}
#-------------------------------------------------------------------------
SET_FIELD_DEFAULTS ()
#-------------------------------------------------------------------------
{
echo "SET_FIELD_DEFAULTS first field called $first_field_name is $first_field, initialize default substitution"
echo "#SET_FIELD_DEFAULTS intialize /tmp/default_fields" > /tmp/default_fields
for field in $TitleList ; do
	#echo "if [ -z \"\$$field\" ] ; then $field=\"\$default_$field\" ; fi ; echo \"$field : \$$field\" " >> /tmp/default_fields
	echo "if [ -z \"\$$field\" ] ; then $field=\"\$default_$field\" ; fi " >> /tmp/default_fields
done
#cat /tmp/default_fields
#echo "--------------------"
}
#-------------------------------------------------------------------------
UPDATE_DEFAULT_VALUES ()
#-------------------------------------------------------------------------
{
echo "UPDATE_DEFAULT_VALUES first field called $first_field_name is $first_field, modify default field values"

echo "#UPDATE_DEFAULT_VALUES intialize  /tmp/modify_defaults" > /tmp/modify_defaults
for field in $TitleList ; do
	case "$field" in
	$first_field)
		;;
	*)
		echo "TOGGLE -notnull $default_values default_$field=\\\"\$$field\\\"" >> /tmp/modify_defaults
		;;
	esac
done
#cat /tmp/modify_defaults
. /tmp/modify_defaults
. $default_values
}
#-------------------------------------------------------------------------
PROCESS_LINE ()
#-------------------------------------------------------------------------
{
echo "PROCESS_LINE first field called $first_field_name is $first_field, process line template"
echo "#PROCESS_LINE intialize  /tmp/line" > /tmp/line
#cat /tmp/default_fields
. /tmp/default_fields
for field in $TitleList ; do
	echo "echo \"$field : \$$field\"" >> /tmp/line
	#echo "$field : \$$field"
done

#cat /tmp/line
. /tmp/line
echo "--------------------"

}
#-------------------------------------------------------------------------
PROCESS_TAIL ()
#-------------------------------------------------------------------------
{
echo "PROCESS_TAIL"
}
#-------------------------------------------------------------------------
PROCESS_CSV_LINES ()
#-------------------------------------------------------------------------
{
#cat -v $data_file | sed 's/\r$//'
first_field_name=`echo "$TitleList" | cut -d" " -f1`
first_field_title_value=`echo "$TitleLine" | cut -d";" -f1`
echo "#PROCESS_CSV_LINES intialize /tmp/first_field, first_field_name $first_field_name first_field_value $first_field_value" > /tmp/first_field
echo "first_field=\"\$$first_field_name\"" >> /tmp/first_field
cat /tmp/first_field
echo "----------"
GET_TEMPLATES
PROCESS_HEAD
LineNumber="0"
cat $data_file | sed 's/\r//' | grep -v "^#" | while IFS=";" read $TitleList ;
do  
	. /tmp/first_field
	#echo "first field is : $first_field"
	#echo "----------"
	if [ ! -z "$first_field" ] ; then
		case "$first_field" in
		$first_field_title_value)
			SET_FIELD_DEFAULTS
			;;
		DEFAULTS)
			UPDATE_DEFAULT_VALUES
			;;
		*)
			PROCESS_LINE
			;;
		esac
	else
		echo "empty line $LineNumber"
	fi

	LineNumber=$[$LineNumber +1]
done
PROCESS_TAIL
}

REFRESH_DEFAULTS -v
#PARAMETERS $LINENO initial
GUESS_FILE_NAME $*
#PARAMETERS $LINENO guessed
REFRESH_DEFAULTS -v
#PARAMETERS $LINENO defaulted
GUESS_FILE_NAME $*
#PARAMETERS $LINENO final
CHECK_PARAMETERS
#PARAMETERS $LINENO checked
REMIND_PARAMETERS $*
PARAMETERS $LINENO recorded

GET_TITLE_LIST
PROCESS_CSV_LINES
echo "fini"
exit 0






