#!/bin/bash
echo "######Madsonic update utility######"
echo "This script works for ubuntu" 
echo "You should be in root mode for it to work"
echo "Yes or no questions are in capital : Y or N"

# Stop subsonic deamon

echo "First, we stop madsonic deamon"
service madsonic stop

# Prompt for backup
echo "Do you want to backup your madsonic folder before continuing ? IT IS HIGHLY RECOMMENDED !!!!! (default=Y) (Y/N)"
read reply
if [ "$reply" = N ]
	then 	echo "OK OK, but don't complain then..."
	else	echo "Your subsonic database (only the /var/madsonic folder) will be saved in /var/subsonicYYMMDDhhmm"
		cp -R /var/madsonic /var/madsonic"$(date +%y%m%d%k%M)" 

fi

# This part makes sure java 7 is installed
echo "Your java version is :"
java -version
echo "Java 7 is needed for madsonic (your version number should be starting with 1.7). If you can't install it, make sure to install a madsonic compatible edition (SE), and answer yes to the next question."

echo "Do you hava java 7 installed ? (default=Y) (Y/N)"
read reply
if [ "$reply" = "N" ]

# Package list update
	then    apt-get update

# Prompt for package upgrades
	echo "Before installing java, do you want to upgrade your packages ? (default=Y) (Y/N)"
	read reply
	if [ "$reply" = "N" ]
		then echo "Ok, skipping package upgrade !"
		else apt-get upgrade
	fi

# Java installation

	apt-get install openjdk-7-jre
	else    echo "Ok, we skip the java install..."
fi


# Prompt for debian madsonic
echo "Do you have the latest madsonic DEBIAN package ? (default=Y) (Y/N) "
read reply

if [ "$reply" = "N" ] 
 	then

# This part installs the madsonic .deb package
		echo "paste the link to the madsonic deb package"
		read deblink
		echo "which build number ?"
		read debno
		wget "$deblink" -O madsonic"$debno".deb
		dpkg -i madsonic"$debno".deb

	else echo "Ok, we skip the debian package install..."  
fi

# This part makes sure this is the latest version of madsonic

echo "Is your madsonic the latest version ? (default=Y) (Y/N)"
read reply

if [ "$reply" = "N" ]
	then
	echo "Make sure you installed a .deb package before, because the war packages doesn't contain everything !!!!!"
	echo "Are you sure you want to install the latest .war package ? (default=N) (Y/N)"
	read reply
	if [ "$reply" = "Y" ]
		then
 
# This part updates madsonic with the latest war package
			echo "Paste the link to the madsonic war package"
			read madlink
			echo "Which build number is it?"
			read buildno
			wget "$madlink" -O madsonic"$buildno".zip
			unzip -o madsonic"$buildno".zip -d /usr/share/madsonic
			chmod +x /usr/share/madsonic/*
			else echo "Ok, we skip the .war update..."
	fi
	else echo "Ok, we skip the .war update..."

fi

# Start madsonic deamon
echo "Madsonic starting again..."
service madsonic start

echo "Thank you for installing madsonic !"

exit

