#!/bin/bash
 
echo WANdisco Subversion Installer for SuSE 11
echo Please report bugs or feature suggestions to opensource@wandisco.com
echo 
echo Gathering some information about your system...

MINVERSION='1'
SVNVER='1.7.9'
NOW=$(date +"%b-%d-%y%s")

#functions
gather_info () {
        ARCH=`uname -m`
	SVNSTATUS=`rpm -qa|grep ^subversion-[0-9]|awk 'BEGIN { FS = "-" } ; { print $1 }'`
}
check_tools () {
        COMMANDS="zypper wget rpm"
        for C in $COMMANDS; do
                if [ -z "$(which $C)" ] ; then
                        echo "This installer uses the $C command which was not found in \$PATH."
                        exit 1
                fi
        done
}


check_suse_version ()
{
       if [ ! -e /etc/SuSE-release ]; then
                echo "No /etc/SuSE-release file, exiting"
                echo "You are most likely not using SuSE."
                echo "Installers for other operating systems are available from our downloads page:"
                echo "http://www.wandisco.com/subversion/download"
		echo "Exiting.."
                exit 1
        fi;
	cat /etc/SuSE-release |grep -e 'VERSION = 11'
	if [ $? == 0 ]; then
		echo "SuSE version 11.x confirmed.."
	else
                echo "You are most likely using an incompatible version of SuSE."
                echo "Installers for other operating systems are available from our downloads page:"
                echo "http://www.wandisco.com/subversion/download"
		echo "Exiting..."
                exit 1
	fi;
}

check_is_root ()
{
	if [[ $EUID -ne 0 ]]; then
   		echo "This script must be run as root" 1>&2
   		exit 1
	fi	
}

svn_remove_old ()
{
	if [ -f /etc/apache2/conf.d/subversion.conf ]; then
		echo Backing up /etc/apache2/conf.d/subversion.conf to /etc/apache2/conf.d/subversion.conf.backup-$NOW
		cp /etc/apache2/conf.d/subversion.conf /etc/apache2/subversion.conf.backup-$NOW
	fi
	echo Removing old packages...
	zypper --non-interactive remove subversion-ruby subversion-python subversion-perl subversion-server subversion-tools subversion &>/dev/null

}

add_repo_config ()
{
        echo Adding repository configuration to /etc/zypp/repos.d/
	reponame="WANdisco-svn17-suse11"
        if [ -f /etc/zypp/repos.d/$reponame.repo ]; then
		rm /etc/zypp/repos.d/$reponame.repo
	fi;
		echo "Importing GPG key"
		wget http://opensource.wandisco.com/RPM-GPG-KEY-WANdisco -O /tmp/RPM-GPG-KEY-WANdisco &>/dev/null
		rpm --import /tmp/RPM-GPG-KEY-WANdisco
		rm -rf /tmp/RPM-GPG-KEY-WANdisco
		zypper addservice --type=YUM http://opensource.wandisco.com/suse/svn17/ $reponame
		zypper --non-interactive ref
		echo " ------ Installing zypper repo: Done ------"	
}

install_svn ()
{
        echo Checking to see if you already have Subversion installed via rpm...
        if [[ "$SVNSTATUS" =~ subversion ]]; then
		echo ------------------------------------------------------------------------------------------------
        	echo Subversion is already installed on the system.
        	echo Do you wish to replace the version of subversion currently installed with the WANdisco version? 
		echo This action will remove the previous version from your system 
		echo "[y/n]"
		read svn_install_confirm
		if [ "$svn_install_confirm" == "y" -o "$svn_install_confirm" == "Y" ]; then
			svn_remove_old
			add_repo_config
			echo		
			echo Installing Subversion $SVNVER-$MINVERSION
			echo
			zypper --non-interactive install subversion-$SVNVER-$MINVERSION.$ARCH subversion-perl-$SVNVER-$MINVERSION.$ARCH subversion-python-$SVNVER-$MINVERSION.$ARCH subversion-tools-$SVNVER-$MINVERSION.$ARCH
			if [ $? -gt 0 ]; then
				echo "Problems installing while specifying package-version.arch, trying without.."
				zypper --non-interactive install subversion subversion-perl subversion-python subversion-tools
			fi;
 			echo Would you like to install apache and the apache SVN modules?
			echo "[y/n]"
			read dav_svn_confirm
			if [ "$dav_svn_confirm" == "y" -o "$dav_svn_confirm" == "Y" ]; then
				echo Installing apache and subversion modules
				zypper --non-interactive install apache2 subversion-server-$SVNVER-$MINVERSION.$ARCH
                                if [ $? -gt 0 ]; then
                                        echo "Problems installing while specifying package-version.arch, trying without.."
                                        zypper --non-interactive install apache2 subversion-server

                                fi;
				echo "Installation complete."
				echo "Please follow the instructions in /usr/share/doc/packages/subversion/README.WANdisco"
				echo "to configure your subversion server."
			fi
			
	       	else
			echo "Install Cancelled"
			exit 1
		fi

	else
		# Install SVN
		echo "Subversion is not currently installed"
		echo "Starting installation, are you sure you wish to continue?"
		echo "[y/n]"
		read svn_install_confirm
                if [ "$svn_install_confirm" == "y" -o "$svn_install_confirm" == "Y" ]; then
			add_repo_config
                        echo
                        echo Installing Subversion $SVNVER-$MINVERSION
                        echo
			zypper --non-interactive install subversion-$SVNVER-$MINVERSION.$ARCH subversion-perl-$SVNVER-$MINVERSION.$ARCH subversion-python-$SVNVER-$MINVERSION.$ARCH subversion-tools-$SVNVER-$MINVERSION.$ARCH 
                        if [ $? -gt 0 ]; then
                                echo "Problems installing while specifying package-version.arch, trying without.."
                                zypper --non-interactive install subversion subversion-perl subversion-python subversion-tools
                        fi;
                        
			echo Would you like to install apache HTTPD and the apache SVN modules?
			echo "[y/n]"
                        read dav_svn_confirm
                        if [ "$dav_svn_confirm" == "y" -o "$dav_svn_confirm" == "Y" ]; then
                                echo Installing apache and subversion modules
                                zypper --non-interactive install apache2 subversion-server-$SVNVER-$MINVERSION.$ARCH
				if [ $? -gt 0 ]; then
					echo "Problems installing while specifying package-version.arch, trying without.."
					zypper --non-interactive install apache2 subversion-server
					
				fi;
				echo "Installation complete."
				echo "Please follow the instructions in /usr/share/doc/packages/subversion/README.WANdisco"
                                echo "to configure your subversion server."
                        fi

                else
                        echo "Install Cancelled"
                        exit 1
                fi
		
        fi
	
}

install_32 ()
{
        echo Installing for $ARCH
	install_svn
}

install_64 ()
{
        echo Installing for $ARCH
	install_svn
}

#Main
check_is_root
check_suse_version
check_tools
gather_info


echo Checking your system arch
if [ "$ARCH" == "i686" -o "$ARCH" == "i386" -o "$ARCH" == "i586" ]; then
	if [ "$ARCH" == "i686" -o "$ARCH" == "i586" ]; then
		ARCH="i586"
	fi;
	install_32
elif [ "$ARCH" == "x86_64" ];
then
	install_64
else 
	echo Unsupported platform: $ARCH
	exit 1
fi
