Firefox profile folder cleanup script

Here’s a simple script which can be useful if you want to version control the Firefox profile folder, or just to keep it slim. Please see the embedded documentation for more information.

cleanup.sh

#!/bin/sh
#
# NAME
#    cleanup.sh - Clean up the Firefox profile folder
#
# SYNOPSIS
#    cleanup.sh [options] [directory]
#
# OPTIONS
#    -v,--verbose    Verbose output
#
# EXAMPLE
#    ./cleanup.sh -v
#
#    Cleanup in ./Profile
#
#    /path/to/cleanup.sh /path/to/Firefox/profile
#
#    Cleanup in /path/to/Firefox/profile
#
# DESCRIPTION
#
#    "Vacuums" the .sqlite files and sorts cert_override.txt and persdict.dat
#    alphabetically, to make them suitable for version control.
#
# BUGS
#    Email bugs to victor dot engmark at gmail dot com. Please include the
#    output of running this script in verbose mode (-v).
#
# COPYRIGHT AND LICENSE
#    Copyright (C) 2009 Victor Engmark
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################

# Output error message
error()
{
    if [ -z "$2" ]
    then
        error_code=$EX_UNKNOWN
    else
        error_code=$2
    fi
    echo "$1" >&2
    exit $error_code
}

usage()
{
    error "Usage: ${cmdname} [-v|--verbose] [directory]" $EX_USAGE
}

verbose_echo()
{
    if [ $verbose ]
    then
        echo "$*"
    fi
}

# Use for mandatory directory checks
# $1 is the directory path
# $2 is the (optional) error message
directory_exists()
{
    if [ ! -d $1 ]
    then
        error "No such directory '${1}'
$2" $EX_NO_SUCH_DIR
    fi
}

# Make sure an executable is available
# $1 is the path to the executable
# $2 is the (optional) error message
executable_exists()
{
    if [ ! -x $1 ]
    then
        error "No such executable '${1}'
$2" $EX_NO_SUCH_EXEC
    fi
}

ifs_original="$IFS" # Reset when done
IFS="
" # Make sure paths with spaces don't make any trouble when looping
    
PATH="/usr/bin:/bin"
cmdname=`basename $0`
directory=$(dirname $(readlink -f $0))

if [ -z "$profile_directory" ]
then
    profile_directory="${directory}/Profile"
fi
    
# Exit codes from /usr/include/sysexits.h, as recommended by
# http://www.faqs.org/docs/abs/HTML/exitcodes.html
EX_OK=0           # successful termination
EX_USAGE=64       # command line usage error
EX_DATAERR=65     # data format error
EX_NOINPUT=66     # cannot open input
EX_NOUSER=67      # addressee unknown
EX_NOHOST=68      # host name unknown
EX_UNAVAILABLE=69 # service unavailable
EX_SOFTWARE=70    # internal software error
EX_OSERR=71       # system error (e.g., can't fork)
EX_OSFILE=72      # critical OS file missing
EX_CANTCREAT=73   # can't create (user) output file
EX_IOERR=74       # input/output error
EX_TEMPFAIL=75    # temp failure; user is invited to retry
EX_PROTOCOL=76    # remote error in protocol
EX_NOPERM=77      # permission denied
EX_CONFIG=78      # configuration error
    
# Custom errors
EX_UNKNOWN=1
EX_NO_SUCH_DIR=91
EX_NO_SUCH_EXEC=92

# Process parameters
until [ $# -eq 0 ]
do
    case $1 in
        -v|--verbose)
            verbose=1
            shift
            ;;
        *)
            if [ $# -ne 1 ]
            then
                usage
            fi
            profile_directory=$1
            shift
            ;;
    esac
done

# Announce that we're running
verbose_echo "Running $cmdname at `date`."

# Make sure the directory path doesn't end with a slash
profile_directory="${profile_directory%\/}"

# Preliminary checks
executable_exists "/usr/bin/sqlite3"
directory_exists "$profile_directory"

verbose_echo "Profile directory: '${profile_directory}'"

cd $profile_directory

# SQLite cleanup
for sql_file in *.sqlite
do
    verbose_echo "Cleaning '${sql_file}'"
    echo "VACUUM;" | sqlite3 $sql_file
done

# Sorting files
for file in cert_override.txt persdict.dat
do
    verbose_echo "Sorting '${file}'"
    sort --output=$file $file
done

cd $directory

verbose_echo "Cleaning up."
IFS="$ifs_original"

verbose_echo "${cmdname} completed at `date`."
exit $EX_OK
Advertisements

One thought on “Firefox profile folder cleanup script

  1. Pingback: Neil Turner's Blog « Links from Delicious for July 23, 2011

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s