#!/usr/bin/env bash
 
# define youe schema file name here
SCHEMA=schema/Schema.xml 
# define your database name here, will be overriden by 
# FIRST command line argument if given
DBNAME=
# define your database usernamename here, will be overriden by 
# SECOND command line argument if given
DBUSER=
# define your database password here, will be overriden by 
# THIRD command line argument if given
DBPASS=
# any extra args you need in your mysql connect string
# number of arguments should be specified within "" 
# FOURTH command line argument if given
DBARGS=""
# set your PHP5 bin dir path here, if it's not in PATH
# The path should be terminated with dir separator!
PHP5PATH=
# Set a special DB load filename here for custom installs
# If a filename is passed, civicrm_data.mysql AND the 
# passed file will be loaded instead of civicrm_generated.mysql.
# The DBLOAD file must be in the sql directory.
DBLOAD=

# ==========================================================
# No changes below, please.
# ==========================================================

CALLEDPATH=`dirname $0`

if [ "$1" = '-h' ] || [ "$1" = '--help' ]; then
	echo; echo Usage: setup.sh [schema file] [database data file] [database name] [database user] [database password] [additional args]; echo
	exit 0
fi


# fetch command line arguments if available
if [ ! -z $1 ] ; then SCHEMA=$1; fi
if [ ! -z $2 ] ; then DBLOAD=$2; fi
if [ ! -z $3 ] ; then DBNAME=$3; fi
if [ ! -z $4 ] ; then DBUSER=$4; fi
if [ ! -z $5 ] ; then DBPASS=$5; fi
if [ ! -z $6 ] ; then DBARGS=$6; fi

# verify if we have at least DBNAME given
if [ -z $DBNAME ] ; then
	echo "No database name defined!"
	exit 1
fi
if [ -z $DBUSER ] ; then
	echo "No database username defined!"
	exit 1
fi
if [ -z $DBPASS ] ; then
	read -p "Database password:"
	DBPASS=$REPLY
fi

# run code generator if it's there - which means it's
# checkout, not packaged code
if [ -d $CALLEDPATH/../xml ]; then
	cd $CALLEDPATH/../xml
	"$PHP5PATH"php GenCode.php $SCHEMA
fi

# someone might want to use empty password for development,
# let's make it possible - we asked before.
if [ -z $DBPASS ]; then # password still empty
	PASSWDSECTION=""
else
	PASSWDSECTION="-p$DBPASS"
fi

cd $CALLEDPATH/../sql
echo; echo Dropping $DBNAME database
mysqladmin -f -u $DBUSER $PASSWDSECTION $DBARGS drop $DBNAME
echo; echo Creating $DBNAME database
mysqladmin -f -u $DBUSER $PASSWDSECTION $DBARGS create $DBNAME
echo; echo Creating database structure
mysql -u $DBUSER $PASSWDSECTION $DBARGS $DBNAME < civicrm_41.mysql

# load civicrm_generated.mysql sample data unless special DBLOAD is passed
if [ -z $DBLOAD ]; then
    echo; echo Populating database with example data - civicrm_generated.mysql
    mysql -u $DBUSER $PASSWDSECTION $DBNAME < civicrm_generated.mysql
else
    echo; echo Populating database with required data - civicrm_data.mysql
    mysql -u $DBUSER $PASSWDSECTION $DBNAME < civicrm_data.mysql
    echo; echo Populating database with $DBLOAD data
    mysql -u $DBUSER $PASSWDSECTION $DBNAME < $DBLOAD
fi

echo; echo "DONE!"

# to generate a new data file do the foll:
# mysqladmin -f -uYourDBUser -pYourDBPassword drop YourDBName
# mysqladmin -f -uYourDBUser -pYourDBPassword create YourDBName
# mysql -uYourDBUser -pYourDBPassword YourDBName < civicrm_41.mysql
# mysql -uYourDBUser -pYourDBPassword YourDBName < civicrm_data.mysql
# mysql -uYourDBUser -pYourDBPassword YourDBName < civicrm_sample.mysql
# mysql -uYourDBUser -pYourDBPassword YourDBName < zipcodes.mysql
# php GenerateData.php
# echo "drop table zipcodes" | mysql -uYourDBUser -pYourDBPassword YourDBName
# mysqldump -c -e -t -n -uYourDBUser -pYourDBPassword YourDBName  > civicrm_generated.mysql
