#!/bin/bash
# Web Site Statistics

#############################################
# Defaults - adjust these
#--------------------------------------------
# directory containing local copy of the site
local=/var/www/html/michel/
#--------------------------------------------
# output path prefix
prefix=http://localhost/michel/
#--------------------------------------------
# do not list html files
list="stat"
#############################################

output=""

usage() {
    echo "$(basename $0) [-h] [-a | -l] [-d srcdir] [-p prefix]"
    echo "  -h         this help message"
    echo "  -a         list all (site statistics and html files)"
    echo "  -l         list html files only"
    echo "  -d srcdir  web site directory            (default: $local)"
    echo "  -p prefix  concatenate file path prefix  (default: $prefix)"
    echo "  -p ''      will remove default path prefix"
}

while getopts ':halp:d:' OPTION; do
    case "$OPTION" in
        h)
            usage
            exit 0
            ;;
        a)  list="all"
            ;;    
        l)
            list="files"
            ;;
        p)
            prefix="$OPTARG"
            ;;
        d) 
            local="$OPTARG"    
            ;;
        *)
            usage
            exit 1
            ;;
    esac
done

if [ ! -d "$local" ]; then
  echo "$local does not exist"
  exit 2
fi

#length of local 
len=${#local}

if [ $list != "files" ]; then
    echo    "Location of site:             $local"
    echo -n "Number of files:              "; find $local -type f | wc -l
    echo -n "Number of HTML files:         "; find $local -type f -name '*html' | wc -l
    echo -n "Number of English HTML files: "; find $local -type f -name '*en\.html' | wc -l
    echo -n "Number of French HTML files:  "; find $local -type f -name '*fr\.html' | wc -l
    echo -n "Number of JPEG image files:   "; find $local -name '*jpg' | wc -l
    echo -n "Number of PNG image files:    "; find $local -name '*png' | wc -l
    echo -n "Number of downloads:          "; find $local -wholename '*dnld/*' | wc -l
    echo -n "Number of ZIP archives:       "; find $local -name '*zip' | wc -l
    echo -n "Number of bash scripts:       "; find $local -name '*sh' | wc -l
    echo -n "Number of Pascal files:       "; find $local -name '*pas' | wc -l
    echo -n "Number of Arduino sketches:   "; find $local -name '*ino' | wc -l
    echo -n "Number of C files:            "; find $local -name '*c' | wc -l
    echo -n "Number of PDF files:          "; find $local -name '*pdf' | wc -l
fi

if [ $list != "stat" ]; then
  # list files if list == all or list == files
  tree  -f -i --noreport --dirsfirst -I 'index.html' -P '*.html' $local | sed -r "s/.{$len}//" | sed '/html$/!d' | sed -e "s#^#$prefix#"
  
fi
