#!/bin/bash

CHRBIN=/usr/sbin/chroot
CHRD=/chroot/admin


if ! [ -x /usr/bin/makepasswd ]; then
    echo "Error: makepasswd package not installed!"
    exit 7
fi


makepass() {
	echo $(makepasswd --chars=16 | tr '[:upper:]' '[:lower:]')
}

# -----------------------------------------------------

# -----------
# sanity check

if [ "$1" != "SURE" ]; then
    echo "WARNING! This script will change the admin/xxxxx/stat url."
    echo "If you are sure, type SURE as parameter"
    echo "Usage: $0 [SURE]"
    exit 7
fi


# -----------------------------------------------------

newurl=$(makepass)
#newurl=xyxyxyxyxyx

echo "New stat URL: /$newurl/stat"

echo "Updating apache2-admin configs..."
conffile=$CHRD/etc/apache2/vhost-sys.conf
if [ -f ${conffile} ]; then
	sed -i -E "s/^(.*Alias \/)[^\/]*(\/stat.*)$/\1${newurl}\2/" $conffile
else
	echo "ERROR: $conffile doesn't exist"
	exit 3
fi

echo "Updating sys-admin menu..."
samfile=$CHRD/web/sys/admin/sysadmin/config/menu.php
if [ -f ${samfile} ]; then
	sed -i -E "s/^(.*CONFIG\[.menu.\]\[\]\=[^\/]*)\/[^\/]*\/?stat(\".*)$/\1\/${newurl}\/stat\2/" ${samfile}
else
	echo "ERROR: $samfile doesn't exist"
	exit 3
fi

/etc/init.d/apache2-admin restart
