#!/bin/csh -f
#
# script by bak 3 dec 2014 for cook to allow for techsas files to be sorted into a directory
#
# modded by bak at nocs on eriu for dy031 15 may 2015, modded to run as a crontab task
#* * * * 1,2,3,4,5,6,7 /local/users/pstar/cruise/data/exec/techsas_linkscript >> & /dev/null


cd /local/users/pstar #~/h # cludge on eriu when pstar home directory is wrong
cd cruise/data/techsas
cd netcdf_files_links_mac

set now = `date +%Y%m%d%H%M%S`
set link_log = /local/users/pstar/cruise/data/techsas/techsas_link_logs/techsas_link_log_mac_${now}
echo " " >>! $link_log
echo `date` >>! $link_log

foreach i (`/bin/ls ../netcdf_files_rawdir_mac/*/*`)
 set i1 = `echo $i | awk '{print index($1,"/")}'`
 set i2 = `echo $i  $i1| awk '{print substr($1,$2+1)}'`
set i3 = `echo $i2 | awk '{print index($1,"/")}'`
set i4 = `echo $i2  $i3| awk '{print substr($1,$2+1)}'`
set i5 = `echo $i4 | awk '{print index($1,"/")}'`
set i6 = `echo $i4  $i5| awk '{print substr($1,$2+1)}'`
set i7 = `echo $i | awk '{print substr($1,1,22)}'`
set i8 = `echo $i | awk '{print substr($1,23,length($1))}'`
set i9 = `echo $i7$i8`

set linkfile = $i6
set techfile = $i9

if (! -e $linkfile) then
 if ( -s $i) then
   set sss = ` /bin/ls -l $i`
   set ss2 = `echo $sss | awk '{print $5}'`
   echo $sss
   echo $ss2
      if ($ss2 > 10000) then
         echo "  making link $linkfile for raw file $i " >>! $link_log
            ln -s $techfile $linkfile
      else
         echo "  ignoring $linkfile length $ss2 is less than 10k bytes " >>! $link_log
      endif
 else
   echo "  ignoring $linkfile has zero length " >>! $link_log
 endif
endif
end