#!/bin/ksh

storm=$1
lat=$2
lon=$3
indate=$4
intime=$5
echo a=$storm
echo b=$lat
echo c=$lon
echo d=$indate
echo e=$intime

#historydir=$HOME/aodt/ADTV7.2.3/history/
#curdir=$HOME/aodt/ADTV7.2.3/scripts

historydir=$HOME/ADTV8.2.1/history/
curdir=$HOME/ADTV8.2.1/scripts

checkfile=$historydir$storm'.ODT'
echo checkfile=$checkfile


if [ -f $checkfile ]
 then
# history file exists... move on
  echo "HISTORY FILE FOR $checkfile EXISTS... exiting"
  echo "valid= -1"
else
# history file does not exist... check for boundary cross
# and existing history file adjoining basin, if applicable
# Changed the threshold from 1.0 to 2.0 PR000176 TN 01/04/2011
  #E90valid=`$curdir/subtract $lon -90 1.0`
  #E160valid=`$curdir/subtract $lon -160 1.0`
#-- test subtract
#`$curdir/subtract`
  E90valid=`$curdir/subtract $lon -90 2.0`
  E160valid=`$curdir/subtract $lon -160 2.0`
  echo "E90=$E90valid E160=$E160valid"

  let ilat=`echo $lat |sed -e s/[.]/\ / `
  echo ilat=$ilat
  let runit=-1
  if [ $ilat -lt 0 ]
   then
    # Changed the threshold from 1.0 to 2.0 PR000176 TN 01/04/2011
    #E90valid=`$curdir/subtract $lon -90 1.0`
    #E160valid=`$curdir/subtract $lon -160 1.0`
    E90valid=`$curdir/subtract $lon -90 2.0`
    E160valid=`$curdir/subtract $lon -160 2.0`
    echo E90=$E90valid E160=$E160valid
    if [[ $E90valid -eq 1 || $E160valid -eq 1 ]]
     then
      let runit=1
    fi
  else
#--HT34337 change criteria from 1.0 to 2.0  08/20/2018
#    DLvalid=`$curdir/subtract $lon -180 1.0`
    DLvalid=`$curdir/subtract $lon -180 2.0`
    echo DL=$DLvalid
    if [ $DLvalid -eq 1 ]
     then
      let runit=1
    fi
  fi
  if [ $runit -eq 1 ]
   then
    echo "BOUNDARY CROSS DETECTED... checking old history files "
#    ls -1t $historydir*'.ODT' >$curdir/temp/listing
    ## No JTWC storm will be copied to other storms.
    ls -1t $historydir*'.ODT' |grep -v [0123456789][ABWSP] >$curdir/temp/listing
    lines=`wc $curdir/temp/listing | awk -F" " '{ print $1 }'`
    let cnt=1
    notfound=TRUE
    echo "HISTORY FILES TO CHECK=$lines"
    while [[ $cnt -le $lines && $notfound = "TRUE" ]]
     do
      hfile=`cat $curdir/temp/listing | sed -n ${cnt},${cnt}p | awk -F" " '{ print $1 }'`
      echo "checking $hfile"
      hdate=`tail -1 $hfile | awk -F" " '{ print $1 }'`
      htime=`tail -1 $hfile | awk -F" " '{ print $2 }'`
      hjulian=`tail -1 $hfile | awk -F" " '{ print $3 }'`
      hlat=`tail -1 $hfile | awk -F" " '{ print $12 }'`
      hlon=`tail -1 $hfile | awk -F" " '{ print $13 }'`
      echo "DATE=$hdate TIME=$htime JULIAN=$hjulian LAT=$hlat LON=$hlon"
      xyear=`echo $hdate | sed -e s/[A-Z]/\ /g | awk '{ print $1 }'`
      injulian=`$curdir/converttime $indate $intime`
      let xjulian=`echo $hjulian |sed -e s/[.]/\ / `
      if [ $xjulian -lt 10 ]
       then
        hjulian='00'$hjulian
      elif [ $xjulian -lt 100 ]
       then
        hjulian='0'$hjulian
      else
        hjulian=$hjulian
      fi
      datevalid=`$curdir/subtract $xyear$hjulian $injulian 1.0`
      echo $xyear$hjulian $injulian $datevalid
      if [ $datevalid -eq 1 ]
       then
#start TLO mods
        if [ $hlon -gt 0 ]
         then
           $hlon = '-'$hlon
        fi
        latvalid=`$curdir/subtract $lat $hlat 2.0`
        lonvalid=`$curdir/subtract $lon $hlon 2.0`
        if [[ $latvalid -eq 1 && $lonvalid -eq 1 ]]
         then
          echo "FOUND IT... "
          echo "MOVE HISTORY FILE $hfile "
          echo "TO $checkfile"
          cp $hfile $checkfile
          notfound=FALSE
        fi
#end TLO mods
      fi
      let cnt=cnt+1
    done
    if [ $notfound = "TRUE" ]
     then
       echo "valid= -1"
    else
       echo "valid= 1"
    fi
  else
    echo "NO BOUNDARY CROSS DETECTED... exiting"
    echo "valid= -1"
  fi 
fi
exit
