#!/bin/csh -f

# Beginning is similar to cleanpath, but here we throw out all
# duplicates and nonexistent directories even if they are relative
# paths.

set templs = /tmp/pathcheck.ls.$user.$$
set tempdirs = /tmp/pathcheck.dirs.$user.$$

rm -f $templs $tempdirs
touch $templs

set pathdirs = ($*)
if ($#argv < 1) set pathdirs = ($path)

foreach dir ($pathdirs)
  (ls -idL $dir >> $templs) >& /dev/null
end

awk '{if (seen[$1] != "yes") print $2}\
         {seen[$1] = "yes"}' $templs > $tempdirs

set pathdirs = (`cat $tempdirs`)

rm -f $templs $tempdirs

rm -f pcheck pcheck.log
touch pcheck pcheck.log
set i = 0
while ($i < $#pathdirs)
  @ i = $i + 1
  set idir = $pathdirs[$i]
  ls $idir > pcheck.i
  set j = $i
  while ($j < $#pathdirs)
    @ j = $j + 1
    set jdir = $pathdirs[$j]
    ls $jdir > pcheck.j
    echo $idir $jdir >> pcheck.log
    foreach file (`comm -12 pcheck.i pcheck.j`)
      if (-f $idir/$file && -x $idir/$file && \
          -f $jdir/$file && -x $jdir/$file) then
        echo $file >> pcheck.log
        diff {$idir,$jdir}/$file >& pcheck.diff
        if ($status) then
          echo $idir $jdir $file >> pcheck
          cat pcheck.diff >> pcheck
          echo '' >> pcheck
        endif
      endif
    end
    echo '' >> pcheck.log
  end
end

cat pcheck | packpars | sort -k 3 | unpackpars > pcheck.`hostname`

rm -f pcheck.i pcheck.j pcheck.diff pcheck
