#!/bin/bash
#=============================================================
# Mike Benjamin, September 12th, 2018
#=============================================================
# This script will search all subdirectories for _alvtmp folders.
# These folders are caches generated by alogview when alogview
# first opens an alog file. The _alvtmp folder splits out all
# the information in an alog file into dedicated files per
# variable. They can be safely deleted (as long as the original
# alog file remains). The only drawback for deleting them is
# that alogview will need to re-generate them again when/if
# the user later launches alogview on that file.

#----------------------------------------------------------
#  Part 1: Initialize variables
#----------------------------------------------------------
DELETE="no"
VERBOSE=""

#----------------------------------------------------------
#  Part 2: Check for and handle command-line arguments
#----------------------------------------------------------
for ARGI; do
    if [ "${ARGI}" = "--help" -o "${ARGI}" = "-h" ] ; then
        echo "alvrm.sh [OPTIONS]                                      " 
	echo "                                                        "
	echo "SYNOPSIS                                                "
	echo "  This script will look for and optionally remove all   "
	echo "  _alvtmp subfolders recursively found from the current "
	echo "  directory. These folders are temporary folders crested"
	echo "  by alogview when it first opens an alog file. They are"
	echo "  caches, used for speeding up alogview. They can be    "
	echo "  safely deleted with no loss of log file data, but     "
	echo "  alogview will regenerate them when/if later launched  "
	echo "  with the same alog file.                              "
	echo "                                                        "
        echo "  --help,    -h      Display this help message          " 
        echo "  --info,    -i      Output brief description of script "  
        echo "  --delete,  -d      Delete all _alvtmp folders found.  "  
        echo "  --verbose, -v      Verbose output when deleting       "  
        exit 0;
    elif [ "${ARGI}" = "--info" -o "${ARGI}" = "-i" ]; then
	echo "Remove alogview cache _alvtmp folders recursively       "
	exit
    elif [ "${ARGI}" = "--delete" -o "${ARGI}" = "-d" ]; then
	DELETE="yes"
    elif [ "${ARGI}" = "--verbose" -o "${ARGI}" = "-v" ]; then
	VERBOSE="-v"
    else 
        echo "Bad arg:" $ARGI "Run with -h for help."
        exit 1
    fi
done

if [ "${DELETE}" = "yes" ]; then
    find . -name '*_alvtmp'  -print -exec rm -rf ${VERBOSE} {} \;
else
    find . -name '*_alvtmp'
fi

exit 0
