#!/bin/bash

#############################################
##                                         ##
##  dbbenchmark install script containing  ##
##  selection of kind of installation such ##
##  as through download or from file.      ##
##  socket or tcp/ip connection etc        ##
##  v 1.0                                  ##
##  29th August 2010                       ##
##                                         ##
#############################################

ARGUMENT=$1

if [ -z "$1" ]
then
    ARGUMENT=notsilent
fi

clear		
bold=`tput bold`		
normal=`tput sgr0`

if [ "$ARGUMENT" != "silent" ]
then
    echo "                                                             "
    echo "     _ _     _                     _                         "
    echo "    | | |   | |                   | |                        "
    echo "  __| | |__ | |__   ___ _ __   ___| |__                      "
    echo " / _' | '_ \| '_ \ / _ \ '_ \ / __| '_ \                     "
    echo "| (_| | |_) | |_) |  __/ | | | (__| | | |                    "
    echo " \__,_|_.__/|_.__/ \___|_| |_|\___|_| |_|                    "
    echo "                                                             "
    echo "                                                             "
    echo " ${bold}brought to you by Matt Reid${normal}"
    echo " http://www.dbbenchmark.com "
    echo ""
    echo " ${bold}installer by Darren Cassar${normal} "
    echo " http://www.mysqlpreacher.com "
    echo ""
    echo " installer version 1.0"
    echo " release date 29th August 2010"
    echo ""
    echo ""
    echo ""
fi

## Checking if mysql binary is available

MYSQLBIN=`which mysql 2> /dev/null`
if [ "$MYSQLBIN" = "" -o ! -x "$MYSQLBIN" ]
then
    echo "It seems this install script can't find MYSQL binary."
    echo "Please make sure it is in your system by running 'mysql --version'"
    exit 1
fi

## Checking if python is available

GUNZIPBIN=`which python 2> /dev/null`
if [ "$GUNZIPBIN" = "" -o ! -x "$GUNZIPBIN" ]
then
    echo "It seems this install script can't find PYTHON binary."
    echo "Please make sure it is in your system by running 'python --version'"
    exit 1
fi

## Checking if tar is available

TARBIN=`which tar 2> /dev/null`
if [ "$TARBIN" = "" -o ! -x "$TARBIN" ]
then
    echo "It seems this installed can't find TAR binary."
    echo "Please make sure it is in your system by running 'tar --version'"
    exit 1
fi

## Create logs folder to keep logs of the installation.

if [ ! -d logs ]
then
    mkdir logs
fi

## Choose if installation should get the file from the net or just install it from a  local file.

echo ""
echo "Which kind of installation would you like to do?"
echo "1. Install from file on disk "
echo "2. Download and install (recommended) "
echo -n "Enter choice (default 2): "
read -e TOI                                                                  ## TOI = Type of Installation

if [ "$TOI" == "" ]
then
    TOI=2
fi

while [ "$TOI" -lt "1" ] && [ "$TOI" -gt "2" ]
do
    echo -n "Wrong value, please re-enter choice (default 2 i.e. Download and install): "
    read -e TOI
    if [ "$TOI" == "" ]
    then
        TOI=2
    fi
done

if [ -f release-url ]
then
    rm release-url
fi

if [ "$TOI" == "2" ]
then
    
    OS=`uname -a | cut -d ' ' -f 1`
    if [ "$OS" == "Linux" ] || [ "$OS" == "SunOS" ] || [ "$OS" == "FreeBSD" ] || [ "$OS" == "CYGWIN_NT-5.12" ]
    then
	wget http://dbbenchmark.googlecode.com/svn/trunk/release-url >/dev/null 2>&1
	if [ $? != 0 ]
	then
	    fetch http://dbbenchmark.googlecode.com/svn/trunk/release-url >/dev/null 2>&1
            if [ $? != 0 ]
            then
		echo "Version check problem connection to live versions on google code could not be established."
		exit 1
            fi
	fi
    elif [ "$OS" == "Darwin" ]
    then
	curl -o release-url http://dbbenchmark.googlecode.com/svn/trunk/release-url >/dev/null 2>&1
	if [ $? != 0 ]
	then
            echo "Version check problem connection to live versions on google code could not be established."
            exit 1
	fi
    else
	echo "OS not supported"
    fi
