#!/bin/bash

SVER=1.0.5

##############################################################################
#  setup-sdbroker - Supportconfig Diagnostic Pattern Setup Tool
#  Copyright (C) 2017 SUSE LLC
#
# Description:  Creates the main mySQL database for the Supportconfig Diagnostic
#               Tool infrastructure
# Runs on:      Broker Server
# Modified:     2017 Dec 18
#
##############################################################################
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; version 2 of the License.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, see <http://www.gnu.org/licenses/>.
#
#  Authors/Contributors:
#     Jason Record (jason.record@suse.com)
#
##############################################################################

DB_USER='root'
DB_NAME='SCAPatterns'
DB_ALL_USERS="sdpuser"
CURRENT_SCRIPT=$(basename $0)
SDP_CONFIG='/etc/sca/sdp.conf'
WWW_SDP_CONFIG='/srv/www/htdocs/sca/sdp-config.php'
DEF_SCHEMA='/usr/share/doc/packages/sca-appliance-patdev'
CONFIRMED=0
EFMT="%-30s %s"
FMT="${EFMT}\n"

##############################################################################
# Functions: Local
##############################################################################

title() {
	echo "####################################################################"
	echo "# Supportconfig Analysis Pattern Setup Tool v$SVER"
	echo "####################################################################"
	echo
}

showHelp() {
	clear
	title
	[ -n "$1" ] && { echo "$1"; echo; }
	echo "Usage: $CURRENT_SCRIPT -p <root_password> [OPTIONS]"
	echo 'Description:'
	echo '  Configures the MySQL custom supportconfig diagnostic pattern database.'
	echo
	echo 'Options:'
	echo '  -h Show this screen and exit'
	echo '  -p <str> MySQL root password'
	echo '  -c <str> Company name for pattern generator'
	echo '  -d <str> Company email domain for pattern generator'
	echo '  -b <str> MySQL sdpuser password'
	echo '  -s <str> Path to schema files'
	echo "     Default: $DEF_SCHEMA"
	echo '  -y Confirms the deletion of a pre-existing database and users'
	echo
}

main() {
	[ -s ${SCHEMA_PATH}/${DB_NAME}_DB.sql ] || { showHelp "ERROR: Schema file not found: ${SCHEMA_PATH}/${DB_NAME}_DB.sql"; exit 5; }
	[ -s ${SCHEMA_PATH}/${DB_NAME}_Users.sql ] || { showHelp "ERROR: Schema file not found: ${SCHEMA_PATH}/${DB_NAME}_Users.sql"; exit 5; }
	DB_ROOT="-u $DB_USER -p${DB_PASS}"
	mysql $DB_ROOT -e "use $DB_NAME" &>/dev/null
	RC=$?
	if [ $RC -eq 0 ]; then
		if (( CONFIRMED )); then
			DELETE_DB=1
		else
			printf "Do you want to delete the database? [y/N]: "
			read GO
			case $GO in
			y*|Y*) DELETE_DB=1 ;;
			*) DELETE_DB=0 ;;
			esac
		fi
		if (( DELETE_DB )); then
			printf "$FMT" "Deleting Database:" $DB_NAME
			mysql $DB_ROOT -e "DROP DATABASE $DB_NAME"
		else
			exit 1
		fi
	fi

	USER_FOUND=$(mysql -NB $DB_ROOT -e "SELECT User FROM user WHERE User='sdbroker'" mysql 2>/dev/null)
	if [ -n "$USER_FOUND" ]; then
		if (( CONFIRMED )); then
			DELETE_USERS=1
		else
			printf "Do you want to delete the users? [y/N]: "
			read GO
			case $GO in
			y*|Y*) DELETE_USERS=1 ;;
			*) DELETE_USERS=0 ;;
			esac
		fi
		if (( DELETE_USERS )); then
			printf "$FMT" "Deleting Users:" "$DB_ALL_USERS"
			mysql $DB_ROOT -e "DROP USER 'sdpuser'@'localhost'"
		else
			exit 1
		fi
	fi

	printf "$FMT" "Creating Database:" $DB_NAME
	mysql $DB_ROOT -e "CREATE DATABASE $DB_NAME"
	RC=$?
	[ $RC -gt 0 ] && { echo; exit $RC; }
	cat ${SCHEMA_PATH}/${DB_NAME}_DB.sql | mysql $DB_ROOT $DB_NAME
	RC=$?
	if [ $RC -eq 0 ]; then
		printf "$FMT" "Creating Users:" "$DB_ALL_USERS"
		cat ${SCHEMA_PATH}/${DB_NAME}_Users.sql | mysql $DB_ROOT
	fi

	echo
	if [ -e $SDP_CONFIG ]; then
		sed -i -e "s!^DB_PASS=.*!DB_PASS='$OPT_PASS'!" $SDP_CONFIG
	else
		echo "ERROR: File not found: $SDP_CONFIG"
		echo
		exit 3
	fi

	printf "$FMT" "Updating Template Data:" 'Done'
	[[ -n "$OPT_EMAILDOM" ]] && sed -i -e "s/\$EmailDomain\s=\s''/\$EmailDomain = '$OPT_EMAILDOM'/" $WWW_SDP_CONFIG
	[[ -n "$OPT_COMPANY" ]] && sed -i -e "s/\$Company\s=\s''/\$Company = '$OPT_COMPANY'/" $WWW_SDP_CONFIG

	echo
	DB_CONNECT="-u sdpuser -p${OPT_PASS} $DB_NAME"
	mysql $DB_CONNECT -e "SHOW TABLES"
	if [ $? -gt 0 ]; then
		echo
		exit 3
	fi
	echo
}

DB_PASS=''
OPT_PASS='sdpuser_password'
OPT_SCHEMA=''
OPT_INSRC=''
OPT_COMPANY=''
OPT_EMAILDOM=''
while getopts ':hb:c:d:p:s:y' TMPOPT
do
	case $TMPOPT in
		\:) showHelp "ERROR: Missing Argument -$OPTARG"; exit 1 ;;
		\?) showHelp "ERROR: Invalid Option -$OPTARG"; exit 2 ;;
		h) title; showHelp; exit 0 ;;
		c) OPT_COMPANY=$OPTARG ;;
		d) OPT_EMAILDOM=$OPTARG ;;
		p) DB_PASS=$OPTARG ;;
		b) OPT_PASS=$OPTARG ;;
		s) OPT_SCHEMA=$OPTARG ;;
		y) CONFIRMED=1 ;;
	esac
done
[[ -z "$DB_PASS" ]] && { showHelp "ERROR: Missing MySQL Root Password"; exit 1; }
[[ -z "$OPT_SCHEMA" ]] && SCHEMA_PATH=$DEF_SCHEMA || SCHEMA_PATH=$OPT_SCHEMA

title
main


