#!/bin/sh

# define our bail out shortcut function anytime there is an error - display the error message, then exit
# returning 1.
exerr () { echo -e "$*" >&2 ; exit 1; }

# Determine the current directory
# Method adapted from user apokalyptik at
# http://hintsforums.macworld.com/archive/index.php/t-73839.html
STAT=$(procstat -f $$ | grep -E "/"$(basename $0)"$")
FULL_PATH=$(echo $STAT | sed -r s/'^([^\/]+)\/'/'\/'/1 2>/dev/null)
START_FOLDER=$(dirname $FULL_PATH | sed 's|/thebrig_install.sh||')

# Store the script's current location in a file
echo $START_FOLDER > /tmp/thebriginstaller

# This first checks to see that the user has supplied an argument
if [ ! -z $1 ]; then
    # The first argument will be the path that the user wants to be the root folder.
    # If this directory does not exist, it is created
    BRIG_ROOT=$1    
    
    # This checks if the supplied argument is a directory. If it is not
    # then we will try to create it
    if [ ! -d $BRIG_ROOT ]; then
        echo "Attempting to create a new destination directory....."
        mkdir -p $BRIG_ROOT || exerr "ERROR: Could not create directory!"
    fi
	mkdir -p temporary || exerr "ERROR: Could not create install directory!"
	cd temporary || exerr "ERROR: Could not access install directory!"
#    cd $BRIG_ROOT || exerr "ERROR: Could not access install directory!"
else
# We are here because the user did not specify an alternate location. Thus, we should use the 
# current directory as the root.
    BRIG_ROOT=$START_FOLDER
fi
# touch /tmp/thebrig.tmp

MAJ_REL=$(uname -r | cut -d- -f1 | cut -d. -f1)
MIN_REL=$(uname -r | cut -d- -f1 | cut -d. -f2)

# Prevent users from breaking their system
if [ $MAJ_REL -lt 10  ]; then
		if [ $MAJ_REL -lt 9 -o $MAJ_REL -eq 9 -a $MIN_REL -lt 3 ]; then
			BRANCHNAME="working"
		else
			BRANCHNAME="alexey"
		fi
	else
	BRANCHNAME="alcatraz"
fi
fetch https://github.com/fsbruva/thebrig/archive/${BRANCHNAME}.zip || exerr "ERROR: Could not write to install directory!"

# Extract the files we want, stripping the leading directory, and exclude
# the git nonsense
echo "Unpacking the tarball..."
tar -xf ${BRANCHNAME}.zip --exclude='.git*' --strip-components 1
echo "Done!"
rm ${BRANCHNAME}.zip

# Run the change_ver script to deal with different versions of TheBrig
/usr/local/bin/php-cgi -f conf/bin/change_ver.php

filever="/tmp/thebrigversion"
# The file /tmp/thebrigversion might get created by the change_ver script
# Its existence implies that we need to carry out the install procedure
if [ -f "$filever" ]
then
	action=`cat ${filever}` 
	# echo "Thebrig "${action}
	if [ ${ALCATRAZ} = 0 ]. then
		if [ `uname -p` = "amd64" ]; then
			echo "Renaming 64 bit ftp binary"
			mv conf/bin/ftp_amd64 conf/bin/ftp
			rm conf/bin/ftp_i386
		else
			echo "Renaming 32 bit ftp binary"
			mv conf/bin/ftp_i386 conf/bin/ftp
			rm conf/bin/ftp_amd64
		fi
	fi
	cp -r * $BRIG_ROOT/
	mkdir -p /usr/local/www/ext/thebrig
	cp $BRIG_ROOT/conf/ext/thebrig/* /usr/local/www/ext/thebrig
	cd /usr/local/www
	# For each of the php files in the extensions folder
	for file in /usr/local/www/ext/thebrig/*.php
	do
	# Check if the link is already there
		if [ -e "${file##*/}" ]; then
			rm "${file##*/}"
		fi
			# Create link
		ln -s "$file" "${file##*/}"
		done
	echo $BRIG_ROOT > /tmp/thebrig.tmp
	echo "Congratulations! Thebrig ${action} . Navigate to rudimentary config tab and push Save "
else
# There was not /tmp/thebrigversion, so we are already using the latest version
	echo "You use fresh version"
fi
# Clean after work
cd $START_FOLDER
# Get rid of staged updates
rm -Rf temporary/*
rmdir temporary
rm /tmp/thebriginstaller
if [ -f "$file" ] 
then 
	rm /tmp/thebrigversion
fi
currentdate=`date -j +"%Y-%m-%d %H:%M:%S"`
echo "[$currentdate]: TheBrig installer!: installer: ${action} successfully" >> $BRIG_ROOT/thebrig.log