#!/bin/bash
#Mangatux MangaFox beta

# /* Import options *\ #

source ~/.mangatux/options;
source /usr/share/mangatux/lang/$MTXLANG;
source /usr/bin/mangatux-fu

##### /* Functions *\ #####

wget --user-agent="Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Ubuntu/10.10 Chromium/10.0.628.0 Chrome/10.0.628.0 Safari/534.16" -O /tmp/.page0 $1 -o /tmp/.log
FNF=`cat /tmp/.page0 | grep "<div id=\"series\">" | cut -d ">" -f3 | cut -d "<" -f1 `
DIRNAME="$(echo $FNF)"
BDIR=`echo $1 | cut -d "/" -f5 | tr -s '[_]' '[ ]' `

#function DDRR(){
if ls $TMPDIR$CDIR | grep "$BDIR" > /dev/null
then
	echo $MNF1
	if ls $TMPDIR$CDIR/"$BDIR" | grep "$DIRNAME" > /dev/null
	then
		read -p "" DIRNAME
		if ls $TMPDIR$CDIR/"$BDIR" | grep "$DIRNAME" > /dev/null
		then
			echo $MCA3
			exit
		fi
	else
		cd $TMPDIR$CDIR/"$BDIR"
		echo $MCA4
		mkdir -p "$DIRNAME"
		cd "$DIRNAME"
	fi
else
	cd "$CDIR"
	mkdir -p "$BDIR"
	cd "$BDIR"
	echo $MCA5
	if ls "$CDIR/$BDIR" | grep "$DIRNAME" > /dev/null
	then
		read -p "" DIRNAME
		if ls $TMPDIR$CDIR/"$BDIR" | grep "$DIRNAME" > /dev/null
		then
			echo $MCA2
			exit
		fi
	else
		echo $MCA4
		mkdir -p "$DIRNAME"
		cd "$DIRNAME"
	fi
fi
#}

ULCT=`echo $1 | cut -d "/" -f -6`
FILESN=`lynx -nonumbers -dump $1 | grep "$ULCT/.*" | tail -1 | cut -d "/" -f 7 | cut -d "." -f1`
if ! [ -e /tmp/mfcdn ];
then
	mkdir -p "/tmp/mfcdn"
fi

for i in `seq 1 $FILESN`;
do
	DWNLINK=`lynx -image_links -nonumbers -dump "$ULCT/$i.html" | grep "http://.*.p.s.mfcdn.net/store/.*"`
	DFILE=`echo $DWNLINK | cut -d "/" -f 9`
	EXTEN=`echo $DFILE | cut -d "." -f 2`
	wget --user-agent="Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Ubuntu/10.10 Chromium/10.0.628.0 Chrome/10.0.628.0 Safari/534.16" $DWNLINK -O /tmp/mfcdn/$DFILE -o /tmp/.logsesion
	montage -crop "0x0+0-36" -geometry +0+0 "/tmp/mfcdn/$DFILE" "$CDIR/$BDIR/$DIRNAME/page $i.$EXTEN"
		echo $DOWN.
done
rm -r /tmp/mfcdn/.*
