#!/bin/sh

#GPL - 2006 -  By Jason Davis - mohadib@openactive.org - http://www.javanub.net

echo -e "\nSwingBot Builder version 1.0\n"
echo -e "This script will attempt to download the latest SwingBot and Jerklib sources"
echo -e "and compile them. For this process the script will create a directory called"
echo -e "SwingBotBuild in the current working directory. You will need Subversion "
echo -e "installed and a Sun Java SDK 1.5 or greater. This script expects to find svn, "
echo -e "javac, and jar in your path , so if they are not please stop and adjust your "
echo -e "path now.\n"

echo -e "Java: http://java.sun.com/j2se/1.5.0/download.jsp"
echo -e "Subversion: http://subversion.tigris.org/project_packages.html\n"

echo -e "------------------------------------------------------------------------------"

echo -e "\nPress 'y' then enter to continue\n";

read answer

if [  "$answer" != y  ];
		then echo "Exiting now";
		exit 1;
fi

# looking for svn
echo -e "Looking for Subversion now (svn)..."
svn_path=$(which svn);
if [ -z $svn_path ];
	 then echo -e "Fatal: svn not found! Please install subversion and assure svn is in your path to continue."
	 exit 1;
else
	echo -e "Found svn: $svn_path" 
fi

#looking for javac
echo -e "\nLooking for javac now..."
javac_path=$(which javac);
if [ -z $javac_path ];
	then echo "Fatal: javac not found! Please install a Sun Java SDK 1.5 or greater and link javac into your path to continue."
	exit 1;
else
	echo -e "Found javac: $javac_path"
fi

#looking for jar
echo -e "\nLooking for jar now..."
jar_path=$(which jar);
if [ -z $jar_path ];
	then echo "Fatal: jar not found! Please install a Sun Java SDK 1.5 or greater and link jar into your path to continue."
	exit 1;
else
	echo -e "Found jar: $jar_path"
fi

echo -e "\nAll needed apps found. Proceeding with install. \o/ "
echo -e "------------------------------------------------------------------------------"


pwd=$(pwd)
build_base_path="$pwd/SwingBotBuild";
echo -e "\nTrying to create directory $build_base_path\n"

if [ -d $build_base_path  ] ;
	then echo -e "$build_base_path already exists"
	echo -e "If I delete this directory your plugin data and bot.conf WILL BE LOST!";
	echo -e "Should I delete it? (Y/n then enter)"
	read answer
	if [ "$answer" != Y ];
		then echo "Directory not deleted. Exiting now";
		exit 1;
	else
		rm -Rf $build_base_path
		echo -e "\nDeleted $build_base_path\n"
		if mkdir $build_base_path;
			then echo -e "Created $build_base_path\n"
		else
			echo -e "Could not make $build_base_path\n"
			echo -e "Exiting now"
			exit 1;
		fi
	fi
else
	if mkdir $build_base_path ;	
		then echo -e "Created $build_base_path\n"
	else
			echo -e "Could not make $build_base_path\n"
			echo -e "Exiting now"
			exit 1;
		fi
fi

cd $build_base_path;

echo -e "\nGetting Jerklib files with svn now...\n"

svn co svn://www.javanub.net/home/mohadib/svn/repo/jerklib

echo -e "\nGetting SwingBot files with svn now...\n"

svn co svn://www.javanub.net/home/mohadib/svn/repo/swingbot

echo -e "\nGetting files complete.\n"

echo -e "Trying to make Jerklib\n"

cd jerklib/

javac jerklib/IRCConnectionManager.java


echo -e "\nJerklib compile complete , now making Jerklib jar\n";

jar -cf jerklib.jar jerklib/

echo -e "Jerklib jar construction complete\n";

echo -e "Trying to make SwingBot\n";

cd ../swingbot

javac -cp ../jerklib/jerklib.jar:. swingbot/SwingBot.java

echo -e "Swingbot compile complete\n";

echo -e "Building SwingBot plugins now\n";

javac -cp ../jerklib/jerklib.jar:. swingbot/plugins/*.java

echo -e "\nPlugins compile complete\n"

echo -e "Building SwingBot jar\n"

jar -cf swingbot.jar swingbot/

echo -e "SwingBot jar construction complete\n"

echo -e "Making persist dir for plugin data : $build_base_path/swingbot/swingbot_persist\n"

cd $build_base_path/swingbot
if mkdir swingbot_persist;
	then echo -e "Created $build_base_path/swingbot/swingbot_persist."
else
	echo -e "Could not make $build_base_path/swingbot/swingbot_persist , Please create a persist "
	echo -e "directory and edit your bot.conf to point there"
fi

echo -e "\n------------------------------------------------------------------------------"
echo -e "\nCongratulations , all builds where successful.. Swingbot is installed in\n"
echo -e "$build_base_path/swingbot\n"
echo -e " A default conf file to run with can be found in\n"
echo -e "$build_base_path/swingbot/swingbot/notes\n";

echo -e "To run Swingbot:";
echo -e "cd $build_base_path/swingbot/";
echo -e "java -cp $build_base_path/swingbot.jar:$build_base_path/jerklib/jerklib.jar:. swingbot.SwingBot $build_base_path/swingbot/swingbot/notes/bot.conf\n"

echo -e "Thanks for using Swingbot and Jerklib. Please email patches and suggestions/bugs to mohadib@openactive.org."
echo -e "Or drop by ##swing at irc.freenode.net ~ cheers :)\n"
echo -e "------------------------------------------------------------------------------"
