#!/bin/sh -efu

LOCALSTATEDIR="${LOCALSTATEDIR:-/var/lib}"
MYSQLDIR="${MYSQLDIR:-$LOCALSTATEDIR/mysql}"
MYSQLCONF="${MYSQLCONF:-$MYSQLDIR/my.cnf}"

. shell-error
. shell-config
. alterator-service-functions

# Reads a named value from the MySQL configuration file.
# args: val-name
read_mysql_conf() {
	grep -q "^[[:space:]]*$1[[:space:]]*$" "$MYSQLCONF" \
	&& echo "$1" && return 0
	shell_config_get "$MYSQLCONF" "$1"
}

# Writes a named value to the MySQL configuration file.
# args: val-name val
write_mysql_conf() {
	local rdelim='[[:space:]]*=\?[[:space:]]*'
	local wdelim='='
	[ -n "$2" ] || wdelim=''
	shell_config_set "$MYSQLCONF" "$1" "$2" "$rdelim" "$wdelim"
}

# Comments a named value in the MySQL configuration file.
# args: val-name
comment_mysql_conf() {
	sed -i -e "s/^[[:space:]]*$1[[:space:]]*$/#&/" "$MYSQLCONF"
	shell_config_comment "$MYSQLCONF" "$1"
}

# Allows network serving for the MySQL daemon.
mysql_allow_network_access() {
	comment_mysql_conf 'skip-networking'
}

# Reloads the MySQL service.
mysql_service_reload() {
	service_control 'mysqld' reload
}

# Apply the changes (reloads the MySQL service).
mysql_apply() {
	mysql_service_reload
}

# Returns the MySQL service status.
mysql_service_status() {
	if service_control 'mysqld' is-active; then
		echo 'on'
	else
		echo 'off'
	fi
}

# Restores the MySQL service to the specified state.
# args: status
mysql_service_control() {
	[ "$1" = on ] &&
		service_control 'mysqld' start ||
		service_control 'mysqld' stop
}

# Starts the MySQL service.
mysql_service_start() {
	mysql_service_control 'on'
}

# Stops the MySQL service.
mysql_service_stop() {
	mysql_service_control 'off'
}

# Confugure the startup status of the MySQL service.
# args: startup-status
mysql_service_startup_control() {
	service_control 'mysqld' "$1"
}

# Runs an SQL command (query).
# args: workdir password cmd charset-name [db-name]
mysql_run_sql_cmd() {
	local workdir="$1"
	local PASSWORD="$2"
	local SQLCMD="$3"
	local CSNAME="${4:-utf8}"
	local qret=0
	local tmpname="$(mktemp "--tmpdir=$workdir" -t "mysql.XXXXXXXX")"
	local mysql="$(which mysql)" || fatal "MySQL client is not installed"
	expect -f - <<EOF > "$tmpname"
spawn -noecho sh -s -c "echo \\"$SQLCMD\\" | $mysql -B -u root -p \\"--default-character-set=$CSNAME\\" ${5:-}"
expect "*?assword:*" { send "$PASSWORD\\r" } eof { exit 1 }
set timeout -1
expect { "*\n" { exp_continue } eof { } }
set ret [wait]
exit [lindex \$ret 3]
EOF
	qret=$?
	[ $qret -ne 0 ] && sed -e '1 d' "$tmpname" 1>&2
	sed -e '1 d' "$tmpname"
	rm -f "$tmpname"
	return $qret
}

# Runs an SQL script.
# args: workdir password file charset-name [db-name]
mysql_run_sql_script() {
	local workdir="$1"
	local PASSWORD="$2"
	local SQLFILE="$3"
	local CSNAME="${4:-utf8}"
	[ -r "$SQLFILE" ] || fatal "Unable to read file $SQLFILE"
	local qret=0
	local tmpname="$(mktemp "--tmpdir=$workdir" -t "mysql.XXXXXXXX")"
	local mysql="$(which mysql)" || fatal "MySQL client is not installed"
	expect -f - <<EOF > "$tmpname"
spawn -noecho sh -s -c "cat \\"$SQLFILE\\" | $mysql -B -u root -p \\"--default-character-set=$CSNAME\\" ${5:-}"
expect "*?assword:*" { send "$PASSWORD\\r" } eof { exit 1 }
set timeout -1
expect { "*\n" { exp_continue } eof { } }
set ret [wait]
exit [lindex \$ret 3]
EOF
	qret=$?
	[ $qret -ne 0 ] && sed -e '1 d' "$tmpname" 1>&2
	sed -e '1 d' "$tmpname"
	rm -f "$tmpname"
	return $qret
}

# Checks if a database exists.
# args: workdir password db-name
mysql_db_exists() {
	mysql_run_sql_cmd "$1" "$2" "show databases like '$3';" 'utf8' \
	| grep -q "^$3[[:space:]]*$"
}

