#!/bin/sh
#
# This is the installer script for Project Based Calendaring System
#
# Written by Roalt Zijlstra <roalt@kwenie.org>
# First released with PBCS v0.4

# START of Defines
SCRIPT_VERSION=0.1
INSTALL_VERSION=0.4
PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin
# END of Defines

clear
echo "Starting PBCS Installer $SCRIPT_VERSION which installs PBCS version" \
"$INSTALL_VERSION."
echo
echo "Be aware that you need to have the tarball of PBCS v$INSTALL_VERSION at hand" 
echo "and that you know a username and password combination for the MySQL database"
echo "which has database creation rights."
echo
echo "If the mysql command is not in the regular PATH then you need to know"
echo "where it can be found."
echo
echo "Press <RETURN> to proceed."
read BLOB

# Check if a pbcs tar.gz can be found.
if [ -f ./pbcs-v$INSTALL_VERSION.tar.gz ]; then
	PBCS_TAR=pbcs-v$INSTALL_VERSION.tar.gz
elif [ -f ../pbcs-v$INSTALL_VERSION.tar.gz ]; then
	PBCS_TAR=../pbcs-v$INSTALL_VERSION.tar.gz
elif [ -f ../../pbcs-v$INSTALL_VERSION.tar.gz ]; then
	PBCS_TAR=../../pbcs-v$INSTALL_VERSION.tar.gz
else
	echo "PBCS tarball not automatically found. It is needed for this script"
	echo "to install properly. Please enter the full path where it can be found."
	read TAR_PATH
	if [ -f $TAR_PATH/pbcs-v$INSTALL_VERSION.tar.gz ]; then
		PBCS_TAR=$TAR_PATH/pbcs-v$INSTALL_VERSION.tar.gz
    else
		echo "Tarball still not found, bailing out!"
		exit
	fi
fi

echo "Tarball found at $PBCS_TAR. Very good!"
echo
echo "Now let's see if we can find all needed commands for you."
# commando's: mysql, mysqladmin, tar
MYSQL=`which mysql`
MYSQLADMIN=`which mysqladmin`
if [ ! -f $MYSQL ]; then
	echo "The command 'mysql' was not found.\nPlease specify the path" \
	     "where it can be found:"
	read MYSQL_PATH
	if [ -f $MYSQL_PATH/mysql ]; then
		MYSQL=$MYSQL_PATH/mysql
		if [ -f $MYSQL_PATH/mysqladmin ]; then
			MYSQLADMIN=$MYSQL_PATH/mysqladmin
		else
			echo "Cannot find 'mysqladmin' in $MYSQL_PATH, bailing out!"
			exit
		fi
	else
		echo "Cannot find 'mysql' in $MYSQL_PATH, bailing out!"
		exit
	fi
fi

if [ ! -f $MYSQLADMIN ]; then
	echo "The command 'mysqladmin' was not found.\nPlease specify the path" \
	     "where it can be found:"
	read MYSQLADMIN_PATH
	if [ -f $MYSQLADMIN_PATH/mysqladmin ]; then
		MYSQLADMIN=$MYSQLADMIN_PATH/mysqladmin
	else
		echo "Cannot find 'mysqladmin' in $MYSQL_PATH, bailing out!"
		exit
	fi
fi

echo "Found $MYSQL and $MYSQLADMIN. Good!"
echo
echo "This installer will use a temporary directory to unpack the tar file."
echo "The default place for this is /tmp, is this OK (y/n) ?"
read TMP_OK

if [ "$TMP_OK" != "y" ]; then
	echo "Okay, then give me a path which I can savely use:"
	read TMP_DIR
	if [ ! -d $TMP_DIR ]; then
		echo "This is not a directort, bailing out."
		exit
	fi
else
	TMP_DIR=/tmp
fi
echo "We will now first install the tarball in the webserver HTML directory."
echo "On Apache it is mostly called the 'htdocs' directory. You can also "
echo "install it in a users home directory."
echo
echo "What should be the directory name where the PBCS files will be "
echo "stored in? (example: pbcs-v$INSTALL_VERSION) "
read PBCS_INSTALL_DIRNAME
echo "Now enter the path where the PBCS directory should be placed:"
read PBCS_INSTALL_PATH

