#!/bin/bash
# @author Ed Cradock 
# 
# Use wget with cookie based websites, primarily written for use with
# rapidshare. But most cookie websites should work. Also download resolution
# support. If supplied with file comment out any files downloaded.
#
BROWSER_DIR=$HOME/.mozilla/firefox
COOKIE_FILE=$HOME/.cookies



# If the cookie file doesn't exist; create it
if [ ! -e  "$COOKIE_FILE" ]
then
   DEFAULT_PROFILE=0;
   IN_BLOCK=0
   
   while read line
   do
      if [ "$line" == "Name=default" ]; then
         IN_BLOCK=1
      fi
   
      if [ $IN_BLOCK -eq 1 ] && [ "${line:0:4}" == "Path" ]; then
         DEFAULT_PROFILE=${line:5};
         break;
      fi
   done < "$BROWSER_DIR/profiles.ini";

   if [ -e "$BROWSER_DIR/$DEFAULT_PROFILE/cookies.txt" ]; then
      echo "Setting up symbolic link for .cookies file"
      ln -s "$BROWSER_DIR/$DEFAULT_PROFILE/cookies.txt" "$COOKIE_FILE"
   fi
else
   wget -c --load-cookies=$COOKIE_FILE $@
fi
