#!/bin/tcsh -f

set statu = 0

if ("$1" == "-h" || "$1" == "-help") then 
   goto HELP
endif

goto PARSE
RETURN_PARSE:

DO_IT:
foreach ff ($others_list)
   if ( ! -f $ff ) then
      echo "File $ff not found"
      goto BEND
   endif
   set good = 1

   #Look for blank(s) following \ at end of line
   set l = `\grep -nE '\\[ \t][ \t]*$' $ff`
   set lt = `file_tool -show_file_type -infiles $ff`
   set lt = `echo $lt | cut -f 2 -d :`
   #set ln = `\grep '[^[:print:]]' $ff`
   set ln = ''
   if ("$l" != "" || "$ln" != "" || "$lt" != "UNIX") then
      if ($clean == 0) then
echo "$ff has improperly terminated lines, non-printable characters, or DOS type"
         echo "$l"
         echo "Use -clean option to cleanup"
         set good = 0
      else 
         set isexec = 0
         if ( -x ${ff} ) then
            set isexec = 1
         endif
         if ( -f ${ff}${suffix} ) then
            echo "${ff}${suffix} alredy exists. Will not clobber"
            goto BEND
         endif
         cp -p ${ff} ${ff}${suffix}
         if ( ! -f ${ff}${suffix} ) then
            echo "Failed to create copy ${ff}${suffix} of unclean file $ff"
            goto BEND
         endif
         sed 's/\(\\[ \t][ \t]*$\)/\\/g' $ff${suffix} | \
            tr -d '\r' | tr -cd '\11\12\15\40-\176' \
               > ${ff}
         if ( $isexec == 1) chmod u+x $ff
         echo "$ff's old unclean version is ${ff}${suffix}"
         file_tool -show_file_type -infiles ${ff}
         set good = 0
      endif
   endif 
   
   if ($good) then
       echo "$ff is good"
   endif
end

goto END


HELP:
   echo ""
   echo "Usage: `basename $0` [-clean] [-suffix SUFF] <Script1> [Script2 ...]"
   echo ""
   echo "Checks script(s) for improperly terminated lines"
   echo "   -clean: Clean bad line breaks"
   echo "   -suffix SUFF: Rename uncleaned file Script1.SUFF"
   echo "                 The default for SUFF is .uncln"
   echo ""
   echo "Example:"
   echo '   echo "A good line" > ./___toy'
   echo '   echo "A good break \" >> ./___toy'
   echo '   echo "A harmless \ slash" >> ./___toy'
   echo '   echo "A bad break \  " >> ./___toy'
   echo '   echo "The end" >> ./___toy'
   echo ""
   echo "To find the bad line breaks"
   echo "   `basename $0` ___toy"
   echo ""
   echo "To find and clean the bad line breaks"
   echo "   `basename $0` -clean ___toy"
   echo ""
   echo "The uncleaned (original) file goes into ___toy.uncln"
   echo ""
   echo "Use file_tool -show_file_type -infiles YOURFILE"
   echo "To check for non-printable characters, and a whole lot more."
   echo ""
   
goto END

PARSE:
	set Narg = $#
	
	#find the locations of -ts and -roi, etc
	set verb = 0
   set cnt = 1
	set check = 1
	set clean = 0
   set suffix = '.uncln'
   set filelist = ()
   while ($cnt <= $Narg)
      set gotopt = 0
      if ("$argv[$cnt]" == "-suffix") then
         set gotopt = 1
         set SubLoc = $cnt		
		   if ($SubLoc == $Narg) then
				echo "Need option after -suffix"
            goto END
			else
				@ cnt ++
				set suffix = "$argv[$cnt]"
            set NxtInd = $cnt
			endif
      endif
		
      
      if ("$argv[$cnt]" == "-clean") then
         set gotopt = 1
         set NxtInd = $cnt
		   
         set clean = 1
      endif
            
      if ("$argv[$cnt]" == "-verb") then
         set gotopt = 1
         set NxtInd = $cnt
		   
         set verb = 1
      endif
		
      if ($gotopt == 0) then
         #if here then get out
         set cnt = $Narg
         @ cnt ++
      endif
       
		@ cnt ++
	end

@ NxtInd ++
set others_list = ( $argv[$NxtInd-$#argv] )

if ($verb) echo "$others_list"
if ($#others_list == 0) then
   echo "No script names given"
   goto BEND
endif

goto RETURN_PARSE

BEND:
   echo "Terminating in error"
   set statu = 1
   goto END
   
END:
exit $statu
