#!/bin/bash

# Usage
#----------------------------------------------------------
help()
{
  cat <<HELP
Usage: modified.sh OPTIONS
    -l | --list          Lists all files in the list file.
    -t | --test          Test the existence of all files in the list file.
    -a | --add FILE      Add FILE to the list file.
    -c | --comment       Add a comment to the list file.
    -r | --remove FILE   Remove FILE from the list file.
    -b | --backup        Backups up all files.
    -e | --edit          Edit the list file.
    -h | --help          This text.

NOTE! Directories are always handled recursively.

The list file is /etc/modified.list
HELP
  exit 0
}

# Check script requirements
#----------------------------------------------------------


# Default variables
#----------------------------------------------------------
LISTFILE=/etc/modified.list
BACKUPDIR=/mnt/archive1/Backup/modified-`date +%Y-%m-%d`
cmd=unset
EDITOR=emacs

# Parse command line options
#----------------------------------------------------------
while [ -n "$1" ]; do
case $1 in
    -l | --list)    cat $LISTFILE | grep "^[^\ #]" ; exit 0 ;;
    -t | --test)    cmd="test"              ; shift 1 ; break ;;
    -a | --add)     cmd="add"               ; shift 1 ; break ;;
    -c | --comment) cmd="comment"           ; shift 1 ; break ;;
    -r | --remove)  cmd="remove"            ; shift 1 ; break ;;
    -b | --backup)  cmd="backup"            ; shift 1 ; break ;;
    -e | --edit)    sudo $EDITOR $LISTFILE  ; exit 0 ; break ;;
    -h | --help)    help                    ; exit 0 ;;
    -*) echo "error: no such option $1."    ; exit 1 ;;
    *)  break;;
esac
done

# Verify number of remaining command line arguments
#----------------------------------------------------------


# Main script
#----------------------------------------------------------

# Create the list file if it doesn't exist.
if [ ! -e $LISTFILE ] ; then
    touch $LISTFILE;
    if [ $? -ne 0 ] ; then 
        echo "Could not create the list file. You will have to create the file"
        echo "\"$LISTFILE\" manually and make it writable to the user that will"
        echo "be using modified.sh."
        exit 1
    fi
fi

if [ $cmd == "add" ]
then
    # Check number of parameters
    if [ $# \< 1 ]; then
        echo "Wrong number of parameters!"
        help
        exit 1
    fi

    # Get the full path to the file
    DIRNAME=`dirname "$1"`
    FULLPATH=$(cd "$DIRNAME" && echo `pwd`/`basename "$1"`)

    # Check if the file already exists in the list file
    if ( grep -x "$FULLPATH" <$LISTFILE >/dev/null 2>&1 ) ; then
        echo "ERROR: Target \"$FULLPATH\" is already in the list file."
        exit 1
    fi

    # Check if the file you are adding exists
    if [ -e "$FULLPATH" ] ; then
        sudo echo "$FULLPATH" >> $LISTFILE
        echo "Added \"$FULLPATH\" to the list file."
    else
        echo "ERROR: Target \"$FULLPATH\" does not exist or is not a file."
        exit 1
    fi

elif [ $cmd == "comment" ]
then
    # Check number of parameters
    if [ $# \< 1 ]; then
        echo "Wrong number of parameters!"
        help
        exit 1
    fi

    sudo echo "# $*" >> $LISTFILE
    echo "Added comment to list file."


elif [ $cmd == "remove" ]
then
    # Check number of parameters
    if [ $# \< 1 ]; then
        echo "Wrong number of parameters."
        help
        exit 1
    fi

    # Get the full path to the file
    DIRNAME=`dirname "$1"`
    FULLPATH=$(cd "$DIRNAME" && echo `pwd`/`basename "$1"`)

    # Check if the file already exists in the list file
    if ( ! grep -x "$FULLPATH" <$LISTFILE >/dev/null 2>&1 ) ; then
        echo "ERROR: Target \"$FULLPATH\" is not in the list file."
        exit 1
    fi

    # Clear tmporary file
    TMPFILE=/tmp/modified.list.tmp
    touch $TMPFILE
    cat /dev/null > $TMPFILE

    # Remove the line that equals
    cat $LISTFILE | while read line
    do
        if [ "$line" != "$FULLPATH" ] ; then
            echo "$line" >> $TMPFILE
        fi
    done
    sudo rm -f $LISTFILE
    sudo mv $TMPFILE $LISTFILE

    echo "\"$FULLPATH\" was removed from the list file."
    echo "It may still exist in the backup directory \"$BACKUPDIR\"."



elif [ $cmd == "backup" ]
then
    # Check if the backup directory exists.
    if [ ! -d $BACKUPDIR ] ; then
        echo "Creating backup directory \"$BACKUPDIR\"."
        mkdir $BACKUPDIR
    fi
    
    # Check if the list file exists
    if [ ! -s $LISTFILE ] ; then
        echo "ERROR: List file \"$LISTFILE\" does not exist or is empty."
        exit 1
    fi

    cat $LISTFILE | grep "^[^\ #]" | while read TARGET
    do
        # Check if target exists
        if [ -e "$TARGET" ]
        then
            echo "Copying $TARGET"
            cp --parents -f -r "$TARGET" "$BACKUPDIR"
        else
            echo "WARNING: Target \"$TARGET\" does not exist. Ignoring."
            continue
        fi
    done
    
    echo "Files can be found in \"$BACKUPDIR\"".


elif [ $cmd == "test" ]
then
    echo -n "Checking..."
    cat $LISTFILE | grep "^[^\ #]" | while read TARGET
    do
        # Check if target exists
        if [ ! -e "$TARGET" ]
        then
            echo "WARNING: Target \"$TARGET\" does not exist."
        fi
    done
    echo "OK"

else
    help
fi

