#!/usr/bin/sh

############################################################################
#
#  Usage:  ccounter filename1.c filename2.c ...
#
#  'ccounter' handles a heap of source files by using 'ccount', a program
#  that supplies results of the structure of a C source code.
#  See 'README.1' for more details!
#
#  Author: Joerg Lawrenz, Universitaet Karlsruhe
#  Date:   93/12/1
#
############################################################################

if test $# -eq 0
  then
       echo "Usage: $0 filename1.c filename2.c ..."
       exit
fi

files="$*"

#
#  Determine the number of all input files.
#

num_files=`ls $files | wc -w`

echo "Files total: $num_files"

if test $num_files -eq 0 
   then exit
fi

#
#  Now ask for the step size by which the files should be selected.
#

if test $num_files -eq 1
   then step=1
   else echo "Please enter size of step:"
        read step
        if test $step 
           then :
           else echo "1"
                step=1
        fi
fi

#
#  Now determine the number of selected source files.
#

max_files=`expr $num_files / $step`

number=1       # current sequence number of all input files
index=1        # current sequence number of selected files
success=0      # number of files that are successfully parsed

#
#  Now apply 'ccount' to each selected source file, perhaps repeatedly
#  by one or the other, depending on with which status 'ccount' will exit.
#  Only if the status is 0 (-> success) or 1 (-> abandoning), take the next 
#  source file. If the status is 3 (-> show tokens), repeat the run with 
#  the -v option for 'ccount', otherwise if the status is 2 (-> entry 
#  occured in 'basename.tpp' or -> source file has been edited), just repeat.
#

for file in $files
do
    if test `expr $number % $step` -eq 0
       then
              status=2
              while test $status -ge 2
              do
                echo
                echo "$index/$max_files: $file"
                if test $status -eq 2
                   then ccount $file
                   else ccount -v $file
                fi
                status=$?
              done

              if test $status -eq 0
                 then success=`expr $success + 1`
              fi

              index=`expr $index + 1`
       fi
    number=`expr $number + 1`
done

echo "$success file(s) successfully parsed."
