#!/bin/bash
# Futanarihentai script (POC)									0.2.15.6

# /* Import options *\ #

source ~/.mangatux/options
source /usr/share/mangatux/langs/$MTXLANG
source /usr/bin/mangatux-pdf

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

wget -O /tmp/.page0 $1 -o /tmp/.log
FN=`cat /tmp/.page0 | egrep  "var manga_name" | cut -d "'" -f 2`
DIRNAME="$(echo $FN)"
FILESN=`cat /tmp/.page0 | grep "Pages:" | cut -d ">" -f2 | cut -d "<" -f1`
echo "$FTH4"
if ls $TMPDIR$CDIR | grep "$DIRNAME" > /dev/null
then
	echo $FTH1
		read -p "" DIRNAME
		if ls $TMPDIR$CDIR | grep "$DIRNAME" > /dev/null
		then
			echo $FTH2
			exit
		fi
else
	cd $TMPDIR$CDIR
	echo "$FTH3"
	mkdir -p "$DIRNAME"
	cd "$DIRNAME"
fi


if cat /tmp/.page0 | egrep \'001.jpg > /dev/null
then
	let FG=1
	let C=2
	let CX=1
else
	let FG=0
	let C=3
	let CX=2
fi
FILX=`cat /tmp/.page0 | egrep \'001`
FILE=`echo $FILX | tr -s \' % `
NAMAE=`cat /tmp/.page0 | egrep  "var Pic_url" | cut -d "'" -f 2 `
for i in `seq 1 9`;
do
	if [ $FG == 1 ]
	then
		CXU=`echo $FILE | cut -d "%" -f $C`
		if [ $CXU != 001.jpg ]
		then
			CTX=`echo $CXU | cut -c3-20`
		else
			CTX=$CXU
		fi
		wget http://www.futahentai.com/`echo $NAMAE``echo $CTX`  -o /tmp/.logsesion
		let C=$C+$CX
		echo $DOWN
	else
		CXT=`echo $FILE | cut -d "%" -f $C `
		wget http://www.futahentai.com/`echo $NAMAE`00$i%`echo $CXT`  -o /tmp/.logsesion
		let C=$C+$CX
		echo $DOWN
	fi
done
for i in `seq 10 $FILESN`;
do
	if [ $FG == 1 ]
	then
		CXU=`echo $FILE | cut -d "%" -f $C`
		if [ $CXU != 001.jpg ]
		then
			CTX=`echo $CXU | cut -c3-20`
		else
			CTX=$CXU
		fi

		wget http://www.futahentai.com/`echo $NAMAE``echo $CTX`  -o /tmp/.logsesion
		let C=$C+$CX
		echo $DOWN
	else
		CXT=`echo $FILE | cut -d "%" -f $C `
		wget http://www.futahentai.com/`echo $NAMAE`0$i%`echo $CXT`  -o /tmp/.logsesion
		let C=$C+$CX
		echo $DOWN
	fi
done

if [ $TMPDIR = 1 ]; then
	PDF
fi
