#!/bin/bash

launcher_options () {
	launcher=totem
}

launcher () {
    #Video_TS:
	search_list="$(find -L "$URL" -maxdepth 1 -type d -iname 'video_ts')"

	if [ -d "$search_list" ]; then
		eval $launcher "$search_list"&
		exit
	fi


    #Seasons:
	search_list="$(find -L "$URL" -maxdepth 1 -type d -iname 'Temporada*' -or -iname 'Season*' -or -iname 'Serie*' -or -iname 'Stagione*' -or -iname 'Seizoen*' -or -iname 'Reeks*' | sort)"

	if [[ -n "$search_list" ]]; then
		search_result="$(echo "$search_list" | sed -e "s|$URL/||" | zenity  --list --window-icon="$icon" --height=333 --width=300 --title "${URL##*/}" --text "Chose a season:" --column "Seasons")"
		if [[ $? = 1 ]]; then
			exit
		fi
		search_list="$(find -L "$URL/$search_result" -maxdepth 1 -type f -iname '*.avi' -or -iname '*.ogg' -or -iname '*.mpg' -or -iname '*.divx' -or -iname '*.mpeg' -or -iname '*.mov' -or -iname '*.mp4' -or -iname '*.mkv' -or -iname '*.wmv' -or -iname '*.asf' -or -iname '*.flv' | sort)"
		search_result="$(echo "$search_list" | sed -e "s|$URL/$search_result/||" -e 's/\....$//' | zenity  --list --window-icon="$icon" --height=333 --width=300 --title "${URL##*/}" --text "Play from:" --column "Episodes")"
		if [[ $? = 1 ]]; then
			exit
		fi
		play_from=$(( $(echo "$search_list" | wc -l) - $(echo "$search_list" | grep -n "$search_result" | cut -d: -f1 ) + 1 ))
		eval $launcher $(echo "$search_list" | tail -n $play_from | sed -e 's/^/"/' -e 's/$/"/' | tr "\n" " ")&
    #Episodes:
	else
		search_list="$(find -L "$URL" -maxdepth 1 -type f -iname '*.avi' -or -iname '*.ogg' -or -iname '*.mpg' -or -iname '*.divx' -or -iname '*.mpeg' -or -iname '*.mov' -or -iname '*.mp4' -or -iname '*.mkv' -or -iname '*.wmv' -or -iname '*.asf' -or -iname '*.flv' | sort)"
		search_result="$(echo "$search_list" | sed -e "s|$URL/||" -e 's/\....$//' | zenity  --list --window-icon="$icon" --height=333 --width=300 --title "${URL##*/}" --text "Play from:" --column "Episodes")"
		if [[ $? = 1 ]]; then
			exit
		fi
		play_from=$(( $(echo "$search_list" | wc -l) - $(echo "$search_list" | grep -n "$search_result" | cut -d: -f1 ) + 1 ))
		eval $launcher $(echo "$search_list" | tail -n $play_from | sed -e 's/^/"/' -e 's/$/"/' | tr "\n" " ")&

	fi
}
