#!/bin/sh

#set -x

version="0.2"

all=0
verbose=0
rebuild=1

user=""
start_month=""
start_year=""

cookies=".cookies"

ljsession=""

url="http://www.livejournal.com/export_do.bml"
static_options="what=journal&format=xml&header=on&encid=2&notranslation=on&field_itemid=on&field_eventtime=on&field_logtime=on&field_subject=on&field_event=on&field_security=on&field_allowmask=on&field_currents=on"

get_cookies() {
	if [ -d $HOME/.mozilla -a $(find $HOME/.mozilla -name cookies.txt | wc -l) = 1 ] ; then
		grep ^.livejournal $(find $HOME/.mozilla -name cookies.txt) >$cookies
	elif [ ! -f "$cookies" ] ; then
		echo Unable to find cookies file
		return 1
	fi

	if grep -q ljsession $cookies ; then
		ljsession=`grep ljsession $cookies | head -n 1 | awk '{print $6"="$7}'`
		return 0
	else
		echo Unable to find ljsession in cookies file
		return 1
	fi
}

get_user() {
	user=`cat $cookies 2>/dev/null | grep ljsession | head -n 1 | cut -f 2 -d :`
	if [ -z "$user" ] ; then return 1 ; else return 0 ; fi
	if [ $verbose = 1 ] ; then
		echo User: $user >&2
	fi
}

get_start() {
	if [ $verbose = 1 ] ; then
		echo Getting start date >&2
	fi
	#start_date=`wget -q "http://www.livejournal.com/userinfo.bml?user=$user&mode=full" -O - | grep "Date created" | cut -f 7 -d "<" | cut -f 2 -d ">"`
	start_date=`curl -s "http://www.livejournal.com/userinfo.bml?user=$user&mode=full" | grep "Date created" | cut -f 7 -d "<" | cut -f 2 -d ">"`
	start_month=`echo $start_date | cut -f 2 -d -`
	start_year=`echo $start_date | cut -f 1 -d -`
	if [ $verbose = 1 ] ; then
		echo Start date is $start_month/$start_year
	fi
}

ljar() {
	year=$1
	month=$2

	if [ $verbose = 1 ] ; then
		echo Getting $user: $month/$year >&2
	fi
	#wget --load-cookies=$cookies -q "$url?authas=$user&year=$year&month=$month&$static_options" -O months/$year-$month
	curl -s -b $ljsession -d $static_options -d month=$month -d year=$year $url?authas=$user >months/$year-$month
	if [ ! -s months/$year-$month ] ; then
		echo Getting $month/$year failed >&2
	fi
}

rebuild_journal() {
	echo "<?xml version=\"1.0\" encoding='utf-8'?>" >journal
	echo "<livejournal>" >>journal

	for i in `ls months/*` ; do
		if [ $verbose = 1 ] ; then
			echo Adding $i to journal >&2
		fi
		grep -Ev "^<\?xml" $i | grep -Ev "^<\/?livejournal>" >>journal
	done

	echo "</livejournal>" >>journal
}

usage() {
	echo "$1 archives your LiveJournal locally"
	echo "Usage: $1 [-ahnVv] [months]"
	echo " -a: Fetch all months"
	echo " -h: Print this help"
	echo " -n: Don't rebuild journal"
	echo " -r: Only rebuild journal"
	echo " -V: version"
	echo " -v: verbose"
	echo
	echo "Months must be in the format MM/YYYY"
	echo "With no arguments, fetches for the current month"
}

while getopts ahnrvV opt ; do
	case $opt in
		a) all=1 ;;
		[h?]) usage $0 ; exit 0 ;;
		n) rebuild=0 ;;
		r) rebuild=2 ;;
		V) echo $0 Version $version ; exit 0 ;;
		v) verbose=1 ;;
	esac
done
shift $(($OPTIND - 1))

if ! get_cookies ; then exit ; fi
if ! get_user ; then exit ; fi

if [ ! -d months ] ; then mkdir months ; fi

if [ "$rebuild" = 2 ] ; then
	rebuild_journal
	exit 0
fi

if [ "$all" = 1 ] ; then
	get_start
	cur_year=`date +%Y`
	cur_month=`date +%m`
	for i in `seq $start_year $cur_year` ; do
		if [ $i == $start_year ] ; then
			for j in `seq -f %02g $start_month 12` ; do
				ljar $i $j
			done
		elif [ $i == $cur_year ] ; then
			for j in `seq -f %02g 1 $cur_month` ; do
				ljar $i $j
			done
		else
			for j in `seq -f %02g 1 12` ; do
				ljar $i $j
			done
		fi
	done

	if [ "$rebuild" = 1 ] ; then
		rebuild_journal
	fi

	exit 0
fi

if [ $# = 0 ] ; then
	year=`date +%Y`
	month=`date +%m`
	echo Getting $month/$year >&2
	ljar $year $month

	if [ "$rebuild" = 1 ] ; then
		rebuild_journal
	fi
fi

while [ -n "$1" ] ; do
	month=`echo $1 | cut -f 1 -d /`
	year=`echo $1 | cut -f 2 -d /`
	if [ -z "$month" -o -z "$year" ] ; then
		echo Invalid date: $1 >&2
	else
		ljar $year $month
	fi
	shift 1

	if [ "$rebuild" = 1 ] ; then
		rebuild_journal
	fi
done
