#!/bin/sh
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#
# This installer was from the TWIG project found at 
# twig.screwdriver.net. It's a GPL web based IMAP client
# with calendaring and contact list management.
#
# Action						Date
# ===================================================== =================
# Mostly rewrote everything by Joshua Curtis    May 29, 2000
#
# Ported for use with MyThreads by Josh Levitsky	Jan 28, 2000
#
# Created by Greg Ross					Long, long ago...
# Modifed by Kendrick Vargas				Oct.  6, 1999
# 	Script cleaned up to add checks n' stuff
# Modifed by Greg Ross 					Nov. 14, 1999
#	Additional verbage and status messages	
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#

#\\\\\\\\\\\\\\\\\\\\\\ OPTIONS /////////////////////////////#
######
# these are the defaults to the questions

dbhost_d="localhost"
username_d="wwwrun"
password_d=""
database_d="links"
adminu_d="test"
adminp_d="test"


#//////////////////// END OPTIONS \\\\\\\\\\\\\\\\\\\\\\\\\\\#
 clear
 echo ""
 echo "MyThreads Install Script : v 4.25"
 echo "=============================================================="
 echo "Note: If Back Space does not work you can use the Delete key"
 echo ""
 echo "This shell script will install MyThreads in to a directory of"
 echo "your choice. You may also use this script to upgrade your"    
 echo "previous intallation."
 echo ""

# Make sure the user specified a path to use.
if [ ! "$1" ]; then
    	echo ""
   	echo "Usage: ./mythreads-install /path/to/installation"
   	echo "or just type the full path now:"
	read target_dir
else
   	target_dir=$1
fi

######
# Now we ask if they are upgrading
 echo ""
 echo "Are you upgrading from a older version? y|n:" 
 read upgrade

if [ ! upgrade ]; then
echo "Exiting. Nothing done..."
fi

######
# Now we ask for some info about there username loggin and
# password so we can edit the config file for them
 echo ""
 echo "Please answer the following questions so we can edit"
 echo "the config.php3 file for you when we install it. Any"
 echo "questions you are unsure about just hit return. Default"
 echo "options will be shown next to the questions."
 

######
# Ask for db host.
 echo ""
 echo "Database host: \"$dbhost_d\""
 read dbhost

# set the default if not aswered
if [ ! $dbhost ]; then
	dbhost="$dbhost_d"
echo "-> $dbhost"
fi

######
# Ask for db name
 echo ""
 echo "Database name: \"$database_d\""
 read database

if [ ! $database ]; then
	database="$database_d"
echo "-> $database"
fi

######
# Username
 echo ""
 echo "Database Username: \"$username_d\""
 read username

if [ ! $username ]; then
	username="$username_d"
echo "-> $username"
fi

######
# Password
 echo ""
 echo "Database Password: \"$password_d\""
 read password

if [ ! $password ]; then
	password="$password_d"
echo "-> $password"
fi

######
# Admin Username
 echo ""
 echo "Admin Username: \"$adminu_d\""
 read adminu

if [ ! $adminu ]; then
	adminu="$adminu_d"
echo "-> $adminu"
fi

######
# Admin Password
 echo ""
 echo "Admin Password: \"$adminp_d\""
 read adminp

if [ ! $adminp ]; then
	adminp="$adminp_d"
echo "-> $adminp"
fi

######
# Now we clear the screen and tell them what we are going to do
# and ask them one last time if its ok
clear
echo "Now we can install MyThreads. Just make sure the"
echo "answers below are correct before answering y or n"
echo ""
echo "Install Path.....: $target_dir"
echo "Upgrade..........: $upgrade"
echo "Database Host....: $dbhost"
echo "Database Username: $username"
echo "Database Password: $password"
echo "Admin Username...: $adminu"
echo "Admin Password...: $adminp"
echo ""
echo "Continue?: y|n"
read go

clear
if [ $go == "n" ]; then
	echo "Exiting.. Nothing installed"
	exit
else
	echo "Starting install.."
fi


if [ $upgrade == "y" ]; then
	target_dir="${target_dir}.new"
	echo ""
	echo "When the install is done update the new files. After testing it"
	echo "you can do a \"mv ${target_dir}.new ${target_dir}\". Please backup"
	echo "your existing files first. Your new install directory was changed"
	echo "to ${target_dir}.new"
	echo ""
	
fi

# Check to make sure we can use the specified directory
if [ ! -d $target_dir ]; then
   	mkdir -p $target_dir >&/dev/null
   	if [ ! $? = 0 ]; then
      		echo "Error: could not create $target_dir."
      		exit
   	fi
 	elif [ ! -w $target_dir ]; then
   		echo "Error: Insufficant rights on $target_dir."
  		exit
	fi

# Everything checks out... go forward with the install
echo ""
echo "Installing templates..."
# Need to add test for existance of current file.
# Need to figure out how to handle if a template exists
cp -R ../templates $target_dir

echo ""
echo "Installing images..."
cp -R ../images $target_dir

echo ""
echo "Installing configuration files..."
cat ../config.php3 |sed "s/\$host=.*;/\$host=\"${dbhost}\";/" |\
		    sed "s/\$username=.*;/\$username=\"${username}\";/" |\
		    sed "s/\$password=.*;/\$password=\"${password}\";/" |\
		    sed "s/\$admin_username=.*;/\$admin_username=\"${adminu}\";/" |\
		    sed "s/\$admin_password=.*;/\$admin_password=\"${adminp}\";/" > $target_dir/config.php3

echo ""
echo "Installing library modules..."
cp -R ../lib $target_dir

echo ""
echo "Installing root files..."
cp ../index.php3 $target_dir
cp ../fasttemplates.php3 $target_dir

echo ""
echo "Creating tmp folder for cache..."
mkdir $target_dir/tmp
chmod 777 $target_dir/tmp

echo ""
echo "Done........!"
echo ""
echo "At this point you want to create and populate"
echo "your database. At some point the installer will"
echo "do this for you, but for now it is a manual process."
echo "If you need help visit http://mythreads.sourceforge.net"
echo ""

