#!/bin/bash

# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

NULL="none"

# Update this variable each version is bunped up
VERSION="0.3.2-1debian7"

SERVER=0
WORKBENCH=1
UTILITIES=2
CONNECTOR_PYTHON=3
CONNECTOR_ODBC=4

FLAG[${SERVER}]=0
FLAG[${WORKBENCH}]=0
FLAG[${UTILITIES}]=0
FLAG[${CONNECTOR_PYTHON}]=0
FLAG[${CONNECTOR_ODBC}]=0

PKG_LIST[${SERVER}]="mysql-server-5.5:mysql-server-5.6:mysql-community-server:mysql-commercial-server"
PKG_LIST[${WORKBENCH}]="mysql-workbench:mysql-workbench-community"
PKG_LIST[${UTILITIES}]="mysql-utilities-community"
PKG_LIST[${CONNECTOR_PYTHON}]="python-mysql.connector"
PKG_LIST[${CONNECTOR_ODBC}]=""

reset_flags () {
	FLAG[${SERVER}]=0
	FLAG[${WORKBENCH}]=0
	FLAG[${UTILITIES}]=0
	FLAG[${CONNECTOR_PYTHON}]=0
	FLAG[${CONNECTOR_ODBC}]=0
}

toggle_flag () {
	FLAG[${1}]=$((${FLAG[${1}]}^1))
}

get_flag () {
	echo ${FLAG[${1}]}
}

get_installed_version() {
	INSTALLED_PKG=${NULL}
	INSTALLED_PKG_VERSION=${NULL}

	IFS_BACKUP=${IFS}
	IFS=":"
	for PKG in ${1};
	do
		STATUS=$(dpkg -s $PKG 2> /dev/null | grep Status: | cut -d' ' -f4)
		if [ "$STATUS" = "installed" ];
		then
			INSTALLED_PKG=$PKG
		fi
	done
	IFS=${IFS_BACKUP}

	if [ "${INSTALLED_PKG}" != "${NULL}" ];
	then
		INSTALLED_PKG_VERSION=$(dpkg -s $INSTALLED_PKG 2> /dev/null | grep Version: | cut -d' ' -f2 | cut -d. -f-2)
	fi

	echo ${INSTALLED_PKG_VERSION}
}

auto_select_version() {
	ENABLE_REPO=${NULL}

	case "${1}" in
	${SERVER})
		ENABLE_REPO=$(auto_select_server_version ${2})
		;;
	${WORKBENCH})
		ENABLE_REPO=$(auto_select_workbench_version ${2})
		;;
	${UTILITIES})
		ENABLE_REPO=$(auto_select_utilities_version ${2})
		;;
	${CONNECTOR_PYTHON})
		ENABLE_REPO=$(auto_select_connector_python_version ${2})
		;;
	${CONNECTOR_ODBC})
		ENABLE_REPO=$(auto_select_connector_odbc_version ${2})
		;;
	esac

	echo ${ENABLE_REPO}
}

auto_select_server_version () {
	ENABLE_REPO=${NULL}

	case "${1}" in
	5.1)
		ENABLE_REPO=${NULL}
		;;
	5.5)
		ENABLE_REPO="mysql-5.6"
		;;
	5.6)
		ENABLE_REPO="mysql-5.6"
		;;
	5.7)
		ENABLE_REPO="mysql-5.7-dmr"
		;;
	none)
		ENABLE_REPO="mysql-5.6"
		;;
	esac

	echo ${ENABLE_REPO}
}

auto_select_workbench_version () {
	ENABLE_REPO=${NULL}

	case "${1}" in
	5.2)
		ENABLE_REPO=${NULL}
		;;
	none)
		ENABLE_REPO=${NULL}
		;;
	esac

	echo ${ENABLE_REPO}
}

auto_select_utilities_version () {
	ENABLE_REPO=${NULL}

	case "${1}" in
	x.x)
		ENABLE_REPO="utilities-1.4"
		;;
	none)
		ENABLE_REPO="utilities-1.4"
		;;
	esac

	echo ${ENABLE_REPO}
}

auto_select_connector_python_version () {
	ENABLE_REPO=${NULL}

	case "${1}" in
	0.3)
		ENABLE_REPO=${NULL}
		;;
	none)
		ENABLE_REPO=${NULL}
		;;
	esac

	echo ${ENABLE_REPO}
}

auto_select_connector_odbc_version () {
	ENABLE_REPO=${NULL}

	case "${1}" in
	x.x)
		ENABLE_REPO="connector-odbc-x.x"
		;;
	none)
		ENABLE_REPO="connector-odbc-x.x"
		;;
	esac

	echo ${ENABLE_REPO}
}
