#!/bin/csh
#
# Name: ys_inctar
#
# Purpose: create incremental tar file via rdist compare output
#
# History: 14-Aug-1993 (SLF) - for remote SW upgrades
# 	   26-Oct-1993 (SLF) - use loop (for case where number files > 512)
#			       (slower but it works and produces correct output)
#	   13-Dec-1993 (SLF) - add ys_preclean call, fix bug in sed
#	   25-Jan-1994 (SLF) - mail warning if:
#				  number of tar files too large
#				  number of characters in tar cmd too large
#	   28-Feb-1995 (SLF) - update tar command for OSF compatibility
#          12-mar-1997 (SLF) - update for V:970213.07
#
if (-e $DIR_SITE_SCRIPT/ys_preclean) then
   echo running $DIR_SITE_SCRIPT/ys_preclean 
   echo "Clean INHIBITED"
#  source $DIR_SITE_SCRIPT/ys_preclean
endif 
#
# Use rdist to compare current /ys against current 'tar' contents /0p/ys_currev
set rd_cr_log1=$DIR_SITE_LOGS/rdist_currev.log1
rdist -vhf $DIR_SITE_SCRIPT/rdist.daily_currev > $rd_cr_log1

# Search rdist output for relavent entries (updates/installs)
set rd_cr_log2=$DIR_SITE_LOGS/rdist_currev.log2
grep -i "need to install:" $rd_cr_log1 >  $rd_cr_log2	
grep -i "need to update:"  $rd_cr_log1 >> $rd_cr_log2	
#
# slf 13-dec-1993 (use temp files since sed pipe gave Arg too long error
# use sed to whittle down the rdist output to just ys-relative file names 
set rd_cr_log3=$DIR_SITE_LOGS/rdist_currev.log3
set rd_cr_log4=$DIR_SITE_LOGS/rdist_currev.log4
#sed s/"need to install: \ /ys \/"/""/g $rd_cr_log2 > $rd_cr_log3
sed s/"need to install: \/0p\/yohkoh\/"/""/g $rd_cr_log2 > $rd_cr_log3

#sed s/"need to update: \/ys\/"/""/g  $rd_cr_log3 > $rd_cr_log4
sed s/"need to update: \/0p\/yohkoh\/"/""/g  $rd_cr_log3 > $rd_cr_log4
set files=`cat $rd_cr_log4`

echo Number of files in incremental tar: $#files
# slf 25-Jan-1994 - add warnings when it gets cumbersome
set nfiles=$#files
set nchar=`echo $files | wc -c`
set inctar_mail=$DIR_SITE_LOGS/inctar_mail
rm -f $inctar_mail

echo "Message from Yohkoh Incremental Tar File Generation" > $inctar_mail
echo " " >> $inctar_mail
set incmail=0
set flim=400
set clim=10000
if ($nfiles > $flim) then
   echo "Number of files in incremental has exceeded $flim ($nfiles)" >> $inctar_mail
   set incmail=1
endif
if ($nchar > $clim) then
   echo "Number of characters in tar command has exceeded $clim ($nchar)" >> $inctar_mail
   set incmail=1
endif

if ($incmail) then
   echo " " >> $inctar_mail
   echo "Please consider generating a minor software release..." >> $inctar_mail
   set subj="Incremental Tar File Warning"   
   set wmail=`which Mail`
   if ($#wmail > 1) set wmail=`which mail`
   $wmail -s "$subj" software@isass0.solar.isas.ac.jp < $inctar_mail
endif

# now make the tar file 
# tar options=create/verbose/suppress_dir/outfile
set version=`"$DIR_GEN_SCRIPT/get_ysversion"`
set tfile="$ys/swmaint/tar/increm.$version"
if ($#files > 400) then
   tar -covf $tfile -C /ys $files[1-300] # create the tar set w/first
#  loop through rest and use tar append (could use -c for [1-512] for speed?)
   set cnt = 301
   while ($cnt <= $#files) 
      tar -rovf $tfile -C /ys $files[$cnt] 
      @ cnt = $cnt + 1
   end
else
#  faster to pass all files directly to tar - use if nfiles < 512
   tar -covf $tfile -C /ys $files
endif

# now compress tar file
rm /0p/ftp/pub/swmaint/tar/increm.$version.Z	####### kludge ########
compress -vf $tfile

# make links in anonymous ftp area
if (-e $ys/site/script/mk_publinks) source $ys/site/script/mk_publinks

exit
   
