#!/bin/sh
# set -x
#
#  Based on globus submission script for pbs
#
#  Submits job to the 3GBridge.
#  Input: path to grami file (same as Globus).
#
#opposed to other backends there is no jobscript

echo "----- starting submit_DGBridge_job -----" 1>&2
joboption_lrms=DGBridge

# ARC1 passes the config file first.
if [ "$1" = "--config" ]; then shift; ARC_CONFIG=$1; shift; fi

basedir=`dirname $0`
basedir=`cd $basedir > /dev/null && pwd` || exit $?

pkgdatadir="$basedir"

. ${pkgdatadir}/submit_common.sh || exit $?

##############################################################
# Parse grami file, read arc config
##############################################################
#output the grami file
init $1

#extract staging info:
bdir=`grep "lrms:dg_stage_dir" $ARC_CONFIG | sed 's/<lrms:dg_stage_dir>//g' | sed 's/<\/lrms:dg_stage_dir>//g'`
bweb=`grep "lrms:dg_stage_prepend" $ARC_CONFIG | sed 's/<lrms:dg_stage_prepend>//g' | sed 's/<\/lrms:dg_stage_prepend>//g'`

#echo $bdir > /tmp/testet
#echo $bweb >> /tmp/testet
#set > /tmp/vars.chrulle

failures_file="$joboption_controldir/job.$joboption_gridid.failed"

if [ -z "${RUNTIME_NODE_SEES_FRONTEND}" ] ; then
  if [ -z "${RUNTIME_LOCAL_SCRATCH_DIR}" ] ; then
    echo "Need to know at which directory to run job: RUNTIME_LOCAL_SCRATCH_DIR must be set if RUNTIME_NODE_SEES_FRONTEND is empty" 1>&2
    echo "Submission: Configuration error.">>"$failures_file"
    exit 1
  fi
fi

##############################################################
# Zero stage of runtime environments
# In the dg bridge this should set the 
# endpoint,grid and app, and VO access 
# $DG_Endpoint, $DG_Grid, $DG_App, $DG_AllowedVOs 
#
# There must be exactly one runtime environment
##############################################################

joboption_num=0
eval "var_is_set=\${joboption_runtime_$joboption_num+yes}"
while [ ! -z "${var_is_set}" ] ; do
  eval "var_value=\${joboption_runtime_$joboption_num}"
  if [ -r "$RUNTIME_CONFIG_DIR/${var_value}" ] ; then
     . "$RUNTIME_CONFIG_DIR/${var_value}" 0 1>&2
     if [ $? -ne '0' ] ; then
       echo "ERROR: runtime script ${var_value} failed" 1>&2
       exit 1
     fi
  else
     echo "ERROR: runtime script ${var_value} is missing" 1>&2
     exit 1
  fi
  joboption_num=$(( $joboption_num + 1 ))
  eval "var_is_set=\${joboption_runtime_$joboption_num+yes}"
done

# There must be exactly one runtime environment

if [ "$joboption_num" -eq 0 ]; then
   echo "ERROR: No runtime environment specified." 1>&2
   exit 1
fi
if [ "$joboption_num" -gt 1 ]; then
   echo "ERROR: More than one runtime environment specified." 1>&2
   exit 1
fi


##############################################################
# Start job script
#Note:  there is no script for DGBridge
##############################################################

# choose queue
#if [ ! -z "${joboption_queue}" ] ; then
 # here we must extract and check endpoint data
 # once the RTE per queue options is implemented
#fi

############################################################
# DG:
# check that EP,APP and Grid has been set
###########################################################

if [ -z "$DG_App"  ]; then
   echo "ERROR: RTE has not set the application id." 1>&2
   exit 1
fi
if [ -z "$DG_Endpoint"  ]; then
   echo "ERROR: RTE has not set the endpoint address." 1>&2
   exit 1
fi
if [ -z "$DG_Grid"  ]; then
   echo "ERROR: RTE has not set the target grid." 1>&2
   exit 1
fi
if [ -z "$DG_AllowedVOs"  ]; then
   echo "ERROR: RTE has not set any allowed VOs." 1>&2
   exit 1
fi

############################################################
# DG:
# check that the user has correct VO
###########################################################

# check is performed in the plugin now.

##############################################################
#  DGBridge:
#  calculate md5sums of input files and move input files to
#   be served from apache (perhaps Attic in the future)
##############################################################
#set storage dir from config



