#!/bin/bash

MOCHA_DBNAME=$(cat /etc/mocha/dbname)
MOCHA_DBUSER=$(cat /etc/mocha/dbuser)
MOCHA_DBPASS=$(cat /etc/mocha/dbpass)

MOCHA_MYSQL="mysql -D $MOCHA_DBNAME -u $MOCHA_DBUSER --password=$MOCHA_DBPASS"

if [ ! -d ~/.config ]; then
	mkdir ~/.config
fi
if [ ! -d ~/.config/mocha ]; then
	mkdir ~/.config/mocha
fi

if [ -f ~/.config/mocha/tenant ]; then

	CURRENT_TENANT=$(cat ~/.config/mocha/tenant)
	#echo "current tenant: $CURRENT_TENANT"
	#echo "use 'mocha oms tenant select' to change"

fi

if [ "$1" == "install" ]; then

	if [ "$2" == "library" ]; then
		echo "installing Mocha OMS from library '$3'..."

		python3 /usr/lib/mocha/internal/mocha-install-library.py $CURRENT_TENANT $MOCHA_DBNAME $MOCHA_DBUSER $MOCHA_DBPASS $3

	else
		echo "installing Mocha OMS from SQL..."

		cd /usr/lib/mocha/sql/php
		php ./install_mysql.php $MOCHA_DBNAME $MOCHA_DBUSER $MOCHA_DBPASS

		$MOCHA_MYSQL -e "CALL mocha_select_tenant(1);" -e "CALL mocha_build_tenant_from_template(mocha_get_current_tenant(), 1);" -e "CALL mocha_release_tenant();"

	fi

