#!/bin/bash
# This script is targeting CentOS release 6.5

readonly PROGNAME=$(basename $0)

# Usage
usage () {
	cat <<- EOF
	Usage: $PROGNAME -d <install_directory> -u <mysql_user> -p <mysql_password> -s <mysql_schema>
	
	Installs Magma Classic to the provided directory

	OPTIONS:
	  -d        directory to install magma to
	  -u        mysql user
	  -p        mysql password
	  -s        mysql schema

	Example: $PROGNAME -d ~/ -u magma -p volcano -s Lavabit

	EOF
}

# Process user input
while getopts ":d:u:p:s:" opt; do
    case $opt in
        d)
            export BASE_DIR=$(readlink -m "$OPTARG/magma")
            ;;
        u)
            export MYSQL_USER="$OPTARG"
            ;;
        p)
            export MYSQL_PASSWORD="$OPTARG"
            ;;
        s)
            export MYSQL_SCHEMA="$OPTARG"
            ;;
        \?)
            echo "Invalid option: -$OPTARG" >&2
            exit 1
            ;;
        :)
            echo "Option -$OPTARG requires an argument" >&2
            exit 1
            ;;
    esac
done


if [ -z "$BASE_DIR" \
	-o -z "$MYSQL_USER" \
	-o -z "$MYSQL_PASSWORD" \
	-o -z "$MYSQL_SCHEMA" ]; then
	usage
	echo "None of the user input is optional"
	exit 1
fi

export SALT=`echo "$(dd if=/dev/urandom bs=33 count=1 | base64 --wrap=300)"`
export SESSION=`echo "$(dd if=/dev/urandom bs=33 count=1 | base64 --wrap=300)"`

# Check for prerequisites
if [ ! -e /etc/init.d/mysqld ]; then
	echo "Can't find /etc/init.d/mysqld"
	echo "Is mysql-server installed?"
	exit 1
fi

if [ ! -e /etc/init.d/memcached ]; then
	echo "Can't find /etc/init.d/memcached"
	echo "Is memcached installed?"
	exit 1
fi

if [ ! -e /tmp/mysql.sock ]; then
	if [ -S /var/lib/mysql/mysql.sock ]; then
		echo "Creating a link to the local mysql socket"
		echo "You may need to enter the root user password"
		su - -c 'ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock'
	else
		echo "/var/lib/mysql/mysql.sock is not a Socket file"
		echo "Is mysql-server installed?"
		exit 1
	fi
fi

# Check limits.conf for our entry
grep "*                hard    memlock         1024" /etc/security/limits.conf

if [ $? -ne 0 ]; then
	echo "Adding entries to /etc/security/limits.conf"
	echo "You may need to enter the root user password"
	su - -c 'printf "*                hard    memlock         1024\n*                soft    memlock         1024" >> /etc/security/limits.conf'
fi

# Check user is running script from inside the tarball
if [ ! -d scripts/ ]; then
	echo "Please run this script from the same directory as magma/"
	exit 1
fi

# Build magma.config
if [ ! -e res/magma.config.stub ]; then
	echo "Can't find magma.config.stub file"
	exit 1
fi

# Substitute the placeholders in magma.config.stub with user input
envsubst < res/magma.config.stub > magma.config

# Reset database to factory defaults
scripts/database/schema.init.sh $MYSQL_USER $MYSQL_PASSWORD $MYSQL_SCHEMA

if [ $? -ne 0 ]; then
	echo "Resetting the database failed"
	exit 1
fi

# Generate expected directories
if [ ! -d "$BASE_DIR" ]; then
	mkdir -p "${BASE_DIR}/logs/"
	mkdir -p "${BASE_DIR}/spool/"
	mkdir -p "${BASE_DIR}/storage/"
	mkdir -p "${BASE_DIR}/servers/local/"
fi

# Final step is to put everything in place
cp -rf bin res scripts ${BASE_DIR}/.
mv -f magma.config ${BASE_DIR}/.


