#!/bin/bash
# BashPodder (BP4iP)
# Copyright 2005 Justin Gerace

# This uses a small amount of BashPodder code, copyright 2004 Linc
# http://linc.homeunix.org:8080/scripts/bashpodder/

# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License (see "LICENSE" for details)

# Set stuff
podcount=$'0'
poddir=$HOME'/BashPodcasts'

if test ! -d $poddir
then
	mkdir $poddir
fi

# Create the Main Menu
function mainmenu()
{
Xdialog --title "BashPodder" \
        --cancel-label "Esci" \
	--item-help \
	--icon BPGUI.xpm \
	--menu "" 800x600 0 \
	"Controlla i Nuovi Podcasts" "" "Controlla i Podcasts e scarica i nuovi" \
	"Ascolta i Podcasts" "" "Ascolta un Podcast che hai scaricato" \
	"Aggiungi un Podcast" "" "Usa questa funzione per aggiungere un nuovo Podcast" \
	"Modifica i Podcasts" "" "Usa questa funzione per modificare i Podcasts esistenti" \
	"Cancella un Podcast" "" "Usa questa funzione per cancellare un Podcast esistente" 2> /tmp/bp4ip.tmp.$$
choice=`cat /tmp/bp4ip.tmp.$$`
rm -f /tmp/bp4ip.tmp.$$

case "$choice" in
"Controlla i Nuovi Podcasts")
	podscan
	mainmenu
;;
"Ascolta i Podcasts")
	play
	mainmenu
;;
"Aggiungi un Podcast")
	addpodcast
	mainmenu
;;
"Modifica i Podcasts")
	editpodcasts
	mainmenu
;;
"Cancella un Podcast")
	removepodcast
	mainmenu
;;
esac
exit 0
}