PBCS_INSTALL_FULLPATH=$PBCS_INSTALL_PATH/$PBCS_INSTALL_DIRNAME
echo "Okay, I will install it in $PBCS_INSTALL_FULLPATH"
mkdir $PBCS_INSTALL_FULLPATH
if [ ! -d $PBCS_INSTALL_FULLPATH ]; then
	echo "Could not create destination directory. Bailing out!"
	exit
fi
echo
echo "Extracting tarball."
tar -C $TMP_DIR -xzf $PBCS_TAR
echo "Copying files into $PBCS_INSTALL_FULLPATH."
cp -a $TMP_DIR/Calendar2/* $PBCS_INSTALL_FULLPATH
echo "Removing temporary files."
rm -Rf $TMP_DIR/Calendar2
echo
echo "Now we get to the SQL part of PBCS. We need to create a new database"
echo "in which all PBCS data will be stored. But before we can do that we "
echo "need to know a username and password combination with enough rights"
echo "to create a database and add some tables."
echo
echo "Give an admin username for MySQL:"
read MYSQL_USER

echo "Give the password for it:"
read MYSQL_PASSWORD

clear
echo "What will be the name of the database?"
read DATABASE

echo "Creating database '$DATABASE'"
$MYSQLADMIN -u $MYSQL_USER --password="$MYSQL_PASSWORD" create $DATABASE
RETVAL=$?
if [ "$RETVAL" -eq "1" ]; then
	echo "Failed to create database. Bailing out!"
	exit
fi
echo "Inserting tables for PBCS."
$MYSQL -u $MYSQL_USER --password="$MYSQL_PASSWORD" $DATABASE < $PBCS_INSTALL_FULLPATH/Create-Tables.mysql
RETVAL=$?
if [ "$RETVAL" -eq "1" ]; then
	echo "Failed to insert tables. Bailing out!"
	exit
fi

echo "We are nearly done now."
echo "But we can do some extra configuration for you now."
echo
echo "The database needs to be accessed using a username and password."
echo "You can use the root user for that but that is not recommended."
echo "It will work though. The best thing to do is to add a special"
echo "'pbcs' user to the MySQL user table which has access to the PBCS"
echo "database only."
echo "Is there a special user which will be used to access the PBCS "
echo "database? (y/n)"
read S_USER_YN

if [ "$S_USER_YN" = "y" ]; then
	echo "Give the username: "
	read DB_USER
	echo "Give its password: "
	read DB_PASSWORD
else
	echo "Using the earlier given admin user and password combo."
	echo "If you want to change it later. Edit the db.php file in the PBCS "
	echo "install directory."
	DB_USER=$MYSQL_USER
	DB_PASSWORD=$MYSQL_PASSWORD
fi

cat < $PBCS_INSTALL_FULLPATH/scripts/install-templates/db1.php > \
      $PBCS_INSTALL_FULLPATH/db.php

echo "// The hostname of the database server for the PBCS database" >> \
      $PBCS_INSTALL_FULLPATH/db.php
echo "define(\"PBCS_DB_HOSTNAME\",\"localhost\");" >> \
      $PBCS_INSTALL_FULLPATH/db.php
echo "// The port number of that database" >> \
      $PBCS_INSTALL_FULLPATH/db.php
echo "define(\"PBCS_DB_PORT\",\"3306\");" >> \
      $PBCS_INSTALL_FULLPATH/db.php
echo "// The database name to select before doing queries" >> \
      $PBCS_INSTALL_FULLPATH/db.php
echo "define(\"PBCS_DB_DB_NAME\",\"$DATABASE\");" >> \
      $PBCS_INSTALL_FULLPATH/db.php
echo "// The username of the user that has privs to the pbcs database" >> \
      $PBCS_INSTALL_FULLPATH/db.php
echo "define(\"PBCS_DB_USERNAME\",\"$DB_USER\");" >> \
      $PBCS_INSTALL_FULLPATH/db.php
echo "// And the corresponding password" >> \
      $PBCS_INSTALL_FULLPATH/db.php
echo "define(\"PBCS_DB_PASSWORD\",\"$DB_PASSWORD\");" >> \
      $PBCS_INSTALL_FULLPATH/db.php

cat < $PBCS_INSTALL_FULLPATH/scripts/install-templates/db2.php >> \
      $PBCS_INSTALL_FULLPATH/db.php

echo "Created a modified db.php file in $PBCS_INSTALL_FULLPATH"
echo
echo "And ready! You should now be able to browse PBCS on your webserver."
echo "To change the login image go see the config/config.php file."

