#!/bin/bash

#Download the ferret script run :
tmpfname=$1
shift 1
#Download a shell script to run this ferret script for all files:</B>
fh="${tmpfname}_prod.bash"
[ -f $fh ] && \rm -f $fh

DIRS=($@)
NDIRS=${#DIRS[@]}
echo "Nb simul " $NDIRS

for thisdir in ${DIRS[@]} ; do
    echo ""
    leadding_sl=$( echo ${thisdir} | grep '.*/$' )
    if [ X"${leadding_sl}" != X ] ; then
	thisdir=$( echo ${thisdir} | sed -e 's&\(.*\)/$&\1&' )
    fi
    echo "dir : " ${thisdir}
    wget -nd ${thisdir}/MONITORING/files 2> /dev/null
    RET=$?
    if [ ${RET} -gt 0 ] ; then
	\ls ${thisdir}/MONITORING/files > files
	RET=$?
	if [ ${RET} -gt 0 ] ; then
	    echo "Error : can't read ${thisdir} !"
	    exit 1
	fi
    fi
    shortdir=$( basename $thisdir ) 
    list_list=(${list_list[@]} ${shortdir}_list)
    mv files ${shortdir}_files
    cat ${shortdir}_files | grep '.*\.nc.*'  | sed -e 's/.*">\(.*\)\.nc<.*/\1.nc/' | sort > ${shortdir}_list
    cat ${shortdir}_list >> allfiles
    if [ X"${listsimus[0]}" = X"" ] ; then
	listsimus[0]=${shortdir}
    else
	listsimus=( ${listsimus[@]} ' vs ' ${shortdir} )
    fi
done
echo ""
listfiles=( $( cat allfiles | sort -u ) )
# if [ ${NDIRS} -eq 2 ] ; then
#     diff -C 0 ${list_list[@]} > difffiles
# else
#     i=0
#     echo $(( NDIRS - 1 ))
#     while [ $i -lt $(( NDIRS - 1 )) ] ; do
# 	diff ${list_list[$i]} ${list_list[$i+1]} > diff_$i
# 	i=$i+1
#     done
# fi
echo "Monitoring comparison: ${listsimus[@]}"

# clean
for thisdir in ${DIRS[@]} ; do
    shortdir=$( basename $thisdir ) 
    \rm -f ${shortdir}_list ${shortdir}_files
done
\rm allfiles

cmd="#!/bin/bash"
printf "$cmd\n" >> $fh
cmd="# Script generated by FEGGsh (Ferret in an EGG in bash)"
printf "$cmd\n\n" >> $fh
cmd="#====================================="
printf "$cmd\n" >> $fh
cmd="#### Please modify if needed"
printf "$cmd\n" >> $fh
cmd="#. /web/igcmg/.atlas_env_eole3_bash               # LSCE (eole3)"
printf "$cmd\n" >> $fh
cmd="#. /home/cont003/p86ipsl/.atlas_env_mercure_ksh   # CCRT (mercure)"
printf "$cmd\n" >> $fh
cmd="#. /home/rech/psl/rpsl035/.atlas_env_ulam_bash    # IDRIS (ulam)"
printf "$cmd\n" >> $fh
cmd="#. /home/users/brock/.atlas_env_asterix_bash      # LSCE (asterix)"
printf "$cmd\n" >> $fh
cmd="#. /home/brocksce/.atlas_env_calcul_ksh           # IPSL (calcul2)"
printf "$cmd\n\n" >> $fh
cmd="#====================================="
printf "$cmd\n" >> $fh
cmd="#### Please modify if needed"
printf "$cmd\n" >> $fh
cmd="scriptname=$tmpfname.jnl"
printf "$cmd\n" >> $fh
cmd="if ! [[ -f \$scriptname && -s \$scriptname ]] ; then"
printf "$cmd\n" >> $fh
cmd="	echo \"Please download the ferret script \$scriptname\""
printf "$cmd\n" >> $fh
cmd="exit"
printf "$cmd\n" >> $fh
cmd="fi"
printf "$cmd\n\n" >> $fh
cmd="#====================================="
printf "$cmd\n" >> $fh
cmd="colors_keys=( ATM    ICE    OCE    SBG    SRF    CHM )"
printf "$cmd\n" >> $fh
cmd="colors_vals=( AECDFF D4E3E6 6D80FF EEE8AA E7FFAB F0D5F4 )"
printf "$cmd\n\n" >> $fh
cmd="function findcolor() {"
printf "$cmd\n" >> $fh
cmd="local i=0"
printf "$cmd\n" >> $fh
cmd="while [ \$i -lt \${#colors_keys[*]} ] ; do"
printf "$cmd\n" >> $fh
cmd="        if [ \"\${colors_keys[\$i]}\" = \"\$1\" ] ; then"
printf "$cmd\n" >> $fh
cmd="                findcolor=\${colors_vals[\$i]}"
printf "$cmd\n" >> $fh
cmd="                return 1"
printf "$cmd\n" >> $fh
cmd="        fi"
printf "$cmd\n" >> $fh
cmd="        let i=i+1"
printf "$cmd\n" >> $fh
cmd="done"
printf "$cmd\n" >> $fh
cmd="findcolor=FFFFFF"
printf "$cmd\n" >> $fh
cmd="return 0"
printf "$cmd\n" >> $fh
cmd="}"
printf "$cmd\n\n" >> $fh
cmd="#====================================="
printf "$cmd\n" >> $fh
cmd="listfiles=("
echo "$cmd \\" >> $fh
for file1 in ${listfiles[*]} ; do
    cmd="$file1 "
    echo "$cmd \\" >> $fh
done
cmd=")"
printf "$cmd\n\n" >> $fh
cmd="#====================================="
printf "$cmd\n" >> $fh
cmd="outputdir=\${scriptname%%%%.jnl}_prod/images"
printf "$cmd\n" >> $fh
cmd="rm -rf \${outputdir}"
printf "$cmd\n" >> $fh
cmd="mkdir -p \${outputdir}"
printf "$cmd\n\n" >> $fh
cmd="#====================================="
printf "$cmd\n" >> $fh
cmd="for file in \${listfiles[*]} ; do"
printf "$cmd\n" >> $fh
cmd="        echo ferret -batch \${outputdir}/\${file%%%%.nc}.gif -script \${scriptname} \${file}"
printf "$cmd\n" >> $fh
cmd="        ferret -batch \${outputdir}/\${file%%%%.nc}.gif -script \${scriptname} \${file}"
printf "$cmd\n\n" >> $fh
cmd="        prefix=\`echo \${file} | cut -f 1 -d _\`"
printf "$cmd\n" >> $fh
cmd="        findcolor \$prefix"
printf "$cmd\n" >> $fh
cmd="        convert -geometry 50%%x50%% -bordercolor \"#\${findcolor}\" -border 15x15 \${outputdir}/\${file%%%%.nc}.gif \${outputdir}/\${file%%%%.nc}.jpg"
printf "$cmd\n" >> $fh
cmd="done"
printf "$cmd\n\n" >> $fh
cmd="#====================================="
printf "$cmd\n" >> $fh
cmd="monitoring01_createindex -t '${listsimus[@]}' \${scriptname%%%%.jnl}_prod"
# printf "$cmd\n" >> $fh
# cmd="mv \${scriptname%%%%.jnl}_prod/\${scriptname%%%%.jnl} \${scriptname%%%%.jnl}_prod/index.html"
printf "$cmd\n\n" >> $fh
cmd="#====================================="
printf "$cmd\n" >> $fh
cmd="#### Please modify if needed"
printf "$cmd\n" >> $fh
cmd="#cp -fR \${scriptname%%%%.jnl}_prod /somewhere/dir"
printf "$cmd\n\n" >> $fh
chmod u+x $fh