fi

## Version control - suggested

URL=`cat release-url`
FILE=`cat release-url | cut -d / -f 5`
LR=`cat release-url | cut -d - -f 3 | cut -d . -f 1-3`
BASEDIR=`pwd`

echo ""
echo -n "Enter version number or press enter for latest (default: $LR (latest version)): "
read -e VN                                                            ## VN = Version Number
if [ "$VN" == "" ]
then
    URL=`cat release-url`
else
    URL=$VN
fi


if [ "$TOI" == "2" ]
then
## Depending on type of OS, use wget, fetch or curl to download the package
    echo "Installation starting"
    
    if [ "$OS" == "Linux" ] || [ "$OS" == "SunOS" ] || [ "$OS" == "FreeBSD" ] || [ "$OS" == "CYGWIN_NT-5.12" ]
    then
	wget $URL
	if [ $? != 0 ]
	then
	    fetch $URL
            if [ $? != 0 ]
            then
		echo "Download problem, file does not exist or connection could not be established."
		exit 1
            fi
	fi
    elif [ "$OS" == "Darwin" ]
    then
	curl -o $FILE $URL
	if [ $? != 0 ]
	then
            echo "Download problem, file does not exist or connection could not be established."
            exit 1
	fi
    else
	echo "OS not supported"
    fi
    
    if [ $? != 0 ]
    then
	echo "Download problem, file does not exist or connection could not be established."
	exit 1
    fi
elif [ "$TOI" == "1" ]
then
    if [ -e $FILE ]
    then
	echo "Installation starting"
    else
	echo "$FILE was not found, please place it in the same folder as this install script"
	exit 1
    fi
else
    echo "Wrong value"
    exit 1
fi

gunzip -f $FILE
if [ $? != 0 ]
then
    echo "Problem encountered ... exiting"
    exit 1
fi

NFILE=`cat release-url |awk -F/ {'print $5'} |sed -e 's/.gz//g'`

tar -xf $NFILE
if [ $? != 0 ]
then
    echo "Problem encountered ... exiting"
    exit 1
fi

DIR=`cat release-url |awk -F/ {'print $5'} |sed -e 's/.tar.gz//g'`
cd $DIR

## Choose from fresh installation or upgrade

 ## FOU = Fresh install or Upgrade
 ## IM = Import choice
 ## FPN = File Path Name


echo ""		
echo -n "Enter mysql USER (default '$INSTUSER'): "		
read -e INSTUSER		

if [ "$INSTUSER" == "" ]		
then		
    INSTUSER=$INSTUSER		
fi

## Enter password (masked for security)

echo ""
echo -n "Enter mysql $INSTUSER Password (default ''): "

stty_orig=`stty -g`
trap "echo ''; echo 'Installation aborted during password entry'; stty $stty_orig ; exit" 1 2 15
stty -echo
read -e PASS
stty $stty_orig
trap "" 1 2 15

## Choose from TCP/IP or Socket file

echo ""
echo "Would you like to connect using:"
echo "1. TCP/IP"
echo "2. Socket file"
echo -n "Enter choice (default 1): "
read -e CH                                                           ## CH = Connection Handler

if [ "$CH" == "" ]
then
    CH=1
    
elif [ "$CH" == "2" ]
then
    echo ""
    echo -n "Enter mysql socket (default '/tmp/mysql.sock'): "
    read -e SOCK
    if [ "$SOCK" == "" ]
    then
        SOCK=/tmp/mysql.sock	 
    fi
    CONOPT="--socket=$SOCK"
fi

while [ "$CH" -lt "1" ] && [ "$CH" -gt "2" ]
do
    echo -n "Wrong value, please re-enter choice (default 1 i.e. TCP/IP): "
    read -e CH
    if [ "$CH" == "" ]
    then
        CH=1
    fi
