#!/bin/bash

###########################################################
fh=$1
echo "" > $fh
shift 1
smooth=$1
shift 1
file=$1
shift 1
DIRS=$@
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}

    http_dir=$( echo ${thisdir} | grep "http://" )
    if [ X"${http_dir}" != X ] ; then
	cgi=$( echo ${thisdir} | grep "cgi-bin/nph-dods" )
	if [ X"${cgi}" = X"" ] ; then
	    thisdir=$( echo ${thisdir} | sed -e "s&http://\([^/]*\)/&http://\1/cgi-bin/nph-dods/&" )
	fi
	echo "new dir name : " ${thisdir}
    fi
    alldir=(${alldir[@]} ${thisdir})
done
###########################################################

cmd="!============================"
printf "$cmd\n" >> $fh
cmd="! Script generated by FEGGsh (Ferret in an EGG in bash)\n"
printf "$cmd\n" >> $fh
# mode linecolors is an undocumented mode (beta) 
# /thick does not work
cmd="set mode linecolors 30"
printf "$cmd\n" >> $fh
cmd="set window/size=0.4 1 ; cancel mode logo"
printf "$cmd\n" >> $fh
cmd="set view full ; go margins_set 15 30 10 08"
printf "$cmd\n" >> $fh
cmd="\n!============================"
printf "$cmd\n" >> $fh
cmd="!Please change the colors or/and legends if needed"
printf "$cmd\n" >> $fh
cmd='let colors={"3B63E6", "FF4500", "FFE000", "34D314", "FF1392", "FFA400", "775BB4", "F5E6BD", "789BF1", "AFDFE6", "0000C7", "1FB1AA", "B12121", "DEB886", "FFB6C1"}'
printf "$cmd\n" >> $fh
cmd="! Color for graticule"
printf "$cmd\n" >> $fh
cmd="ppl color 6, 80, 80, 80"
printf "$cmd\n" >> $fh
nd=1
for s in ${alldir[@]} ; do
	let ncolor=nd+6     # Users colors will begin at 7
	cmd="go set_color_from_hexa $ncolor \`colors[i=$nd]\`"
	printf "$cmd\n" >> $fh
        let nd=nd+1
done
cmd="\n!============================"
printf "$cmd\n" >> $fh
cmd="def symbol FILE=(\$01%%$file%%)"
printf "$cmd\n\n" >> $fh
nd=0
for s in ${alldir[@]} ; do
	cmd="use \"$s/MONITORING/files/(\$FILE)\""
	printf "$cmd\n" >> $fh
        let nd=nd+1
done
cmd="\ndef symbol NVAR1=\`..nvars\`"
printf "$cmd\n" >> $fh
cmd="def symbol VAR1=\`..varnames[i=(\$NVAR1)]\`"
printf "$cmd\n" >> $fh
cmd="def symbol SMOOTH=(\$02%%$smooth%%)"
printf "$cmd\n" >> $fh
cmd="let var=(\$VAR1)[L=@SBX:(\$SMOOTH)]"
printf "$cmd\n" >> $fh
cmd="\n!============================"
printf "$cmd\n" >> $fh
cmd="plot/nolab/line/grat=(dash,color=6) var[d=1]"
i=2
while [ $i -le $nd ] ; do
        cmd="$cmd, var[d=$i]"
	let i=i+1
done
printf "$cmd\n" >> $fh
nd=1
for s in ${alldir[@]} ; do
	let ncolor=nd+6     # Users colors will begin at 7
	cmd="plot/nolab/over/color=$ncolor/line var[d=$nd]"
	printf "$cmd\n" >> $fh
        let nd=nd+1
done
cmd="\n!============================"
printf "$cmd\n" >> $fh
cmd="define view/x=0:1/y=0:1 full2 ; set view full2"
printf "$cmd\n" >> $fh
cmd="go margins_set 15 30 10 08"
printf "$cmd\n" >> $fh
cmd="plot/nolab/noaxis i[i=1:5]*0-1E34 ! plot nothing, needed to define symbols used by text_legend_put"
printf "$cmd\n" >> $fh
xpos1=10; yposstart=20; ypos=$yposstart
nd=1
for s in ${alldir[@]} ; do
	let ncolor=nd+6     # Users colors will begin at 7
	if [ $ypos -lt 4 ] ; then
		ypos=$yposstart
		xpos1=40
	fi 
	let xpos2=xpos1+10 
	cmd="go text_legend_put $xpos1 $xpos2 $ypos \"@AS`basename $s`\" $ncolor 0.35"
	let ypos=ypos-3
	printf "$cmd\n" >> $fh
        let nd=nd+1
done
cmd="\n!============================"
printf "$cmd\n" >> $fh
cmd="go text_put 05 95 \"@AS\`(\$VAR1),return=dset\`.nc\" -1 0.3"
printf "$cmd\n" >> $fh
cmd="go text_put 10 90 \"@AS\`(\$VAR1),return=title\` (\`(\$VAR1),return=units\`) (@SBX:(\$SMOOTH))\" -1 0.35"
printf "$cmd\n" >> $fh
cmd="\n!============================"
printf "$cmd\n" >> $fh
