#!/bin/sh

# Magento Installer
# Version 1 r3, 11-19-2009
# By JuanJose Galvez (DH Tech Support)
# Based on Magento command line install isntructions

# Abort on any errors
set -e

# DH Specific ENV variables
export MAGE_PEAR_PHP_BIN=/usr/local/php5/bin/php
export PATH=/usr/local/php5/bin:$PATH

# User Variables
DBHOST=""
DBNAME=""
DBUSER=""
DBPASS=""
DBPREFIX=""
URL=""
ADMINFIRST=""
ADMINLAST=""
ADMINEMAIL=""
ADMINUSER=""
ADMINPASS=""

# Other Needed Variables For Install
LICENSEAGREE="yes"
LOCALE="en_US"
TIMEZONE="America/Los_Angeles"
CURRENCY="USD"
REWRITES="yes"
SECURE="no"
SECUREURL=""
SECUREADMIN="no"

if test $# == 0
then
	echo "In order to install Magento all the following are required."
	echo "       -h: MySQL Database Hostname"
	echo "       -d: MySQL Database Name"
	echo "       -u: MySQL Database User"
	echo "       -p: MySQL Database Password"
	echo "       -x: Database Prefix"
	echo "       -a: Magento Shop Address (Example: http://shop.example.com/)"
	echo "       -f: Magento Admin First Name"
	echo "       -l: Magento Admin Last Name"
	echo "       -e: Magento Admin Email Address"
	echo "       -r: Magento Admin Login"
	echo "       -s: Magento Admin Pass (Must have letters and numbers.)"
	exit 1
fi

while getopts "h:d:u:p:x:a:f:l:e:r:s:" args
do
	case "$args" in
		h) DBHOST=$OPTARG;;
		d) DBNAME=$OPTARG;;
		u) DBUSER=$OPTARG;;
		p) DBPASS=$OPTARG;;
		x) DBPREFIX=$OPTARG;;
		a) URL=$OPTARG;;
		f) ADMINFIRST=$OPTARG;;
		l) ADMINLAST=$OPTARG;;
		e) ADMINEMAIL=$OPTARG;;
		r) ADMINUSER=$OPTARG;;
		s) ADMINPASS=$OPTARG;;
	esac
done
if [ -f magento-1.3.2.4.tar.gz ]
then
	tar -zxvf magento-1.3.2.4.tar.gz
else
	echo "You must upload a copy of magento-1.3.2.4.tar.gz before attempting the install."
	exit 1
fi

# Move the magento files to the current directory.
mv magento/* magento/.htaccess .

# Setup/Install Mage
./pear mage-setup .
./pear install magento-core/Mage_All_Latest-stable

#Remove pearlib cache
rm -rf downloader/pearlib/cache/* downloader/pearlib/download/*

/usr/local/php5/bin/php -f install.php -- \
--license_agreement_accepted "${LICENSEAGREE}" \
--locale "${LOCALE}" \
--timezone "${TIMEZONE}" \
--default_currency "${CURRENCY}" \
--db_host "${DBHOST}" \
--db_name "${DBNAME}" \
--db_user "${DBUSER}" \
--db_pass "${DBPASS}" \
--db_prefix "${DBPREFIX}" \
--url "${URL}" \
--use_rewrites "${REWRITES}" \
--use_secure "${SECURE}" \
--secure_base_url "${SECUREURL}" \
--use_secure_admin "${SECUREADMIN}" \
--admin_firstname "${ADMINFIRST}" \
--admin_lastname "${ADMINLAST}" \
--admin_email "${ADMINEMAIL}" \
--admin_username "${ADMINUSER}" \
--admin_password "${ADMINPASS}";

# Clean up empty magento folder and the tar.gz which is no longer needed
rm -rf magento/

# Echo install instructions for installing Modern theme.
# echo "If you would like to install the modern theme run: "
# echo "./pear install magento-core/Interface_Frontend_Default_Modern"
