#! /bin/bash
# Last edited on 2012-12-08 21:29:11 by stolfilocal

PROG_NAME=${0##*/}
PROG_DESC="find all files under given directories; print size, mod time, name"
PROG_HELP=(
  "${PROG_NAME} [-nsec] {DIR}.."
)
PROG_INFO=(
  "\nNAME"
  "\n  ${PROG_NAME} - ${PROG_DESC}."
  "\n"
  "\nSYNOPSIS"
  "\n  ${PROG_HELP[@]}"
  "\n"
  "\nDESCRIPTION"
  "\n  Writes to stdout a list of all ordinary files"
  "\n  in the specified directories."
  "\n"
  "\n  For each file, prints: the size in bytes, the last-modified time"
  "\n (in the format YYYY-MM-DD-hhmmss), and the file's pathname."
  "\n" 
  "\nOPTIONS"
  "\n  -nsec."
  "\n    If this option is present, the time field ends with a"
  "\nfraction of second consisting of a '.' and nine decimal"
  "\ndigits (nanoseconds).  Otherwise the time field has just"
  "\nwhole seconds."
  "\n"
  "\nSEE ALSO"
  "\n  find(1)"
  "\nAUTHOR"
  "\n  Created 2007-01-17 by Jorge Stolfi, Unicamp"
)

# ----------------------------------------------------------------------
# INTERNAL OPTIONS

# ----------------------------------------------------------------------
# COMMAND LINE PARSING



# Parse command line switches: 
nseccmd=( sdf-file-remove-nsec )
# bogus=( NOTBOGUS )
while [[ ( $# -ge 1 ) && ( "/$1" =~ /-.* ) ]]; do
  if [[ ( $# -ge 1 ) && ( "/$1" == "/-nsec" ) ]]; then 
    nseccmd=( sdf-file-put-nsec ); shift;
#   elif [[ ( $# -ge 2 ) && ( "/$1" == "/-bogus" ) ]]; then 
#     bogus=( BOGUS "$2" ); shift; shift;
  else
    echo "unknown option $1" 1>&2 ;
    echo -e "usage:\n  ${PROG_HELP[@]}" 1>&2 ; exit 1 
  fi
done 

# Get positional parameters
dirs=( \
  ` echo "$@" \
      | tr ' ' '\012' \
      | sed -e 's:^\$:./:' -e 's:[/]*\$:/:' \
  ` \
)
if [[ ${#dirs[@]} == 0 ]]; then
  dirs=( ./ )
fi

# # Check for leftover arguments:
# if [[ $# -ne 0 ]]; then
#   echo 'wrong number of arguments "'"$1"'" ...' 1>&2
#   echo -e "usage:\n  ${PROG_HELP[@]}" 1>&2 ; exit 1 
# fi

# END COMMAND LINE PARSING
# ----------------------------------------------------------------------

# Get files and remove any leading "./" and fractional part of seconds:
find "${dirs[@]}" \
    -type f \
    -printf "%12s %TY-%Tm-%Td-%TH%TM%TS %p\n" \
  | sed -e 's: [.]/: :g' \
  | ${nseccmd[@]} 
