#!/bin/bash
	
	autosave="$PWD/autosave"
	workdir="$HOME/Downloads"
	dialogrc="$PWD/.dialogrc"
	outputdir="$HOME"
	url_regex='https://readcomiconline.li/Comic/(.*?)'
	load_time=3
	save_time=6
	scroll_num=3
	scroll_delay=15000
	browser="firefox"

	function user_custom_issue {
	
		while true; do
		
			issue=$(dialog  --clear \
					--colors \
					--help-button \
					--backtitle "\Zbreadcomic_dl\Zn \Z1by tablet_seeker\Zn" \
					--title "\ZbCustom Title Selection\Zn" "$@" \
					--output-fd 1 \
					--inputbox "$(echo -e "Enter a file naming scheme for your range.\n\
					You can use any combination of letters and symbols which \
					\Zb\Z1must\Zn be followed by a combination of \Zb\Zr - \Zn \
					and a number consisting of up to four digits \Zb\Zr 9999 \Zn. \
					Spaces are ignored, but special symbols and capital letters can be used.\n\n\
					Examples: \Zb\Zr MyComic-34 \Zn \Zb\Zr My_Cool_Comic-122 \Zn\n
					Press \Zb\Zr TAB \Zn and \Zb\Zr Cancel \Zn to skip" | sed -e "s/\\t//g")" 17 52 \
					3>&1 2>&3 3>&-)
					
			case "$?" in
			
				2)
					dialog --title "\ZbDetails\Zn" --colors \
					--clear \
					--backtitle "\Zbreadcomic_dl\Zn \Z1by tablet_seeker\Zn" \
	      				--msgbox "This option aims to grant users an opportunity to \
	      				choose a custom comic title. The 1-4 digit number that must be included \
	      				marks the issue number of the first issue in your range and will be raised \
	      				by increments of +1 until the end of the range. \
	      				For example, \"My_Comic-12\" implies the user's wish to download a range starting with issue 12, but prefers \
	      				a custom title instead of the default which would most likely be \"Issue-XX\"." 15 45
					;;
				1)
					issue="default" && break
					;;
				
				0)
					[[ $issue =~ ^(.*?)-[0-9]{,4}$ ]] && break || dialog --colors \
					--title "\ZbError Input\Zn" \
					--msgbox "Your naming scheme does not match the prescribed method. Try again!" 7 28
					;;				
				esac
			
		done
	
	}

	function user_comic_selection {
	
		while true; do

				index=($(dialog --clear \
					--help-button \
					--item-help \
					--colors \
					--separate-output \
					--no-tags \
					--separator " " \
					--backtitle "\Zbreadcomic_dl\Zn \Z1by tablet_seeker\Zn" \
					--title "\Zb$(echo "${array[0]}" | cut -d "/" -f3)\Zn" "$@" \
					--output-fd 1 \
					--checklist "$(echo -e "$checklist_string" | sed -e "s/\\t//g")" 30 66 5 "${darray[@]}" 3>&1 2>&3 3>&-))

				case "$?" in
				
					2)
						dialog --title "\ZbDetails\Zn" --colors \
						--clear \
						--backtitle "\Zbreadcomic_dl\Zn \Z1by tablet_seeker\Zn" \
		      				--msgbox "Depending on the mode, this option either allows for the selection \
		      				of a range by setting two check marks, a random assortment or a single comic.\
		      				The list is always sorted in descending order, meaning it starts with the first \
		      				issue. Special issues like TPB or Full are always displayed at the very top, above \
		      				regular issues." 13 45
						;;
					1)
						exit
						;;
					
					0)
						[[ $(echo "${index[@]}" | wc -w) =~ $error_check ]] && \
						break || dialog --colors \
						--title "\ZbError Input\Zn" \
						--msgbox "$error_string" 7 29
						;;				
					esac



		done
	
	}
	
	[ -f ~/.dialogrc ] || cp "$dialogrc" ~/.dialogrc

	while true; do
	
		url=$(dialog  --title "Comic URL" \
				--inputbox "Enter the URL of your comic's main page:" 8 50 \
				3>&1 1>&2 2>&3 3>&- | tr -d ' ' \
		)
		
		[[ $url =~ $url_regex && $url != *"?id"* ]] && break || dialog --title "Error Input" \
		--msgbox "This script exclusively supports main page links from readcomiconline.li \nPlease try again!" 8 28
	done
	
	read -ra array <<< $(wget "$url" -q -O - | grep -Eo '"(.*?)\?id=[0-9]*"' | sed '1!G;h;$!d' | tr -d '"'| tr '\n' ' ')

	for x in "${!array[@]}"; do 

		clean_title[x]=$(echo "${array[x]}" | sed -e "s/\?id=[0-9]*//g" -e 's@.*/@@')
		item_help=$(echo "${array[x]}" | cut -d "/" -f3- | sed -e "s/\?id=[0-9]*//g")
		darray+=($(echo "$x ${clean_title[x]} OFF $item_help"))

	done
	
	full_comic="${item_help%/*}-"
	duplicates=($(echo "${clean_title[@]}" | fmt -1 | sort | uniq -Du))	
	
	[ -n "$duplicates" ] && { for x in "${!clean_title[@]}"; do
		echo "${duplicates[twin]}" | \
		grep -Ewqo "${clean_title[x]}" && \
		clean_title[x]="${clean_title[x]}#duplicate-$((twin+1))" && \
		((twin++)) && [ -z "${duplicates[twin]}" ] && break; \
		done; }

	[ "${#array[@]}" -gt "1" ] && dialog --colors \
		--backtitle "\Zbreadcomic_dl\Zn \Z1by tablet_seeker\Zn" \
		--title "\ZbMode Selection\Zn" "$@" \
		--yesno "Would you like to download a range of comics?" 6 30 \
		3>&1 1>&2 2>&3 3>&- || false
		
	[ "$?" -eq "1" ] && mode="multi" || mode="range"


	case "$mode" in

	  range)
	    
		user_custom_issue
		
		checklist_string="Please set a checkmark at the beginning and end of your\n\
		desired range by toggling the boxes next to the comic handle denoting each.\n
		Naming Scheme: \Zb\Z1$issue\Zn \n  
		Press \Zb\Zr SPACE \Zn to toggle an option on/off.\n\n\
		Press \Zb\Zr Page UP \Zn or \Zb\Zr Page DOWN \Zn for rapid scrolling.\n\n\
		Press \Zb\Zr Arrow UP ↑ \Zn or \Zb\Zr Arrow DOWN ↓ \Zn for slow scrolling.\n\n"

		error_check='2'
		error_string="You must select exactly two options to complete a valid range!"
		
		user_comic_selection
		
		start="${index[0]}"
		limit=$(echo $((index[1]+1)))
		issue_nr=$(echo "$issue" | sed "s|.*-||")
	    ;;

	  multi)

		checklist_string="Please set a checkmark at whichever comic you would like\n\
		to download. You can pick as many options as you like.\n
		Press \Zb\Zr SPACE \Zn to toggle an option on/off. \n\\n\
		Press \Zb\Zr Page UP \Zn or \Zb\Zr Page DOWN \Zn for rapid scrolling.\n\n\
		Press \Zb\Zr Arrow UP ↑ \Zn or \Zb\Zr Arrow DOWN ↓ \Zn for slow scrolling.\n\n"

		error_check='^[1-9]+$'
		error_string="You have not selected any valid option. Please try again!"
		
		user_comic_selection
		
		start=0
		limit=${#index[@]}
		
		for x in "${!index[@]}"; do

			choice[x]="${clean_title[${index[x]}]}"
		done
	    ;;
	esac


	for ((x=$start;x<$limit;x++)); do
		
		case "$mode" in

		range)
			[[ $issue =~ default ]] && scheme="${clean_title[x]}" || \
			{ scheme="${issue%-*}-$issue_nr"; full_comic=""; }
			num=$x
		    ;;

		multi)
			scheme="${choice[x]}"
			num="${index[x]}"
		    ;;
		
		esac

		bash "$autosave" -b "$browser" -d "$workdir/$scheme" -c "$full_comic$scheme" \
		--load-wait-time "$load_time" --save-wait-time "$save_time" --scroll-num "$scroll_num" --scroll-delay "$scroll_delay" \
		"https://readcomiconline.li${array[num]}&s=&quality=hq&s=&readType=1"

		sleep 1

		img2pdf "$workdir/$scheme"_files/*.j*g --output "$outputdir/$scheme".pdf || \
		{ echo -e "\nInput Error! \nCaptchas may have been triggered or load/save time too short"; exit; }

		rm -rf "$workdir/$scheme"_files "$workdir/$scheme"
		
		((issue_nr++))

	done
