#!/bin/bash
## sympow-new_data-job-generate
## BASH script to generate SH job script for precomputing sympow mesh data files
##
## usage: sympow-new_data-job-generate PARAMETERFILE [GENERATEDSCRIPTNAME]
##   PARAMETERFILE:
##     data file containing one sympow new_data like third parameter per line;
##     lines beginning with # being discarded.
##   GENERATEDSCRIPTNAME:
##     name of the generated script (default: `sympow-new_data.job').
##
## possible launch usage:
## $ ./GENERATEDSCRIPTNAME
## $ at -f ./GENERATEDSCRIPTNAME now
## $ at -f ./GENERATEDSCRIPTNAME teatime
## $ at -f ./GENERATEDSCRIPTNAME midnight
##
## comments:
## 1] the TOP_JOBDIR environment variable in the header of the generated
##    job script might be modified according to your need and taste;
## 2] as batch job scheduler, besides at, slurm-wln might be used (modulo
##    meta command in the header), or any other decent job scheduler.
##
## Written for Debian on behalf of the Debian Science Team
## by Jerome Benoit <calculus@rezozer.net>, 2016/05/02.
##
TOP_BUILDDIR=$PWD

if [ -x $TOP_BUILDDIR/sympow ]; then
	SYMPOW=$TOP_BUILDDIR/sympow
else
	SYMPOW=/usr/bin/sympow
fi

export SYMPOW_PKGCACHEDIR=${TMPDIR:-/tmp}/nonexistent/var/cache/sympow
export SYMPOW_PKGLIBDIR=${TMPDIR:-/tmp}/nonexistent/usr/lib/sympow
export SYMPOW_PKGDATADIR=SEDREGEX_SYMPOW_PKGDATADIR
export SYMPOW_CACHEDIR=SEDREGEX_SYMPOW_CACHEDIR

##SYMPOW_OPTS="-quiet"
SYMPOW_OPTS="-verbose"

[ -x $SYMPOW ] || exit 1

sympow_generate_sh_write_preamble () {
	cat << EOS
#!/bin/sh
##
## generation date: $(date -u '+%Y%m%dT%H%M%SZ')
## generated with: ${0##*/}
## generated by: ${DEBFULLNAME:-$USER} <${DEBEMAIL:-${USER}@$(hostname -s)}>
##
## sympow version: $($SYMPOW -version)
##
TOP_JOBDIR=\$PWD

SYMPOW_DEFAULT_CACHEDIR=\${TOP_JOBDIR}/sympow_cache

GP=/usr/bin/gp

if [ -r \${TOP_JOBDIR}/standard1.gp ]; then
	## meant for development job
	SYMPOW_PKGDATADIR=\${TOP_JOBDIR}
	SYMPOW_CACHEDIR=\${TOP_JOBDIR}
elif [ -z "\${SYMPOW_PKGDATADIR}" ]; then
	SYMPOW_PKGDATADIR=/usr/share/sympow
fi
if [ -z "\${SYMPOW_CACHEDIR}" ]; then
	SYMPOW_CACHEDIR=\${SYMPOW_DEFAULT_CACHEDIR}
	if [ -d \${SYMPOW_CACHEDIR} ]; then
		echo >&2 "WARNING: the cache directory \${SYMPOW_CACHEDIR} already exists"
	fi
fi

if [ ! -x \$GP ]; then
	echo >&2 "ERROR: the PARI calculator \$GP is not executable"
	exit 2
fi

for ndx in 1 2 3; do
	if [ ! -r \${SYMPOW_PKGDATADIR}/standard\${ndx}.gp ]; then
		echo >&2 "ERROR: the PARI calculator instrunction file \${SYMPOW_PKGDATADIR}/standard\${ndx}.gp is not readable"
		exit 2
	fi
done

if [ ! -d \${SYMPOW_CACHEDIR} ]; then
	mkdir \${SYMPOW_CACHEDIR}
fi
if [ ! -d \${SYMPOW_CACHEDIR}/datafiles ]; then
	mkdir \${SYMPOW_CACHEDIR}/datafiles
fi

EOS
	mkdir -p SEDREGEX_SYMPOW_CACHEDIR
	return 0
	}

sympow_generate_sh_write_amble () {
	cat << EOS
##MARK##############################################
echo "Parameters: '$@'"

echo "Running the gp script"
cat << EOP | \$GP -f -q > /dev/null 2>&1
EOS
$SYMPOW $SYMPOW_OPTS -pari "$@" 2> /dev/null | \
		sed 's|SEDREGEX_SYMPOW_PKGDATADIR|\$SYMPOW_PKGDATADIR|g;s|SEDREGEX_SYMPOW_CACHEDIR|\$SYMPOW_CACHEDIR|g'
	cat << EOS
EOP

EOS
$SYMPOW $SYMPOW_OPTS -shell2 "$@" 2> /dev/null | \
		sed 's|SEDREGEX_SYMPOW_CACHEDIR|\$SYMPOW_CACHEDIR|g'

cat << EOS

echo
##############################################KRAM##

EOS

	return 0
	}

sympow_generate_sh_write_postamble () {
	rm -rf SEDREGEX_SYMPOW_CACHEDIR/datafiles
	rmdir SEDREGEX_SYMPOW_CACHEDIR
	cat << EOS
exit 0
## eos
EOS
	return 0
	}

sympow_generate_sh_write () {
	local datafile=$1
	local script=${2:-'sympow-new_data.job'}
	sympow_generate_sh_write_preamble > $script
	listof_arg=$(cat $DATAFILE | sed '/^#/d' | tr '\n' ':')
	IFS=:
	for arg in $listof_arg; do
		sympow_generate_sh_write_amble $arg >> $script
	done
	unset IFS
	sympow_generate_sh_write_postamble >> $script
	chmod a+x $script
	return 0
	}

DATAFILE=${1}
SCRIPT=${2}

sympow_generate_sh_write $DATAFILE $SCRIPT

exit 0
## eos