elif [ "$1" == "tenant" ]; then

	if [ "$2" == "list" ]; then

		if [ "$3" == "names" ]; then

			$MOCHA_MYSQL -N -s -e "SELECT tenant_name FROM mocha_tenants"

		else

			$MOCHA_MYSQL -e "SELECT * FROM mocha_tenants"

		fi
	
	elif [ "$2" == "create" ]; then

		if [ $# -lt 3 ]; then

			echo "usage: mocha oms tenant create TENANTNAME"
			exit

		fi

		$MOCHA_MYSQL -e "CALL mocha_create_tenant('$3', NULL)"
		$MOCHA_MYSQL -e "CALL mocha_build_tenant_from_template(mocha_get_tenant_by_name('$3'), 1);"

		OLDTENANT=$(mocha oms tenant)
		mocha oms tenant select $3
		mocha oms install library /usr/share/mocha/libraries/net.alcetech.Mocha.System/
		mocha oms tenant select $OLDTENANT


	elif [ "$2" == "delete" ]; then

		if [ $# -lt 3 ]; then

			echo "usage: mocha oms tenant delete TENANTNAME"

		fi

		if [ "$3" == "super" ]; then

			echo "cannot delete the super tenant!"
			exit

		fi

		$MOCHA_MYSQL -e "DELETE FROM mocha_tenants WHERE tenant_name = '$3'"

	elif [ "$2" == "select" ]; then

		if [ $# -lt 3 ]; then

			echo "usage: mocha oms tenant select TENANTNAME"
			exit

		fi

		echo $3 > ~/.config/mocha/tenant
		echo "current tenant: $3"

	elif [ "$2" == "release" ]; then

		rm ~/.config/mocha/tenant
		echo "current tenant: (none)"

	elif [ "$2" == "" ]; then

		if [ -f ~/.config/mocha/tenant ]; then
			echo $(cat ~/.config/mocha/tenant)
		fi

	fi

elif [ "$1" == "user" ]; then

	if [ "$CURRENT_TENANT" == "" ]; then

		echo "no tenant selected; please 'mocha oms tenant select' first"
		exit

	fi

	if [ "$2" == "list" ]; then
		
		$MOCHA_MYSQL -e "SELECT * FROM mocha_instances WHERE class_id = 39 AND tenant_id = mocha_get_tenant_by_name('$CURRENT_TENANT')";

	elif [ "$2" == "add" ]; then

		if [ "$3" != "" ]; then

			$MOCHA_MYSQL -e "CALL mocha_select_tenant(mocha_get_tenant_by_name('$CURRENT_TENANT'));" -e "CALL mocha_create_user('$3', NULL);" -e "CALL mocha_release_tenant();"
			echo $3

		else

			echo "usage: mocha oms user add USERNAME"

		fi

	elif [ "$2" == "set-password" ]; then

		if [ "$3" == "" ]; then

			echo "usage: mocha oms user set-password USERNAME [PASSWORD]"
			exit

		fi

		PASSWORD=$4
		if [ "$PASSWORD" == "" ]; then

			echo "Changing password for $3."

			stty -echo
			read -p "New password: " PASSWORD
			stty echo
			echo ""

			stty -echo
			read -p "Retype new password: " PASSWORD2
			stty echo
			echo ""
			echo ""

			if [ "$PASSWORD" != "$PASSWORD2" ]; then
				echo "Sorry, passwords do not match."
				exit
			fi

		fi

		PASSWORD_SALT=$(pwgen -s 128 -N 1)
		PASSWORD_SALTED=$PASSWORD$PASSWORD_SALT
		PASSWORD_HASH=$(php /usr/lib/mocha/sql/php/hash_pw.php $PASSWORD_SALTED)

		# these two commands SHOULD execute atomically
		# BUT for some reason this doesn't work on initial provisioning of the vagrant vm
		# ........ for some reason, it works just fine when run manually though...
		# oh well, it's not like anything is going to happen in the few picoseconds between calls...
		$MOCHA_MYSQL -e "CALL mocha_select_tenant(mocha_get_tenant_by_name('$CURRENT_TENANT'));" -e "CALL mocha_set_attribute_value(mocha_get_user_by_username('$3'), mocha_get_instance_by_global_identifier('F377FC294DF14AFB96434191F37A00A9'), '$PASSWORD_HASH', NULL, NULL);" -e "CALL mocha_set_attribute_value(mocha_get_user_by_username('$3'), mocha_get_instance_by_global_identifier('8C5A99BC40ED4FA2B23FF373C1F3F4BE'), '$PASSWORD_SALT', NULL, NULL);" -e "CALL mocha_release_tenant();"
		if [ $? -ne 0 ]; then

			echo "mocha: password unchanged"
			exit

		fi

		echo "mocha: password updated successfully"
		exit

	else

		echo "usage: mocha oms user add|delete|lock|list|unlock|set-password"

	fi


elif [ "$1" == "instance" ]; then

	if [ "$2" == "list" ]; then

		# usage: mocha oms instance list attributes INSTID
		if [ "$3" == "" ]; then

			$MOCHA_MYSQL -e "SELECT id, tenant_id, CONCAT(class_id, '$', inst_id) AS inst_key, global_identifier FROM mocha_instances WHERE tenant_id = mocha_get_tenant_by_name('$CURRENT_TENANT')"

		elif [ "$3" == "attributes" ]; then

			if [ "$4" == "" ]; then

				echo "usage: mocha oms instance list attributes INSTID"
				exit

			fi

			# thanks https://stackoverflow.com/a/10520718
			ATT_INST_ID=$4
			ATT_INST_ID_CID=${ATT_INST_ID%\$*}
			ATT_INST_ID_IID=${ATT_INST_ID#*\$}

			$MOCHA_MYSQL -e "CALL mocha_select_tenant(mocha_get_tenant_by_name('$CURRENT_TENANT'));" -e "SELECT * FROM mocha_attributes WHERE tenant_id = mocha_get_tenant_by_name('$CURRENT_TENANT') AND src_inst_id = mocha_get_instance_by_key($ATT_INST_ID_CID, $ATT_INST_ID_IID);" -e "CALL mocha_release_tenant();"

		fi

	elif [ "$2" == "get" ]; then

		if [ "$3" == "by-gid" ]; then

			# thanks https://stackoverflow.com/a/911213
			if [ -t 1 ]; then

				$MOCHA_MYSQL -e "SELECT * FROM mocha_instances WHERE tenant_id = mocha_get_tenant_by_name('$CURRENT_TENANT') AND global_identifier = mocha_normalize_uuid('$4');"

			else

				$MOCHA_MYSQL -N -e "SELECT id FROM mocha_instances WHERE tenant_id = mocha_get_tenant_by_name('$CURRENT_TENANT') AND global_identifier = mocha_normalize_uuid('$4');"

			fi
		
		elif [ "$3" == "by-iid" ]; then

			# thanks https://stackoverflow.com/a/10520718
			INST_ID=$4
			INST_ID_CID=${INST_ID%\$*}
			INST_ID_IID=${INST_ID#*\$}

			# thanks https://stackoverflow.com/a/911213
			if [ -t 1 ]; then

				$MOCHA_MYSQL -e "CALL mocha_select_tenant(mocha_get_tenant_by_name('$CURRENT_TENANT'));" -e "SELECT * FROM mocha_instances WHERE tenant_id = mocha_get_tenant_by_name('$CURRENT_TENANT') AND class_id = $INST_ID_CID AND inst_id = $INST_ID_IID;" -e "CALL mocha_release_tenant();"

			else

				$MOCHA_MYSQL -N -e "SELECT id FROM mocha_instances WHERE tenant_id = mocha_get_tenant_by_name('$CURRENT_TENANT') AND class_id = $INST_ID_CID AND inst_id = $INST_ID_IID;"

			fi
		fi

	fi


elif [ "$1" == "attribute" ]; then

	if [ "$2" == "list" ]; then

		REL_COLUMNS="id, tenant_id, mocha_get_instance_key(src_inst_id) AS source_inst, mocha_get_instance_key(att_inst_id) AS attribute_inst, att_value, mocha_get_instance_key(usr_inst_id) AS user_inst, att_effective_date"

		# usage: mocha oms instance list attributes INSTID
		if [ "$3" == "" ]; then

			$MOCHA_MYSQL -e "CALL mocha_select_tenant(mocha_get_tenant_by_name('$CURRENT_TENANT'));" -e "SELECT $REL_COLUMNS FROM mocha_attributes WHERE tenant_id = mocha_get_tenant_by_name('$CURRENT_TENANT');" -e "CALL mocha_release_tenant();"

		elif [ "$3" == "for" ]; then

			if [ "$4" == "" ]; then

				echo "usage: mocha oms relationship list for INSTID"
				exit

			fi
			
			$MOCHA_MYSQL -e "CALL mocha_select_tenant(mocha_get_tenant_by_name('$CURRENT_TENANT'));" -e "SELECT $REL_COLUMNS FROM mocha_attributes WHERE tenant_id = mocha_get_tenant_by_name('$CURRENT_TENANT') AND src_inst_id = $4;" -e "CALL mocha_release_tenant();"

		fi

	elif [ "$2" == "get" ]; then

		if [ "$4" == "for" ]; then

			ATT_ID=$3
			INST_ID=$5

			$MOCHA_MYSQL -N -s -e "CALL mocha_select_tenant(mocha_get_tenant_by_name('$CURRENT_TENANT'));" -e "CALL mocha_get_attribute_value($INST_ID, $ATT_ID, NULL);" -e "CALL mocha_release_tenant();"
			
		fi

	elif [ "$2" == "set" ]; then

		if [ "$4" == "for" ]; then
		
			# usage: `mocha oms attribute set (att) for (inst) (value)`

			ATT_ID=$3
			INST_ID=$5
			ATT_VALUE=$6

			$MOCHA_MYSQL -e "CALL mocha_select_tenant(mocha_get_tenant_by_name('$CURRENT_TENANT'));" -e "CALL mocha_set_attribute_value($INST_ID, $ATT_ID, '$ATT_VALUE', NULL, NULL);" -e "CALL mocha_release_tenant();"
			
		fi

	fi


elif [ "$1" == "relationship" ]; then

	if [ "$2" == "list" ]; then

		REL_COLUMNS="id, tenant_id, mocha_get_instance_key(source_inst_id) AS source_inst, mocha_get_instance_key(relationship_inst_id) AS relationship_inst, mocha_get_instance_key(destination_inst_id) AS destination_inst, mocha_get_instance_key(user_inst_id) AS user_inst, effective_date"

		# usage: mocha oms instance list attributes INSTID
		if [ "$3" == "" ]; then

			$MOCHA_MYSQL -e "SELECT $REL_COLUMNS FROM mocha_relationships WHERE tenant_id = mocha_get_tenant_by_name('$CURRENT_TENANT')"

		elif [ "$3" == "for" ]; then

			if [ "$4" == "" ]; then

				echo "usage: mocha oms relationship list for INSTID"
				exit

			fi
			
			$MOCHA_MYSQL -e "CALL mocha_select_tenant(mocha_get_tenant_by_name('$CURRENT_TENANT'));" -e "SELECT $REL_COLUMNS FROM mocha_relationships WHERE tenant_id = mocha_get_tenant_by_name('$CURRENT_TENANT') AND source_inst_id = $4;" -e "CALL mocha_release_tenant();"

		fi

	elif [ "$2" == "get" ]; then

		if [ "$3" == "by-gid" ]; then

			# thanks https://stackoverflow.com/a/911213
			if [ -t 1 ]; then

				$MOCHA_MYSQL -e "CALL mocha_select_tenant(mocha_get_tenant_by_name('$CURRENT_TENANT'));" -e "SELECT * FROM mocha_instances WHERE tenant_id = mocha_get_tenant_by_name('$CURRENT_TENANT') AND global_identifier = mocha_normalize_uuid('$4');" -e "CALL mocha_release_tenant();"

			else

				$MOCHA_MYSQL -N -e "SELECT id FROM mocha_instances WHERE tenant_id = mocha_get_tenant_by_name('$CURRENT_TENANT') AND global_identifier = mocha_normalize_uuid('$4');"

			fi

		fi

	fi
	
else

	echo "usage: mocha oms tenant|user|instance|attribute|relationship"

fi

