#!/bin/bash
## Pequeño script para descargar desde Megaupload y Rapidshare
## by KaR]V[aN GPLv3, http://karman.homelinux.net

# -- Configuracion -- 
MU_USER=""		# Megaupload User
MU_PASS=""		# Megaupload Password
RA_USER=""	# Rapidshare User
RA_PASS=""	# Rapidshare Password
USE_PROXY="no"		# Use proxy? yes or no
PROXY_ADDRESS="http://127.0.0.1:808"	# Proxy Address
PROXY_AUTH="no"	# Use auth with proxy? yes or no
PROXY_USER=""	# Proxy User
PROXY_PASS=""	# Proxy Password

# Do not edit below

# How many args?
if [ $# != 1 ]; then
	echo "Usage: $0 [links file]"
	exit 2
fi

# tmp dir for cookies
if [ ! -d "tmp" ]; then
	mkdir -p tmp
fi

# Do we have wget?
if [ $(whereis wget | sed 's/\s/\n/g' | grep bin | grep wget | wc -l) -lt 1 ]; then
	echo "wget not found in your system"
	exit 1
fi

# Its Megauopload (MU) or Rapidshare (RA)?
if [ $( head -n1 $1 | grep rapidshare | wc -l ) -eq 1 ]; then
	echo "Detectado Rapidshare"
	COOKIE_FILE="cookie_ra.txt"
	URL="https://ssl.rapidshare.com/cgi-bin/premiumzone.cgi"
	USER=$RA_USER
	PASS=$RA_PASS
	OPTIONS="--no-check-certificate"
elif [ $( head -n1 $1 | grep megaupload | wc -l ) -eq 1 ]; then
	echo "Detectado Megaupload"
	COOKIE_FILE="cookie_mu.txt"
	URL="http://megaupload.com/"
	USER=$MU_USER
	PASS=$MU_PASS
else
	echo "Unrecognized page links"
	exit 1
fi

# Use proxy?
if [ $USE_PROXY = "yes" ]; then
	export http_proxy=$PROXY_ADDRESS
	if [ $PROXY_AUTH = "yes" ]; then
		OPTIONS="$OPTIONS --proxy-user $PROXY_USER --proxy-passwd $PROXY_PASS"
	fi
fi

# Obtain the cookie
echo "Downloading the Cookie..."
OPTIONS="$OPTIONS --save-cookies tmp/$COOKIE_FILE --post-data username=$USER&password=$PASS&login=1 -O- $URL"
wget $OPTIONS >/dev/null 2>&1

OPTIONS="--load-cookie tmp/$COOKIE_FILE"

if [ $PROXY_AUTH = "yes" ]; then
	OPTIONS="$OPTIONS --proxy-user $PROXY_USER --proxy-passwd $PROXY_PASS"
fi

# Start download of the links on the file
echo "Starting Download"
wget $OPTIONS -i $1 &
