#!/bin/bash
################################################################################
# file:		export_ff_cookies.sh
# created:	29-07-2010
# modified:	2010 Jul 29
#
# exports cookies from Firefox's cookies.sqlite to cookies.txt
#
# schema:
#   moz_cookies (id INTEGER PRIMARY KEY, name TEXT, value TEXT, host TEXT,
#                path TEXT,expiry INTEGER, lastAccessed INTEGER,
#                isSecure INTEGER, isHttpOnly INTEGER)
################################################################################
[ ${BASH_VERSINFO[0]} -ne 3 ] && {
  echo -e "error: bash version != 3, this script might not work properly!" 1>&2
  echo    "       you can bypass this check by commenting out lines $[${LINENO}-2]-$[${LINENO}+2]." 1>&2
  exit 1
}
################################################################################
function usage() {
  cat <<- EOF
	usage: ${0} /path/to/cookies.sqlite
EOF
  return ${?}
} # usage()
################################################################################
[ -z "${1}" -o ${#} -ne 1 ] && {
  usage
  exit 0
}
COOKIES_COUNT=0
RET=0
COOKIES_FILE="${1}"
[ ! -f "${COOKIES_FILE}" ] && {
  echo "error: cookies file \`${COOKIES_FILE}' not found!"
  exit 1
}
sqlite3 -separator ' ' "${COOKIES_FILE}" "SELECT host,path,isSecure,expiry,name,value from moz_cookies" | while read -a COOKIE
do
  HOST="${COOKIE[0]}"
  [ "${HOST:0:1}" = "." ] && FLAG="TRUE" || FLAG="FALSE"
  DOMAIN_PATH="${COOKIE[1]}"
  (( ${COOKIE[2]} )) && SECURE="TRUE" || SECURE="FALSE"
  EXPIRY="${COOKIE[3]}"
  NAME="${COOKIE[4]}"
  VALUE="${COOKIE[5]}"
  # so we don't print the header if there's an sql error or something...
  ((!COOKIES_COUNT++)) && {
    echo "# HTTP Cookie File"
    echo "# Generated by ${0##*/}"
    echo "# Edit at your own risk"
  }
  echo -e "${HOST}\t${FLAG}\t${DOMAIN_PATH}\t${SECURE}\t${EXPIRY}\t${NAME}\t${VALUE}"
done
RET=${PIPESTATUS[0]}
exit ${RET}