# Scan for Podcasts
function podscan()
{
if `Xdialog --title "BashPodder" \
	    --ok-label "Continua" \
	    --cancel-label "Menu Principale" \
	    --yesno "Pronto per controllare i Podcasts" 6 45`
then
	rm -f temp.log
	while read podcast
	do		
		podcastaddress=`expr index "$podcast" @`
		podcastURL=${podcast:$podcastaddress}
		datadir=${podcast:0:$podcastaddress-1}
		podcastdir=$poddir'/'$datadir
		wget -q $podcastURL -O /tmp/bpgui.tmp.$$
		title=$(cat /tmp/bpgui.tmp.$$ | tr '\r' '\n' | tr \' \" | sed -n 's/.*<title>\([^">]*\)<\/title>.*/\1/p' | head -n1)
		Xdialog --title "BashPodder" --no-buttons --interval 2 --infobox "Sto controllando:\n$title" 6 45 &
		file=$(cat /tmp/bpgui.tmp.$$ | tr '\r' '\n' | tr \' \" | sed -n 's/.*url="\([^"]*\)".*/\1/p')
	#	rm /tmp/bpgui.tmp.$$
		for url in $file
		do
			echo $url >> temp.log
			if ! grep "$url" podcast.log > /dev/null
			then
				if test ! -d $podcastdir
				then
					Xdialog --title "Avviso" --no-buttons --interval 10 --infobox "Sto creando $title Podcast\ndirectory..." 6 45
					mkdir $podcastdir
				fi
				# Get download size
				progress="0"
				i="1"
				newurl=$url
				while [ $i -ne 0 ]
				do
					i=`expr index "$newurl" \/`
					newurl=${newurl:$i}
				done
				if test ! -e $podcastdir'/'$newurl
				then
				{
					wget -v -P $podcastdir "$url" 2> .download.status &
					echo 0
					dlsize=$(cat /tmp/bpgui.tmp.$$ | tr '\r' '\n' | tr \' \" | sed -n 's/.*'$newurl'"\ length="\([^"]*\)".*/\1/p')
					while [ "$progress" != "$dlsize" ]
					do
						progress="`ls -l "$podcastdir/$newurl" |sed -e \"s/\ \+/,/g\"|cut -d\, -f5`"
						echo $[progress*100/dlsize]
						sleep 1s
					done
					echo 100
				}  | Xdialog --title "Sto scaricando" --gauge "Sto scaricando\n$url" 7 60 0
				rm .download.status

				Xdialog --title "Avviso" --no-buttons --interval 10 --infobox "$url\nRicevuti" 7 60
				url=$newurl
				let "podcount += 1"
				fi
			fi
			ls -1 --time=ctime $podcastdir | cat > $podcastdir/$datadir.m3u
			grep -v $datadir $podcastdir/$datadir.m3u | cat > $podcastdir/$datadir.m3u
		done
	done < bp.conf
	if [ $podcount = '1' ]
	then
		podcast=$'Podcast'
		were=$'scaricato'
	else
		podcast=$'Podcasts'
		were=$'scaricati'
	fi
	total=$podcount
	if [ $podcount = '0' ]
	then
		total="Zero"
	fi
	Xdialog --title "Avviso" \
		--ok-label "Menu Principale" \
		--msgbox "$total $podcast $were" 6 45
		cat podcast.log >> temp.log
		sort temp.log | uniq > podcast.log
		rm temp.log
		return 0
fi
return 0
}

# Add a podcast
function addpodcast()
{

Xdialog --title "BashPodder" \
	--ok-label "Successivo" \
	--no-cancel \
	--inputbox "Aggiungere URL del Feed del Podcast" 8 45 2> /tmp/bp4ip.tmp.$$
feedurl=`cat /tmp/bp4ip.tmp.$$`
rm -f /tmp/bp4ip.tmp.$$

ldir=$(wget -q $feedurl -O - | tr '\r' '\n' | tr \' \" | sed -n 's/.*<title>\([^">]*\)<\/title>.*/\1/p' | head -n1)
i="1"
while [ $i -ne 0 ]
do
	i=`expr index "$ldir" " "`
	if [ $i -ne 0 ]
	then
		word=${ldir:0:$i-1}
		ldir=$word${ldir:$i}
	fi
done

Xdialog --title "BashPodder" \
	--ok-label "Successivo" \
	--no-cancel \
	--inputbox "Quale è il nome del Podcast ?\n(sarà il nome della directory in verrà scaricato)" 8 45 "$ldir" 2> /tmp/bp4ip.tmp.$$
ldir=`cat /tmp/bp4ip.tmp.$$`
rm -f /tmp/bp4ip.tmp.$$
if `Xdialog --title "BashPodder" \
	--ok-label "Sì" \
	--cancel-label "No" \
	--yesno "Questi dati sono corretti ?\n\nDirectory Locale: '$ldir'\n$feedurl" 9 69`
then
	echo $ldir'@'$feedurl >> bp.conf
	title=$(wget -q $feedurl -O - | tr '\r' '\n' | tr \' \" | sed -n 's/.*<title>\([^">]*\)<\/title>.*/\1/p' | head -n1)
	Xdialog --title "Avviso" \
		--msgbox "'$title'\naggiunto con successo" 7 50
else
	Xdialog --title "Avviso" \
		--ok-label "Menu Principale" \
		--msgbox "Nessun Podcast aggiunto" 6 45
fi
return 0
}

# Edit Podcasts
function editpodcasts()
{
echo 'Xdialog --title "BashPodder" --cancel-label "Menu Principale" --item-help --icon BPGUI.xpm --menu "" 800x600 0 \' > .pselect.mnu
podcastnumber=$'0'
podcasttotal=$'0'
while read podcast
do
	let "podcasttotal += 1"
done < bp.conf
while read podcast
do
	let "podcastnumber += 1"
	podcastaddress=`expr index "$podcast" @`
	feedurl=${podcast:$podcastaddress}
	ldir=${podcast:0:$podcastaddress-1}
	if [ $podcastnumber = $podcasttotal ]
	then
		echo '"'$ldir'" "" "Modifica '$ldir'" 2> /tmp/bp4ip.tmp.$$' >> .pselect.mnu
	else
		echo '"'$ldir'" "" "Modifica '$ldir'" \' >> .pselect.mnu
	fi
done < bp.conf
source .pselect.mnu
pselection=`cat /tmp/bp4ip.tmp.$$`
rm /tmp/bp4ip.tmp.$$
if [ "$pselection" = "" ]
then
	return 0
fi
pselection=`grep "$pselection" bp.conf`
podcastaddress=`expr index "$pselection" @`
podcastURL=${pselection:$podcastaddress}
ldir=${pselection:0:$podcastaddress-1}

Xdialog --title "BashPodder" \
	--no-cancel \
	--2inputsbox "Cosa vuoi cambiare ?" 15 60 \
	"Directory Locale:" "$ldir" \
	"URL:" "$podcastURL" 2> /tmp/bp4ip.tmp.$$
newpodcast=`cat /tmp/bp4ip.tmp.$$`
rm /tmp/bp4ip.tmp.$$
oldpodcast=$ldir'/'$podcastURL
if [ "$newpodcast" = "$oldpodcast" ]
then
	Xdialog --title "Avviso" --msgbox "Nessuna modifica apportata" 6 45
	editpodcasts
	return 0
fi
slash=`expr index "$newpodcast" /`
newldir=${newpodcast:0:$slash-1}
newurl=${newpodcast:$slash}
grep -v "$ldir" bp.conf | cat > bp.conf
echo $newldir'@'$newpodcast >> bp.conf

Xdialog --title "Avviso" \
	--msgbox "'$newldir' Modificato" 6 45
editpodcasts
return 0
}

# Remove a Podcast
function removepodcast()
{
echo 'Xdialog --title "BashPodder" --cancel-label "Menu Principale" --ok-label "Cancella" --item-help --icon BPGUI.xpm --menu "" 800x600 0 \' > .pselect.mnu
podcastnumber=$'0'
podcasttotal=$'0'
while read podcast
do
	let "podcasttotal += 1"
done < bp.conf
while read podcast
do
	let "podcastnumber += 1"
	podcastaddress=`expr index "$podcast" @`
	ldir=${podcast:0:$podcastaddress-1}
	if [ $podcastnumber = $podcasttotal ]
	then
		echo '"'$ldir'" "" "Cancella '$ldir'" 2> /tmp/bp4ip.tmp.$$' >> .pselect.mnu
	else
		echo '"'$ldir'" "" "Cancella '$ldir'" \' >> .pselect.mnu
	fi
done < bp.conf
source .pselect.mnu
pselection=`cat /tmp/bp4ip.tmp.$$`
rm /tmp/bp4ip.tmp.$$
if [ "$pselection" = "" ]
then
	return 0
fi
if `Xdialog --title "BashPodder" --ok-label "Cancella" --cancel-label "Annulla" --yesno "Vuoi davvero cancellare $pselection ?" 6 45`
then
	grep -v "$pselection" bp.conf > bp.conf.new
	rm bp.conf
	mv bp.conf.new bp.conf
	Xdialog --title "Avviso" \
		--ok-label "Menu Principale" \
		--msgbox "$pselection Cancellato" 6 45
	return 0
fi
removepodcast
return 0
}

# Play a podcast
function play()
{
echo 'Xdialog --title "BashPodder" --cancel-label "Menu Principale" --item-help --icon BPGUI.xpm --menu "" 800x600 0 \' > .pselect.mnu
podcastnumber=$'0'
podcasttotal=$'0'
while read podcast
do
	let "podcasttotal += 1"
done < bp.conf
while read podcast
do
	let "podcastnumber += 1"
	podcastaddress=`expr index "$podcast" @`
	feedurl=${podcast:$podcastaddress}
	ldir=${podcast:0:$podcastaddress-1}
	if [ $podcastnumber = $podcasttotal ]
	then
		echo '"'$ldir'" "" "Ascolta i Podcasts di: '$ldir'" 2> /tmp/bp4ip.tmp.$$' >> .pselect.mnu
	else
		echo '"'$ldir'" "" "Ascolta i Podcasts di: '$ldir'" \' >> .pselect.mnu
	fi
done < bp.conf
source .pselect.mnu
pselection=`cat /tmp/bp4ip.tmp.$$`
rm /tmp/bp4ip.tmp.$$
if [ "$pselection" = "" ]
then
	return 0
fi
xmms $poddir/$pselection/$pselection.m3u &
return 0
}

# Convert old bp.conf into new bp.conf
function convert()
{
rm -f bp.conf.new
if ! `Xdialog --title "Avviso" \
	--ok-label "Aggiorna" \
	--cancel-label "Esci" \
	--yesno "Sembra che il file bp.conf debba essere aggiornato.\nAggiorno ?" 7 47`
then
	exit 0
fi
confcount="0"
conftotal="0"
while read oldconf
do
	let "conftotal += 1"
done < bp.conf
{
echo 0
while read oldconf
do
	let "confcount += 1"
	title=$(wget -q $oldconf -O - | tr '\r' '\n' | tr \' \" | sed -n 's/.*<title>\([^">]*\)<\/title>.*/\1/p' | head -n1)
	echo "XXX"
	echo "Sto aggiornando $title"
	echo "XXX"
	echo $[confcount*100/conftotal]
	i="1"
	while [ $i -ne 0 ]
	do
		i=`expr index "$title" " "`
		if [ $i -ne 0 ]
		then
			word=${title:0:$i-1}
			title=$word${title:$i}
		fi
	done
	echo $title'@'$oldconf >> bp.conf.new
done < bp.conf
sleep 1s
} | Xdialog --title "Aggiornamento" --gauge "Mi preparo ad aggiornare..." 7 60
Xdialog --title "Avviso" \
	--ok-label "Menu Principale" \
	--msgbox "Aggiornamento completato.\nOra puoi modificare i Podcasts" 7 45
mv bp.conf bp.conf.old
mv bp.conf.new bp.conf
#Xdialog --title "Avviso" \
#	--ok-label "Menu Principale" \
#	--msgbox "Questa feature è ancora in via di sviluppo" 6 45
return 0
}

# Display welcome message
Xdialog --title "BashPodder" \
	--no-buttons \
	--interval 2.5 \
	--infobox "Benvenuto in BashPodder" 6 45

# Check if bp.conf needs to be updated
grepconf=`grep @ bp.conf`
if [ "$grepconf" = "" ]
then
	if test -e bp.conf
	then
		convert
	fi
fi

# Launch Main Menu
mainmenu


