#!/bin/bash

# GameCP Installation Script!
# GameCP - www.gamecp.com

# Developed by: Tristan Morris
# Credits to: William (Providing general banter), GameCP Clients (Feedbad & errors)

# Colour Script modified from: http://howtos.linux.com/guides/abs-guide/colorizing.shtml

# This program in FULL or in PART is not allowed to:
# Used in other non GameCP produced applications, any form.
# Be Distributed in ANY form, even bits on messanger programs.
# Sold for ANY reason.

# Revision History
# Revision: 1.0 - 8 Dec 2005 - Inital Version.
# Revision: 1.1 - 18 Dec 2005 - Fixed mysql permissions & now removed compiled templates
# Revision: 1.2 - 5 Mar 2005 - Dependency checking improved. Colour added. Errors handled better. Updated to get stable archive
# Revision: 1.3 - 23 Apr 2009 - Added selinux, allowusers and new apacheuser chown permissions
# Revision: 1.3b - 23 Apr 2009 - Added -e on echoes that print colors


RANDOM=`head -c4 /dev/urandom| od -An -tu4`
#### Variables ####

# Command Aliases
alias echo="echo -e"

# Colour Definitions - WITH CORRECT SPELLING! DAMN YANKS
black='\E[30;40m'
red='\E[31;40m'
green='\E[32;40m'
yellow='\E[33;40m'
blue='\E[34;40m'
magenta='\E[35;40m'
cyan='\E[36;40m'
white='\E[37;40m'

ok="\E[32;40m Ok \E[37;40m"
failed="\E[31;40m Failed \E[37;40m"

# Some vars used for colour functions
blueonblack='\E[37;44m'
bold=1
underline=2
default_msg="No message"

# Script Vars
DATE=`date +'%Y-%m-%d'`;
BACKUP_TAR_FILE_NAME="GCPBackup.$DATE.$RANDOM.tar.gz";
DOWNLOAD_ARCHIVE="GameCPX.tar.gz";


#### Functions ####

# Bold text output
becho() {
	message=${1:-$default_msg}	
	echo -e "\033[1m$message\033[0m"
}

uecho() {
	message=${1:-$default_msg}	
	echo -e "\033[4m$message\033[0m"
}


# Colourized text output
cecho () {
	message=${1:-$default_msg}
	color=${2:-$white}
	control_code=${3:-0}
	
	# Check if the text should be bold or underlined
	if [ "$control_code" -eq "2" ] ; then
		uecho "$color$message"
	else	
		if [ "$control_code" -eq "0" ] ; then
			echo -e "$color$message"
		else 
			becho "$color$message"
		fi	
	fi
	
	tput sgr0
	return
}

# Show error & quit
show_error () {
	error_message=${1:-$default_msg}
	typical_command=${2:-"NONE"}
	echo ""
	cecho "An error has occurred!" $red $underline
	cecho "Error: $error_message" $red
		
	if [ "$typical_command" != "NONE" ] ; then
		cecho "Typical Fix/Command: $typical_command" $green
	fi
	
	#echo "Further support is available at www.gamecp.com"
	echo ""
	
	exit -1
}


### Main Code Execution ###
# Clear screen
clear;
cecho " GameCP Installer - www.gamecp.com - Version: 1.3b                            " $blueonblack $bold

# Check terminal size
N_COLS=`tput cols`
N_LINES=`tput lines`

# Check terminal size
if [ $N_COLS -lt 80 ] || [ $N_LINES -lt 20 ]; then
	show_error "Your terminal window must have a minimum size of 80 x 20"
fi

# Output what the script does :/
echo "";
echo -e "Welcome to the GameCP Download script. \nThis script will download and extract GameCP to the current folder.";
echo "";
echo ""


### Backing up current directory ###
echo "Performing Backup";

# Check if backup directory exists
if [ -d backups/ ]; then
	echo " - Backing up to: backups/";
else
	echo " - Creating directory: backups/";
	mkdir backups/
fi

# Backups Security - .htaccess

if [ -e backups/.htaccess ] ; then
	echo " - .htaccess exists";
else
	echo " - Creating .htaccess";
	echo "<limit POST GET PUT DELETE>" >> backups/.htaccess
	echo "order deny,allow" >> backups/.htaccess
	echo "deny from all" >> backups/.htaccess
	echo "</limit>" >> backups/.htaccess
fi;
if [ ! -e backups/index.html ] ; then
	echo "Directory Disabled" >> backups/index.html
fi;

# Perform TAR of current directory, skip backups/
echo " - Creating backup $BACKUP_TAR_FILE_NAME";
tar --exclude=backups -czf $BACKUP_TAR_FILE_NAME * 

echo " - Moving backup into backups/";
mv $BACKUP_TAR_FILE_NAME backups/
echo "";

### WGET Remote Archive ###
echo "Downloading GameCP Core";
if [ -e $DOWNLOAD_ARCHIVE ] ; then
	echo " - Removing existing upgrade tarball";
	rm -f $DOWNLOAD_ARCHIVE*
fi;

# Actual WGET
echo " - Downloading remote archive, this may take a few minutes";
wget -q http://cvs.gamecp.com/$DOWNLOAD_ARCHIVE -O $DOWNLOAD_ARCHIVE

### Extraction ###
# Check excludes exists
if [ -e includes/extract_exclude.txt ] ; then
	echo " - Extracts excludes exists";
	echo " - - includes/extract_exclude.txt";
	
	echo " - Extracting Archive";
	tar -X includes/extract_exclude.txt -zxf $DOWNLOAD_ARCHIVE
	
else
	echo " - Extracts excludes does not exist";
	echo " - - includes/extract_exclude.txt";
	
	echo " - Extracting Archive";
	tar -zxf $DOWNLOAD_ARCHIVE
fi;

### Cleanup ###
if [ -e $DOWNLOAD_ARCHIVE ] ; then
	echo " - Removing install/upgrade tarball";
	rm -f $DOWNLOAD_ARCHIVE*
fi;

echo ""

# Determine if upgrade or not
if [ -e includes/mysql.inc.php ] ; then
	chown -R --reference . *
	chmod -R 755 *
	chmod 644 includes/mysql.inc.php
	echo "Successfuly updated your GameCP build.";
	echo "Navigate to http://yourwebserver.com/gcp/installer/ to completed installation.";
	echo " 'yourwebserver.com/gcp' is an example and should be replaced with your url.";
else
	echo "Successfuly downloaded and installed GameCP.";
	echo "Navigate to http://yourwebserver.com/gcp/installer/ to completed installation.";
	echo " 'yourwebserver.com/gcp' is an example and should be replaced with your url.";
	
	### More installation steps, create keys, create tables etc! ###
	echo " " >> includes/mysql.inc.php
	chmod -R 755 *
	chown -R --reference . *
fi;


echo ""
exit;
