#!/bin/bash

RunningGlideins() {

OLD_IFS=$IFS;IFS=" ";ilocalslot=( $@ );IFS=$OLD_IFS

MYNODE=${ilocalslot[0]}

echo "#!/bin/bash" > /tmp/findglides_$$
echo "#Get Log Dir" >> /tmp/findglides_$$
echo "MYLOG=\"\`condor_config_val -startd LOG\`\"" >> /tmp/findglides_$$
echo "#Get Working Dir" >> /tmp/findglides_$$
for i in $(seq 1 $((${#ilocalslot[@]} - 1)))
do
#old#MYSLOT=${ilocalslot[$i]}
echo MYINFO="\`condor_who | grep ${ilocalslot[$i]}\`" >> /tmp/findglides_$$
echo MYSLOT="slot\$(a=(\$MYINFO);echo \${a[2]})" >> /tmp/findglides_$$
echo MYEXEC="\$(a=(\$MYINFO);echo \${a[6]})" >> /tmp/findglides_$$
#test#echo "echo MYEXEC=\$MYEXEC" >> /tmp/findglides_$$
echo "MYGRIDID=" >> /tmp/findglides_$$
#old#
echo MYWNDIR="\`cat \$MYLOG/StarterLog.\$MYSLOT | grep IWD: |  tail -1 | cut -d\  -f5 \`" >> /tmp/findglides_$$
#new#echo MYWNDIR="\`dirname \$MYEXEC \`" >> /tmp/findglides_$$
#get grid id of slot singlecore ...
#echo MYJOBSLOT="( \`[ -f \$MYWNDIR/glide_*/log/StarterLog ]&&cat \$MYWNDIR/glide_*/log/StarterLog | grep IWD: |  tail -1 | cut -d\  -f5\`)" >> /tmp/findglides_$$
#echo MYGRIDID="\$MYJOBSLOT" >> /tmp/findglides_$$
#echo [ -f \$MYJOBSLOT/_condor_stdout]&&MYGRIDID="\`grep subject \$MYJOBSLOT/_condor_stdout | tail -1 | sed -e s/subject\ \ \ :/glideinrun/ \`">> /tmp/findglides_$$
#get grid id of slot multicore ...
echo MYJOBSLOTS="( \`ls  \$MYWNDIR/glide_*/log/StarterLog* \`)" >> /tmp/findglides_$$
echo "for IISLOT in \$(seq 0 \$((\${#MYJOBSLOTS[@]} - 1 )))" >> /tmp/findglides_$$
echo "do" >> /tmp/findglides_$$
echo   JOBSLOT="\${MYJOBSLOTS[\$IISLOT]}" >> /tmp/findglides_$$
#echo   MYGRIDID="\$MYGRIDID:JOBSLOT=\$JOBSLOT" >> /tmp/findglides_$$
echo   JOBDIR="\`cat \$JOBSLOT | grep IWD: |  tail -1 | cut -d\  -f5 \`">> /tmp/findglides_$$
#echo   MYGRIDID="\$MYGRIDID:JOBDIR=\$JOBDIR">> /tmp/findglides_$$
echo   "MYGRIDIDi=" >> /tmp/findglides_$$
echo   "[ -f \$JOBDIR/_condor_stdout ]&&"  >> /tmp/findglides_$$
echo   MYGRIDIDi="\`grep subject \$JOBDIR/_condor_stdout | tail -1 | sed -e s/subject\ \ \ :/glideinrun/ \`">> /tmp/findglides_$$
echo   "let  jj=IISLOT+1" >> /tmp/findglides_$$
echo   MYGRIDID="\$MYGRIDID:slot\$jj=\$MYGRIDIDi">> /tmp/findglides_$$
echo "done" >> /tmp/findglides_$$
#old#echo "[ -f \$MYWNDIR/glide_*/execute/dir_*/_condor_stdout ]&&"  >> /tmp/findglides_$$
#old#echo "#Get GridID if exist" >> /tmp/findglides_$$
#old#echo MYGRIDID="\`grep subject \$MYWNDIR/glide_*/execute/dir_*/_condor_stdout | tail -1 | sed -e s/subject\ \ \ :/glideinrun/ \`" >> /tmp/findglides_$$
#old#echo MYGRIDID="\`grep subject \$MYWNDIR/glide_*/execute/dir_*/_condor_stdout | tail -1 | cut -d:  -f2\`" >> /tmp/findglides_$$
echo "#GridId =\":slot1=\", reset" >> /tmp/findglides_$$
echo "[ \"\$MYGRIDID\" == \":slot1=\" ]&&MYGRIDID=" >> /tmp/findglides_$$
echo "#No GridId, Check if glidein pilot exist" >> /tmp/findglides_$$
echo "[ \"\$MYGRIDID\" == \"\" ]&&[ -f \$MYWNDIR/glide_*/grid-mapfile ]&&" >> /tmp/findglides_$$
echo MYGRIDID="\`grep condor \$MYWNDIR/glide_*/grid-mapfile | sed -e s/condor/glideinpilots/ |sed -e s/\\\"//g\`" >> /tmp/findglides_$$
echo "[[ \"\$MYGRIDID\" == *glideinpilot* ]]&&" >> /tmp/findglides_$$
echo MYGRIDID=\"\`echo glideinpilot \${MYGRIDID%%glideinpilots*} \` \" >> /tmp/findglides_$$
echo "#No GridId, Other" >> /tmp/findglides_$$
echo "[ \"\$MYGRIDID\" == \"\" ]&&[ -f \$MYWNDIR/x509_user_proxy ]&&" >> /tmp/findglides_$$
echo MYGRIDID="\`openssl x509 -noout -issuer -in \$MYWNDIR/x509_user_proxy | sed -e s/issuer=/jobproxy/g \`" >> /tmp/findglides_$$
echo "echo \"\$MYGRIDID,\" " >> /tmp/findglides_$$
done
MYSCRIPT=`cat /tmp/findglides_$$`
rm /tmp/findglides_$$
MYGRIDID=`ssh -Y $MYNODE "$MYSCRIPT"` 

echo $MYGRIDID
}

#get output from condor
OLD_IFS=$IFS;IFS=$'\n';lines=( $(condor_q -run $1) );IFS=$OLD_IFS
 
#preprocess condor output (get nodes/slots info)
for idx in $(seq 0 $((${#lines[@]} - 1)))
do
    line="${lines[$idx]}"
    linesid[$idx]="list"
    #tag empty lines
    [ "$line" == "" ] && linesid[$idx]="blank"
    #tag info lines
    [[ "$line" == *"--"* ]]&&linesid[$idx]="title" 
    [[ "$line" == *"ID"* ]]&&[[ "$line" == *"OWNER"* ]]&&linesid[$idx]="title" 

    #get node/slot info
    words=( $line )
#debug#    echo "words[0] = ${words[0]}"
    jobid="${words[0]}"
#    [[ "${linesid[$idx]}" == "list" ]]&&nodeslot=(( echo "${words[5]}" | tr "@" " " ))
    OLD_IFS=$IFS;IFS=$'@';nodeslot=( ${words[5]} );IFS=$OLD_IFS
    pairinfo[$idx]="${nodeslot[1]} ${nodeslot[0]}"
    jobinfo[$idx]="$jobid"

    #organize nodes/slots into array of strings
    slot=0
    for inode in $(seq 0 $((${#slots[@]} - 1)))
    do
      [[ "${slots[$inode]}" == *"${nodeslot[1]}"* ]]&&slots[$inode]="${slots[$inode]} ${nodeslot[0]}"&&slot=1&&jobs[$inode]="${jobs[$inode]} $jobid"
      [[ "$slot" == "1" ]]&&reverse[$idx]=$inode&&pair[$inode]="${pair[$inode]} $idx"&&break
    done
    [[  "${linesid[$idx]}" == "list" ]]&&[[ "$slot" == "0" ]]&&
    jobs[${#jobs[@]}]="$jobid"&&
    slots[${#slots[@]}]="${nodeslot[1]} ${nodeslot[0]}"&&
     reverse[$idx]=$((${#slots[@]} - 1))&&
     pair[${reverse[$idx]}]=$idx

#debug test
#    echo "${linesid[$idx]}($idx) - ${lines[$idx]} (${pair[${reverse[$idx]}]}) ${slots[${reverse[$idx]}]}"
done 

#ssh to running nodes
echo "Start ssh to ${#slots[@]} nodes "
for inode in $(seq 0 $((${#slots[@]} - 1)))
do
   OLD_IFS=$IFS;IFS=" ";islot=( ${slots[$inode]} );IFS=$OLD_IFS
   OLD_IFS=$IFS;IFS=" ";ijob=( ${jobs[$inode]} );IFS=$OLD_IFS

   WNNODE=${islot[0]}
#old#   echo "Node ${islot[@]} ($((${#islot[@]}-1)) slots)"
   echo "Node $WNNODE ${ijob[@]} ($((${#ijob[@]}-0)) jobs)"
   #Get Certs of running user in node (ssh)
#old#   certs=`RunningGlideins "${islot[@]}"`
#debug#   echo "RunningGlideins \"$WNNODE ${ijob[@]}\""
   certs=`RunningGlideins "$WNNODE ${ijob[@]}"`
   OLD_IFS=$IFS;IFS=$',';listcerts=( $certs );IFS=$OLD_IFS
   #debug#echo "Node $WNNODE ${listcerts[@]}"

   #debug#   echo "pair=${pair[$inode]}"
   listpairs=( ${pair[$inode]} )

   #Sort certs and put it in a list by condor_q_line (listOfCerts)
   for ipair in $(seq 0 $((${#listpairs[@]} - 1)))
   do
      let  j=ipair+1
      listOfCerts[${listpairs[$ipair]}]=${listcerts[$ipair]}  
   done
done

#Print output condor_q like extended with certs
for idx in $(seq 0 $((${#lines[@]} - 1)))
do
#    echo "${linesid[$idx]}($idx) - ${lines[$idx]} ${listOfCerts[$idx]}"
    echo "${lines[$idx]} ${listOfCerts[$idx]}"
done
exit
