#!/bin/bash
scriptPath=`dirname $0`
removeFlag=0
quietFlag=0
copyrightTextFile=""
filePattern=""

function usage() {
    echo "installCopyright.sh"
    echo "  Insert/update/remove copyright text in each of the files matching the file"
    echo "  pattern.  The text will be placed at the top of the file and delimited by"
    echo "  begin and end tokens.  Changing these tokens will prevent this script"
    echo "  from removing/updating the copyright statements."
    echo ""
    echo "  Usage: installCopyright.sh [-hqr] [-c <copyrightTextFile>] [-p <filePattern>]"
    echo "  Parameters:"
    echo "    -h: Help text"
    echo "    -q: Suppress informational messages"
    echo "    -r: Remove copyright text from all files matching pattern"
    echo "    -c copyrightTextFile:  Path to file containing copyright text"
    echo "    -p filePattern:  filename or wildcard pattern.  Will be applied recursively to the current directory"
    exit 1
}

#
# Backup original files based on pattern
#
function backupFiles() {
    [[ $# -ne 1 ]] && echo Invalid call to backupFiles && exit 1
    [[ quietFlag -lt 1 ]] && echo Making backup of target files and storing in copyrightBackup.tgz
    find . -name "$1" | xargs tar -zcf copyrightBackup.tgz 
}

#
# Entry point for script
#

#
# Process the arguments
#
while getopts qhrc:p: arg
do
    case "$arg" in 
        r)
            removeFlag=1
            ;;
        q)
            quietFlag=1
            ;;
        c)
            copyrightFile=$OPTARG
            ;;
        p)
            filePattern=$OPTARG
            ;;
        h)
            usage
            ;;
        [?]) usage
            ;;
    esac
done
#echo removeFlag $removeFlag
#echo copyrightFile $copyrightFile
#echo filePattern $filePattern

#
# If adding copyright text, the file must be found.
#
if [[ removeFlag -eq 0 ]] && ! [ -e $copyrightFile ] 
then
    echo ERROR: File not found: $copyrightFile ; exit 1
fi


#
# Create temp copyright file with delimiters
#
if [[ removeFlag -eq 0 ]] 
then
    sed -e'1i\
    \/* __copyright_begin__
    ;$a\
    __copyright_end__ *\/
    ' $copyrightFile > copyrightTemp.txt
fi

#
# Create a backup of all files matching the pattern
#
backupFiles $filePattern 


#
# Iterate over all files mataching the pattern and for each file:
# -- Remove current copyright notice if present and inserting/removing
# -- Insert new copyright notice if applicable
#
for fileName in `find . -name "$filePattern"`; do
    #echo Working on $fileName
    copyrightDelim=`grep -m 1 -wc __copyright_begin__ $fileName`
    if [[ removeFlag -eq 1 ]]
    then
        if [[ copyrightDelim -gt 0 ]]; then
            [[ quietFlag -lt 1 ]] && echo "INFO: Removing copyright from: $fileName"
            sed -e "/__copyright_begin__/, /__copyright_end__/ d" $fileName > $fileName.temp
            mv $fileName.temp $fileName
        fi
    else
        if [[ copyrightDelim -gt 0 ]] 
        then
            [[ quietFlag -lt 1 ]] && echo "Replacing copyright in: $fileName"
            sed -e "/__copyright_begin__/, /__copyright_end__/ d" $fileName > $fileName.temp
            cat copyrightTemp.txt $fileName.temp > $fileName
            [ -e $fileName.temp ] &&  rm $fileName.temp 
        else
            [[ quietFlag -lt 1 ]] && echo "Inserting copyright into: $fileName"
            cat copyrightTemp.txt $fileName > $fileName.temp
            mv $fileName.temp $fileName
        fi
    fi
done

#
# Cleanup temp files.
#
[ -e copyrightTemp.txt ] &&  rm copyrightTemp.txt 
[[ quietFlag -lt 1 ]] && echo INFO: Done. 
[ -e copyrightBackup.tgz ] && [[ quietFlag -lt 1 ]] && echo INFO: Delete copyrightBackup.tgz if process successful

exit 0

