#!/bin/bash

# A script to find missing files in the f-spot database, and then delete them.
# At present these files crash f-spot. It's frustrating as all hell.

echo "Welcome to the f-spot database cleaner. All the usual disclaimers apply, as you might imagine."
echo "What would you like to do: " 
echo "  1) Run in demo-mode " 
echo "  2) Clean up your f-spot database" 
echo "  3) Quit" 
read -p "Your choice: " choice

case $choice in 
    1) demomode="true";;
    2) demomode="false";;
    3) exit 0;;
esac

# With that beginning stuff out of the way, let us do some functions
# First, a function to gather the database contents and to print out the ones that are orphans

function findAndPrintOrphans {
    if [ -f $HOME/.gnome2/f-spot/photos.db ]
    then
        # Select the filenames, and put them in a variable.
        filenames=$(sqlite3 $HOME/.gnome2/f-spot/photos.db "SELECT URI FROM PHOTOS")
        
        # Chomp off the first instance of file://, and replace the rest with newlines.
        filenames=$(echo $filenames | sed 's/file:\/\///' | sed 's/file:\/\//\n/g'  )
        
        if [ $demomode == "true" ]
        then            
            while read -r line
            do
                # Decode the filename
                decodedLine=$(echo -e "${line//\%/\\x}")
                if [ ! -f  "$decodedLine" ] 
                then
                    # If the file doesn't exist, we output the filename, if in demomode, or we fix it if we are not in demomode.
                    echo  "Errant record found: $decodedLine"
                fi
            done <<< "$filenames"
        else
            # We backup the database, and make the correction
            cp $HOME/.gnome2/f-spot/photos.db $HOME/.gnome2/f-spot/photos.db.`date -I`.bak
            if [ $? -eq 0 ]
            then
                #The backup worked, tell the user.
                echo "Your database has been backed up to ~/.gnome2/f-spot/photos.db.`date -I`.bak"
            else
                echo "Error backing up your database."
                exit 3
            fi
            
            #echo $filenames
            while read -r line2
            do
                # Decode the filename
                decodedLine2=$(echo -e "${line2//\%/\\x}")
                
                if [ ! -f "$decodedLine2" ]
                then
                    # Do some sql here.
                    foo="file://${line2}"
                    echo $foo
                    echo -n "Deleting URI $line2 from the database..."
                    sqlite3 $HOME/.gnome2/f-spot/photos.db "DELETE FROM PHOTOS WHERE uri = '$foo'"
                    echo "done."
                fi
            done <<< "$filenames"
        fi

    else
        echo "Error: Could not find database. Damn."
        exit 1
    fi
}

if [ "$demomode" == "true" ]
then
    echo "Great. Proceeding in demomode."
    
    findAndPrintOrphans

    echo "Demomode successfully finished. Exiting."
    exit 0;
elif [ "$demomode" == "false" ]
then
    echo "Great. Cleaning up your database."
    
    findAndPrintOrphans    

    echo "Database cleaned successfully."
    exit 0;
else
    echo "Something strange happened. See the script for details. Exiting."
    exit 1;
fi
