#! /bin/csh -f
# Last edited on 2008-01-12 16:08:08 by stolfi

set usage = "$0 [ -exec ] [ DIR... ]"

# Analyzes the consistency and shadowing of a list of directories.
# If "-exec" is given, considersonly executable files.
# If no directories are given, analyzes the current $path
# (and assumes "-exec").


set exec = 0
while ( ( $#argv > 0 ) && ( "/$1" =~ /-* ) )
  if ( ( $#argv > 0 ) && ( "/$1" == "/-exec" ) ) then
    set exec = 1; shift
  else
    echo "usage: ${usage}"; exit 1
  endif
end

if ( $#argv == 0 ) then
  set ds = ( $path )
  set exec = 1
else
  set ds = ( $* )
endif

set noglob
set here = $cwd

set t = /tmp/analyze-path-$$
/bin/rm -f $t.all
/bin/touch $t.all

foreach d ( $ds )
  echo -n "# $d" 
  if ( ! ( -e $d ) ) then
    echo " doesn't exist"
  else if ( ! ( -d $d ) ) then
    echo " not a directory"
  else if ( ! ( -r $d ) ) then
    echo " not readable"
  else if ( ! ( -x $d ) ) then
    echo " not searchable"
  else
    echo ""
    /bin/rm -f $t.loc $t.locs
    /bin/touch $t.loc
    cd $d
    foreach f ( `/bin/ls` )
      if ( -f "$f" ) && ( ( ! ${exec} ) || ( -x "$f" ) ) ) then
        echo "$f" >> $t.loc
      endif
    end
    /bin/sort $t.loc > $t.locs
    foreach tgop ( +/1-2 -/1.2 )
      set tag = "${tgop:h}"
      set op = "${tgop:t}"
      ${STOLFIHOME}/bin/${PLATFORM}/bool ${op} \
        $t.locs $t.all > $t.${tag}
      ls -l `/bin/cat $t.${tag}` \
        | gawk -v tag="${tag}" \
            ' /./{ \
                gsub(/ *-> */, "->", $0); \
                printf "%s %10s %11d %3s %2d %5s %s\n", tag, $1, $5, $6, $7, $8, $9; \
              } \
            ' 
    end
    cd $here
    /bin/sort -m $t.all $t.+ > $t.merge
    /bin/mv $t.merge $t.all
    /bin/rm -f $t.loc $t.locs $t.+ $t.-
  endif
end
    