done

if [ "$CH" == "" ] || [ "$CH" == "1"  ]
then
    echo -n "Enter mysql Hostname/IP (default '127.0.0.1'): "
    read -e HOST
    if [ "$HOST" == "" ]
    then
        HOST=127.0.0.1
    fi
    echo -n "Enter mysql Port (default '3306'): "
    read -e PORT
    if [ "$PORT" == "" ]
    then
        PORT=3306
    fi
    CONOPT="--host=$HOST -P $PORT"
fi	

DBEXISTS=`mysql -u $INSTUSER --password=$PASS $CONOPT -s -B --execute="select count(*) from information_schema.SCHEMATA where SCHEMA_NAME='dbbench'"`
if [ $? != 0 ]; then
    exit 1
fi

if [ "$CH" == "" ] || [ "$CH" == "1"  ] || [ "$CH" == "2" ]
then
    innodbyes=`mysql -u $INSTUSER --password=$PASS -s -B $CONOPT --execute="show engines" | cut -f 1,2 | grep InnoDB | cut -f 2`
    if [ $? != 0 ]; then
	exit 1
    fi
    echo -n "Create database? [Y|n]: "
    read CREATE
    if [ "$CREATE" = "Y" ] || [ "$CREATE" = "" ]; then
	
	if [ "$DBEXISTS" = "1" ]; then
            echo -n "DB already exists, are you sure you want to drop and create? [Y|n]: "
            read CRT
            
            if [ "$CRT" = "Y" ] || [ "$CRT" = "" ]; then
		mysql -u $INSTUSER --password=$PASS $CONOPT --execute="drop database dbbench;"
		if [ $? != 0 ]; then
		    exit 1
		fi
		echo "DB dropped: [OK]"
		mysql -u $INSTUSER --password=$PASS $CONOPT --execute="create database dbbench;"
		if [ $? != 0 ]; then
                    exit 1
		fi
		echo "DB created: [OK]"
            fi
	else
            mysql -u $INSTUSER --password=$PASS $CONOPT --execute="create database dbbench;"
	    if [ $? != 0 ]; then
                exit 1
	    fi
	    echo "DB created: [OK]"
	fi
    fi
    
    echo -n "Create user account and permissions? [Y|n]: "
    read PERMS
    if [ "$PERMS" = "Y" ] || [ "$PERMS" = "" ]; then
	mysql -u $INSTUSER --password=$PASS $CONOPT dbbench --execute="grant all on dbbench.* to 'dbbench'@'localhost' identified by 'dbbench'"
	if [ $? != 0 ]; then
            exit 1
        fi
	echo "Permissions granted: [OK]"
    fi
    
    echo -n "Create table? [Y|n]: "
    read TB
    if [ "$TB" = "Y" ] || [ "$TB" = "" ]; then
        if [ "$innodbyes" = 'YES' ]; then
	    ENGINE="InnoDB"
	else
	    ENGINE="MyISAM"
	fi
	mysql -u $INSTUSER --password=$PASS $CONOPT dbbench --execute="CREATE TABLE dbbench.dbbench (id BIGINT(64) UNSIGNED NOT NULL auto_increment,A BIGINT(64) UNSIGNED NOT NULL DEFAULT '0', B CHAR(255) NOT NULL DEFAULT '', C CHAR(255) NOT NULL DEFAULT '',PRIMARY KEY  (id),KEY B_ix (B)) ENGINE=$ENGINE DEFAULT CHARSET=UTF8"
	if [ $? != 0 ]; then
            exit 1
        fi	
	echo "Table created: [OK]"
    fi
else
    echo "Wrong value selected, please enter 1 or 2."
fi    
echo "Installation complete"
echo -n "Run the benchmark now? [Y|n]: "
read INS

if [ "$INS" = "Y" ] || [ "$INS" = "" ]; then
    ./dbbenchmark.py
fi

