#!/bin/bash
#---------------------------------------------------------------
# Description: GTK GUI for Paul Battley's iplayer-dl script
# Author: Matt Clarke, TechPad.co.uk
# Dependencies: zenity, iplayer-dl, ruby
#---------------------------------------------------------------

# Phrases
  WindowTitle="iPlayer Downloader"
  MessageEnterUrl="Enter iPlayer URL"
  MessageTypePreference="Do you want the normal version or the subtitled version?"
  MessageDownloading="Downloading programme now..."
  MessageDone="Your iPlayer download has completed!"
  MessageNotInstalled="iplayer-dl was not found.\n\nPlease install and configure iplayer-dl to continue."
  ColumnDownload="Download"
  ColumnVersion="Version"

# Check to see if iplayer-dl is installed as it's a pre-requisite
if [ $(basename `which iplayer-dl`) !="iplayer-dl" ]; 
	then zenity --title "$WindowTitle" --error --text "$MessageNotInstalled"
	exit
fi

# Ask user for iPlayer URL
iPlayerURL=$(zenity --entry --text "$MessageEnterUrl" --title "$WindowTitle" --width 500);
if [ $? = 1 ];
then exit
fi

# Ask the user to select a download directory
Directory=$(zenity --file-selection --directory --title "$WindowTitle" --width 500);
if [ $? = 1 ];
then exit
fi

# Ask the user whether they want the original version or signed version
TypePreference=$(zenity --title "$WindowTitle" --text "$MessageTypePreference" --list --radiolist --width 500 --column "Select" --column "Version" True original False signed);
if [ $? = 1 ];
then exit
fi

# Download using iplayer-dl and pipe output to zenity for progress bar
iplayer-dl $iPlayerURL -d $Directory -s --type-preference=$TypePreference 2>&1 | zenity --progress --title "$WindowTitle" --text="$MessageDownloading" --width 500 --auto-kill --auto-close
if [ $? = 1 ];
then exit
fi

# Show download success message
zenity --info --title "$WindowTitle" --text "$MessageDone"
exit
