#!/bin/bash
CWD=$(pwd)
BASE_DIR=$(cd $(dirname $0);pwd)

### CONFIG ###
SQLITE_BIN=/usr/bin/sqlite3.exe
### CONFIG END ###

function usage() {
  cat <<__EOF__
firefox profile アーカイブスクリプト

Usage: $0 <profile-directory> <output-name>

ex) $0 ../profiles/profile1 profile1_20060420

__EOF__
}
if [ $# -lt 2 ]; then
  usage
  exit
fi

SECURE=0
if [ "$1" = "--secure" ]; then
  SECURE=1
  shift
fi
TARGET_DIR=$(cd $1; pwd)
ARCHIVE_NAME="${2}.tar"
PROFILE_DIR=${TARGET_DIR%/*}
PROFILE_NAME=${TARGET_DIR##*/}

echo --- SUMMARY ---
echo ARCHIVE FROM: $TARGET_DIR
echo PROFILE NAME: $PROFILE_NAME
echo ARCHIVE TO: $CWD/${ARCHIVE_NAME}.gz
echo SECURE: $SECURE
echo ---

pushd $BASE_DIR >/dev/null
echo "Cleaning cache files..."
rm -rf $TARGET_DIR/Cache/*
rm -rf $TARGET_DIR/cache2.Trash*
rm -rf $TARGET_DIR/cache2/*
rm -f $TARGET_DIR/jumpListCache/*
rm -f $TARGET_DIR/thumbnails/*
rm -f $TARGET_DIR/minidumps/*

rm -rf $TARGET_DIR/chaika/chaika-logs/*
echo "Cleaning cache file done."
echo

echo "Reindex and vacuuming sqlite db..."
FILES=`find $TARGET_DIR -type f -name *.sqlite`
for F in $FILES
do
  echo $F
  BEFORE_BYTES=`stat -c %s $F`
  sqlite3 $F reindex
  sqlite3 $F vacuum
  AFTER_BYTES=`stat -c %s $F`
  echo $'\t'"$BEFORE_BYTES -> $AFTER_BYTES"
done
#find $TARGET_DIR -type f -name *.sqlite | xargs sqlite3 reindex
echo "Reindex and vacuuming done."
echo

echo "Archive to $CWD/${ARCHIVE_NAME}"
pushd "$PROFILE_DIR" >/dev/null
tar -cpvf "$CWD/${ARCHIVE_NAME}" $PROFILE_NAME
popd >/dev/null
echo "Archive done."
echo

pushd $CWD >/dev/null
if [ $SECURE -eq 1 ]; then
  echo "Removing crypted passwords"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/key3.db"
  echo "Removing logins"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/logins.json"
  echo "Removing Cookies"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/cookies.sqlite"
  echo "Removing Form histories"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/formhistory.sqlite"
  echo "Removing crypted passwords"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/signons.sqlite"
  echo "Removing session storage"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/webappsstore.sqlite"
  echo "Removing bookmark backups"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/bookmarkbackups"
  echo "Removing paths"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/compatibility.ini"
  echo "Removing local storages"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/storage"
  echo "Removing sessionstore"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/sessionstore.js"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/sessionstore.bak"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/sessionstore.bak-*"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/sessionstore-backups"
  echo "Removing startup cache"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/startupCache"
  echo "Removing offline cache"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/OfflineCache"
  echo "Removing marionette log"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/marionette.log"
  echo "Removing cert override"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/cert_override.txt"
  echo "Removing healthreport"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/healthreport.sqlite"
  echo "Removing content prefs (サイトごとの縮小率など)"
  tar -vf ${ARCHIVE_NAME} --delete "$PROFILE_NAME/content-prefs.sqlite"
fi

echo "Gzip"
gzip ${ARCHIVE_NAME}
popd >/dev/null
