#!/bin/bash
# Filename: ff-password-sync.sh
# Version: 0.1
# Livense: GPLv3
#
# Description:
# bash script to assist in importing/exporting of passwords for firefox using password exporter addon
# Read more at: http://www.void.gr/kargig/blog/2009/07/02/using-firefox-password-exporter/
# Created by George Kargiotakis kargig[at]void[dot]gr
#
# Changelog
# v0.1 Original version


FILE1=$1
FILE2=$2
if [ ! -e $FILE2 ]; then
    echo "Usage: ff-password-sync.sh passfile1 passfile2"
    exit
fi

#create some temp files
TMPFILE=`mktemp /tmp/ffpassexp.XXXXXXXXXX` || exit 1
TMPFILE1=`mktemp /tmp/ffpassexp.XXXXXXXXXX` || exit 1
TMPFILE2=`mktemp /tmp/ffpassexp.XXXXXXXXXX` || exit 1
TMPFILE3=`mktemp /tmp/ffpassexp.XXXXXXXXXX` || exit 1
TMPFILE4=`mktemp /tmp/ffpassexp.XXXXXXXXXX` || exit 1
TMPFILEDIFF=`mktemp /tmp/ffpassexp.XXXXXXXXXX` || exit 1

#header of new xml file
echo "<xml>
<entries ext=\"Password Exporter\" extxmlversion=\"1.1\" type=\"saved\" encrypt=\"false\">" > $TMPFILE

#remove unecessary entries from files
head -n -2 $FILE1 | tail -n +3 | sort | uniq > $TMPFILE1
head -n -2 $FILE2 | tail -n +3 | sort | uniq > $TMPFILE2

#diff the files
diff -uN $TMPFILE1 $TMPFILE2 > $TMPFILEDIFF

#find the unique missing entries
cat $TMPFILE1 $TMPFILE2 > $TMPFILE3
sort $TMPFILE3 | uniq -u > $TMPFILE4

#main contents of new xml file
cat $TMPFILE4 >> $TMPFILE
#footer of new xml file
echo "</entries>
</xml>" >> $TMPFILE

#copy output files to home dir
cp $TMPFILE ~/ff-password-sync.xml
cp $TMPFILEDIFF ~/ff-password-sync.diff

#delete temp files
rm -f $TMPFILE $TMPFILE1 $TMPFILE2 $TMPFILE3 $TMPFILE4 $TMPFILEDIFF
