#!/bin/bash
#
# XBMC & VDR install script
# (CC) BY-NC-SA by speefak
#
########################
### define variables ### 
########################

CODENAME=$(lsb_release -c | tr -d : | cut -f 2)
SFTLIST="xbmc xbmc-standalone vdr vdr-plugin-streamdev-server vdr-plugin-vnsiserver vdr-plugin-xineliboutput vdradmin-am vdr-plugin-undelete libvdpau1"

########################
### define functions ###
########################

# Adding PPA sources and fingerprints

function addsrc ()	# Backup and update /etc/apt/sources.list
{ 
	date				>> /etc/apt/sources.list.bck
	cat /etc/apt/sources.list 	>> /etc/apt/sources.list.bck
	echo
	echo "Backupfile created"
	echo
	echo "Adding new sources to /etc/apt/sources.list"
	echo
	echo											>> /etc/apt/sources.list
	echo											>> /etc/apt/sources.list
	echo "#PPAs for Nvidia VDPAU"   	 						>> /etc/apt/sources.list
	echo deb http://ppa.launchpad.net/nvidia-vdpau/ppa/ubuntu 	$CODENAME	main 	>> /etc/apt/sources.list
	echo deb-src http://ppa.launchpad.net/nvidia-vdpau/ppa/ubuntu 	$CODENAME	main 	>> /etc/apt/sources.list
	echo 										 	>> /etc/apt/sources.list
	echo "#stable VDR"									>> /etc/apt/sources.list
	echo deb http://ppa.launchpad.net/yavdr/stable-vdr/ubuntu	$CODENAME	main 	>> /etc/apt/sources.list
	echo deb-src http://ppa.launchpad.net/yavdr/stable-vdr/ubuntu 	$CODENAME 	main 	>> /etc/apt/sources.list
	echo											>> /etc/apt/sources.list
	echo "#stable xbmc"								 	>> /etc/apt/sources.list
	echo deb http://ppa.launchpad.net/yavdr/stable-xbmc/ubuntu 	$CODENAME 	main 	>> /etc/apt/sources.list
	echo deb-src http://ppa.launchpad.net/yavdr/stable-xbmc/ubuntu 	$CODENAME	main 	>> /etc/apt/sources.list
	echo										 	>> /etc/apt/sources.list
	echo "#stable yaVDR"								 	>> /etc/apt/sources.list
	echo deb http://ppa.launchpad.net/yavdr/stable-yavdr/ubuntu 	$CODENAME 	main 	>> /etc/apt/sources.list
	echo deb-src http://ppa.launchpad.net/yavdr/stable-yavdr/ubuntu $CODENAME 	main 	>> /etc/apt/sources.list
	echo 											>> /etc/apt/sources.list
	echo "New sources.list created"
	echo
	echo "Adding Fingerprints to System"
	echo
	apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 00D4A4E69470F55660357A7F272A2CE18103B360 # stable VDR   fingerprint
	apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 00D4A4E69470F55660357A7F272A2CE18103B360 # stable xbmc  fingerprint
	apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 00D4A4E69470F55660357A7F272A2CE18103B360 # stable yaVDR fingerprint
	apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 71609D4D2F1518FA9C5DC0FB1DABDBB4CEC06767 # Nvidia VDPAU fingerprint
} 

function instsft ()
{
	apt-get update		
	apt-get install -y $SFTLIST
}
#####################
## Starting Script ##
#####################

#########################################
##  Chekking installation dependences  ##
#########################################

	if [ `whoami` == "root" ] ; then
		echo 
	else
		echo "Are You Root ?"
		exit
	fi

	if [ "$(lsb_release -c | tr -d : | cut -f 2)" == "lucid" ] ; then
		echo "System is Ubuntu 10.04 Lucid Lynx"
		echo "XBMC & yaVDR installation can be proceeded"
	else	
		echo "WARNING, wrong Ubuntu Installation. This script is based on Ubuntu Linux 10.04 Lucid Lynx"
		echo "Proceeding can cause heavy damage to your System"  
	fi

echo -n "Continue installing ? Y/N "
proceed=
#	if [ "$CODENAME" == "lucid" ] ; then
#	proceed=y
#	

read -n 1 proceed
	if   [ "$proceed" == "y" -o "$proceed" == "Y" ] ; then 
		echo
		echo "Starting Installation ox XBMC yaVDR and VDPAU"
	
	elif [ "$proceed" == "n" -o "$proceed" == "N" ] ; then
		echo
		echo "Installation canceled"
		exit

	else
		echo
		echo
		echo "No or wrong Input - continue with No"
		exit
	fi
# else
# fi


##########################################
## Update Softwaresources and  Software ##
##########################################
echo
echo "==================================================================="
echo "===== Backup etc/apt/sources.list to etc/apt/sources.list.bck ====="
echo "===== and adding new sources to sources.list                  ====="
echo "==================================================================="
echo 	
echo -n "Adding new sources and create backup of /etc/apt/sources.list ? Y/N "
proceed=
read -t 5 -n 1 proceed
	if   [ "$proceed" == "y" -o "$proceed" == "Y" ] ; then 
		echo
		addsrc
	
	elif [ "$proceed" == "n" -o "$proceed" == "N" ] ; then
		echo
		echo			
		echo "Backupfile not created"

	else
		echo
		echo
		echo "No or wrong Input - continue with Yes"
		addsrc
	fi
echo

echo
echo "============================================"
echo "===== Installing XBMC, yaVDR and vdpau ====="
echo "============================================"
echo 	
echo -n "Installing Software ? Y/N "
proceed=
read -t 5 -n 1 proceed
	if   [ "$proceed" == "y" -o "$proceed" == "Y" ] ; then 
		echo
		instsft

	elif [ "$proceed" == "n" -o "$proceed" == "N" ] ; then
		echo
		echo			
		echo "Installation canceled"
		exit
	else
		echo
		echo
		echo "No or wrong Input - continue with Yes"
		instsft
	fi
echo



