#!/bin/bash
FDOVER_MAJOR_MINOR=3.9
FDOVER_POINT=1
FDOVER_MAJOR_MINOR_REV=${FDOVER_MAJOR_MINOR}.${FDOVER_POINT}
MGVER_MAJOR_MINOR=2.6
MGVER_POINT=1
MGVER_MAJOR_MINOR_REV=${MGVER_MAJOR_MINOR}.${MGVER_POINT}

FDOINST=/usr/local/fdo-${FDOVER_MAJOR_MINOR_REV}
MGINST=/usr/local/mapguideopensource-${MGVER_MAJOR_MINOR_REV}

# Must have root
if [[ $EUID -ne 0 ]]; then
    echo "You must run this script with superuser privileges"
    exit 1
fi

do_uninstall()
{
    echo "[uninstall]: Stopping mgserver"
    killall mgserver

    if [ -f $MGINST/webserverextensions/apache2/bin/apachectl ]; then
        echo "[uninstall]: Stopping Apache"
        $MGINST/webserverextensions/apache2/bin/apachectl stop
    fi
    
    if [ -d $MGINST ]; then
        echo "[uninstall]: Uninstalling MapGuide"
        rm -rf $MGINST
    fi

    if [ -d $FDOINST ]; then
        echo "[uninstall]: Uninstalling FDO"
        rm -rf $FDOINST
    fi
    
    echo "Uninstall completed"
}

while true; do
    read -p 'This script will remove everything under the MapGuide and FDO installation directories. Do you wish to uninstall MapGuide? [y/n]' yn
    case $yn in
        [Yy]* ) do_uninstall; break;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac
done
