#!/bin/bash
# info: change phpmyadmin/phppgadmin alias url
# options: TYPE ALIAS
#
# example: v-change-sys-db-alias pma phpmyadmin
#          # Sets phpMyAdmin alias to phpmyadmin
#
# example: v-change-sys-db-alias pga phppgadmin
#          # Sets phpPgAdmin alias to phppgadmin
#
# This function changes the database editor url in
# apache2 or nginx configuration.

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Argument definition
type=$1
alias=$2

# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

check_args '2' "$#" 'type alias'

is_common_format_valid "$alias" "Alias"

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# Detect common allowed entries for phpMyAdmin
if [ "$type" = "pma" ] || [ "$type" = "PMA" ] || [ "$type" = "phpmyadmin" ]; then
	# Set database editor friendly name
	db_editor="phpMyAdmin"
	# Set new alias value
	$BIN/v-change-sys-config-value 'DB_PMA_ALIAS' "$alias"

	# Replace old configuration files and update alias
	if [ -e "/etc/apache2/conf.d/phpmyadmin.inc" ]; then
		rm -f /etc/apache2/conf.d/phpmyadmin.inc
		cp -f $HESTIA_INSTALL_DIR/pma/apache.conf /etc/apache2/conf.d/phpmyadmin.inc
		sed -i "s|%pma_alias%|$alias|g" /etc/apache2/conf.d/phpmyadmin.inc

		# Restart services
		$BIN/v-restart-service apache2
	fi

	if [ -e "/etc/nginx/conf.d/phpmyadmin.inc" ]; then
		rm -f /etc/nginx/conf.d/phpmyadmin.inc
		cp -f $HESTIA_INSTALL_DIR/nginx/phpmyadmin.inc /etc/nginx/conf.d/phpmyadmin.inc
		sed -i "s|%pma_alias%|$alias|g" /etc/nginx/conf.d/phpmyadmin.inc

		# Restart services
		$BIN/v-restart-service nginx
	fi
fi

# Detect common allowed entries for phpPgAdmin
if [ "$type" = "pga" ] || [ "$type" = "PGA" ] || [ "$type" = "phppgadmin" ]; then
	# Set database editor friendly name
	db_editor="phpPgAdmin"

	# Set new alias value
	$BIN/v-change-sys-config-value 'DB_PGA_ALIAS' "$alias"

	# Replace old configuration files and update alias
	if [ -e "/etc/apache2/conf.d/phppgadmin.inc" ]; then
		rm -f /etc/apache2/conf.d/phppgadmin.inc
		cp -f $HESTIA_INSTALL_DIR/pga/phppgadmin.conf /etc/apache2/conf.d/phppgadmin.inc
		sed -i "s|%pga_alias%|$alias|g" /etc/apache2/conf.d/phppgadmin.inc

		# Restart services
		$BIN/v-restart-service apache2
	fi

	if [ -e "/etc/nginx/conf.d/phppgadmin.inc" ]; then
		rm -f /etc/nginx/conf.d/phppgadmin.inc
		cp -f $HESTIA_INSTALL_DIR/nginx/phppgadmin.inc /etc/nginx/conf.d/phppgadmin.inc
		sed -i "s|%pga_alias%|$alias|g" /etc/nginx/conf.d/phppgadmin.inc

		# Restart services
		$BIN/v-restart-service nginx
	fi
fi

#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

# Logging
$BIN/v-log-action "system" "Info" "System" "System access alias changed (Tool: $db_editor, Alias: $alias)."
log_event "$OK" "$ARGUMENTS"

exit