#stageweb="http://edgi-dev.nbi.dk:9090/3GBridge/in/$joboption_gridid"
#stagedir="/var/www/3GBridge/in/$joboption_gridid/"
stageweb=$bweb"/in/$joboption_gridid"
stagedir=$bdir"/in/$joboption_gridid/"
#clean dir and link
stageweb=`echo $stageweb|sed -e 's/^[ \t]*//'`
stagedir=`echo $stagedir|sed -e 's/^[ \t]*//'`

DG_Inputlist=""
# read files
inputs0=$joboption_directory/*
#remove directories
inputs=""
for inf in $inputs0; do
 if [ -f $inf ]; then
   inputs="$inputs$inf "
 fi
done

if [ ! -z "$inputs" ]; then

  mkdir $stagedir
  chmod a+rx $stagedir
  for infile in $inputs; do
    
    #calc md5 and size
    mdsum=`md5sum $infile`
    infmdsum=${mdsum%%\ *}
    infsize=`stat -c %s $infile` 
    cp $infile $stagedir
    DG_Inputlist="$DG_Inputlist -i ${infile##*/}=$stageweb/${infile##*/}=$infmdsum=$infsize"    
  done
  chmod a+rx -R $stagedir
fi

#get files from .3GData I.e. atticlinks
dgdatafile=$joboption_controldir/job.$joboption_gridid.3gdata
if [ -f $dgdatafile ]; then
 while read line; do
      if [ ${#line} -gt 2 ]; then
        infile=${line%% *}
        infurl=${line#* }
        infurl=${infurl%;*}
        infmd5=${line#*md5=}
        infmd5=${infmd5%\:*}
        infsize=${line#*size=}
        infsize=${infsize%:*}
        DG_Inputlist="$DG_Inputlist -i $infile=$infurl=$infmd5=$infsize"
      fi
  done < $dgdatafile
fi


############################################################
# DG:
# create input and output list
###########################################################
DG_Outputlist=""
outfile=$joboption_controldir/job.$joboption_gridid.output
outputfs=`cat $outfile`

for ofile in $outputfs; do
 DG_Outputlist="$DG_Outputlist -o ${ofile#/} "
done 

###########################################################
#  Get Arguments from job
###########################################################

DG_Args=""
# remember to set -a flag if there are arguments
i=1
joboption_args=
eval "var_is_set=\${joboption_arg_$i+yes}"
while [ ! -z "${var_is_set}" ] ; do
   eval "var_value=\${joboption_arg_$i}"
   # Use -- to avoid echo eating arguments it understands
   var_value=`echo -- "$var_value" |cut -f2- -d' '| sed 's/\\\\/\\\\\\\\/g' | sed 's/"/\\\"/g'`
   joboption_args="$joboption_args \"${var_value}\""
   i=$(( $i + 1 ))
   eval "var_is_set=\${joboption_arg_$i+yes}"
done
if [ ! -z "$joboption_args" ]; then
   DG_Args="-a '${joboption_args}'"
fi



#######################################
#  Submit the job
#######################################

cmdline="wsclient -e $DG_Endpoint -m add -n $DG_App -g $DG_Grid $DG_Inputlist $DG_Outputlist $DG_Args"
echo "running command: $cmdline" 1>&2

dato=`date +%Y-%m-%d`
edgilog=$joboption_controldir/3gbridge_logs/$dato
dato=`date +%Y-%m-%d_%H:%M:%S`

OUT=$(eval "$cmdline 2>&1")
if [ $? -eq 0 ]; then
   jobid=$OUT
   #write EDGI monitor log
   msg="dt=$dato event=job_submission bridge_id=$DG_Endpoint job_id=$joboption_gridid job_id_bridge=$jobid status=Submitted"
   flock -w 10 $edgilog -c "echo $msg >> $edgilog"
   if [ $? == 1 ]; then
     echo "Failed to log monitor data to: $edgilog" 1>&2
   fi
   # write output to arg file
   echo "joboption_wsendpoint=$DG_Endpoint" >> $arg_file
   echo "joboption_jobid=$jobid" >> $arg_file
   echo "job submitted successfully!" 1>&2
   echo "local job id: $jobid" 1>&2
   echo "----- exiting submit_DGBridge_job -----" 1>&2
   echo "" 1>&2
   exit 0
fi

#write EDGI monitor log
msg="dt=$dato event=job_submission bridge_id=$DG_Endpoint job_id=$joboption_gridid status=Failed"
echo "monitor: $msg"
flock -w 10 $edgilog -c "echo $msg >> $edgilog"
if [ $? == 1 ]; then
  echo "Failed to log monitor data to: $edgilog" 1>&2
fi

echo "jobsubmission failed." 1>&2
echo "$OUT" 1>&2
echo "----- exiting submit_DGBridge_job -----" 1>&2
echo "" 1>&2
exit 1
