#!/bin/sh
# Podracer installer
# By Lorenzo Taylor

testroot()
	{
	if test ! $(whoami) = "root"
		then
		echo Only root can $1 Podracer.
		return 2
	fi
	}
installtimeout()
	{
	cc timeout.c -o /usr/bin/timeout ||return 1
	if test -d /usr/share/man/man1
		then
		cp timeout.1.gz /usr/share/man/man1 || return 1
		else
		cp timeout.1.gz /usr/man/man1 || return 1
	fi
	echo Timeout has been successfully installed.
	}

removetimeout()
	{
	rm /usr/bin/timeout >& /dev/null || return 1
	rm -f /usr/share/man/man1/timeout.1.gz >& /dev/null || return 1
	rm -f /usr/man/man1/timeout.1.gz >& /dev/null || return 1
	echo Timeout has been successfully removed.
	}

checkdeps()
	{
	fail=0
	if  test ! $(which curl) 
		then
		echo Curl is not installed.
		fail=1
	fi
	if test ! $(find /usr -name BitTorrent) 
		then
		echo BitTorrent is not installed.
		fail=1
	fi
	if test ! $(which screen)
		then
		echo Screen is not installed.
		fail=1
	fi
	if test ! $(which timeout)
		then
		echo Timeout was not installed on this system.  Installing it now.
		if ! installtimeout
			then
			echo Failed installing timeout.
			fail=1
		fi
	fi
	return $fail
	}
	
install()
	{
	if ! checkdeps
		then
		echo Failed dependency check.
		return 3
	fi
	echo Installing Podracer...
	mkdir -p /usr/share/doc/podracer || return 1
	cp CREDITS LICENSE README ChangeLog TODO sample.subscriptions \
	/usr/share/doc/podracer || return 1
	cp podracer /usr/bin || return 1
	chmod 755 /usr/bin/podracer || return 1
	cp podracer.conf /etc || return 1
	if test -d /usr/share/man/man1
		then
		cp podracer.1.gz /usr/share/man/man1 || return 1
		else
		cp podracer.1.gz /usr/man/man1 || return 1
	fi
	echo Podracer has been successfully installed.
	echo All documentation files may be found in /usr/share/doc/podracer
	echo The podracer program is /usr/bin/podracer
	echo And the system-wide configuration file is /etc/podracer.conf
	echo You can get info on how to use podracer by typing
	echo man podracer
	echo Keep winning the pod race with Podracer.
	}

uninstall()
	{
	echo Removing Podracer...
	rm -R /usr/share/doc/podracer >& /dev/null || return 1
	rm /usr/bin/podracer >& /dev/null || return 1
	rm  /etc/podracer.conf >& /dev/null || return 1
	rm -f /usr/share/man/man1/podracer.1.gz >& /dev/null || return 1
	rm -f /usr/man/man1/podracer.1.gz >& /dev/null || return 1
	echo Podracer has been successfully removed.
	}

description()
	{
	cat <<- END
	Podracer v1.4
	By Lorenzo Taylor

	To install Podracer, run this script with \`\`install'' as the argument.
	To remove Podracer from your system, run this script with \`\`remove'' or
	\`\`uninstall'' as the argument.
	If you used this package to install Timeout and wish to remove it as well, add
	\`\`timeout'' as a second argument after \`\`remove'' or \`\`uninstall''.

	Description:

	Podracer is a podcast downloader.  It takes a file with the URLs to all your
	podcast rss feeds and goes and gets the mp3s and stores them in a specified
	location.  It is a BASH script with an internal BitTorrent downloader
	written in Python.  See the CREDITS file for information on all the
	great work that helped make Podracer the winner of the pod race.
	END
	}

case $1 in
	install)
	testroot $1 && install || echo Podracer installation failed.  Aborting installer.
	;;
	remove|uninstall)
	testroot $1 && uninstall || echo Podracer is not installed.
	test $2 &&
	if test $2 = 'timeout'
		then
		echo Removing Timeout...
		removetimeout || echo Failed removing timeout.
	fi
	;;
	*)
	description
esac
