#!/bin/bash -e
# -e Causes bash script to exit if any of the steps
# return with a non-zero return value.

if [[ $EUID -ne 0 ]]; then
    echo "Please run as root user."
    exit 1
fi

getStorDirFromDatabase() {
    DATABASE_NAME="$1"
    # here-doc to execute this block as postgres user
    result=`su postgres <<EOF
    set +e
    echo $(psql -d ${DATABASE_NAME} -tAc "SELECT directory FROM cc_music_dirs WHERE type='stor'")
    set -e
# don't indent this!
EOF`
    echo $result
}

dropAirtimeDatabase() {
    DATABASE_NAME="$1"
    DATABASE_USER="$2"
    # here-doc to execute this block as postgres user
    su postgres <<'EOF'
    set +e
    # DROP DATABASE cannot be executed from a function or multi-command string
    psql -d postgres -tAc "DROP DATABASE IF EXISTS ${DATABASE_NAME}"
    psql -d postgres -tAc "DROP USER IF EXISTS ${DATABASE_USER}"
    set -e
# don't indent this!
EOF
}

removeRabbitmqAirtimeSettings() {
    RMQ_VHOST="$1"
    RMQ_USER="$2"
    rabbitmqctl delete_vhost ${RMQ_VHOST}
    rabbitmqctl delete_user ${RMQ_USER}
}

SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"
AIRTIMEROOT=${SCRIPT_DIR}

RMQ_VHOST='/airtime'
RMQ_USER='airtime'
POSTGRES_DB='airtime'
POSTGRES_USER='airtime'
POSTGRES_PASSWORD='airtime'
if [ -f '/etc/airtime/airtime.conf' ]; then
    RMQ_VHOST=$(awk -F ' = ' '{if (! ($0 ~ /^;/) && $0 ~ /^vhost/ ) print $2}' /etc/airtime/airtime.conf)
    RMQ_USER=$(awk -F ' = ' '{if (! ($0 ~ /^;/) && $0 ~ /^user/ ) print $2}' /etc/airtime/airtime.conf)
    POSTGRES_DB=$(awk -F ' = ' '{if (! ($0 ~ /^;/) && $0 ~ /^dbname/ ) print $2}' /etc/airtime/airtime.conf)
    POSTGRES_USER=$(awk -F ' = ' '{if (! ($0 ~ /^;/) && $0 ~ /^dbuser/ ) print $2}' /etc/airtime/airtime.conf)
    POSTGRES_PASSWORD=$(awk -F ' = ' '{if (! ($0 ~ /^;/) && $0 ~ /^dbpass/ ) print $2}' /etc/airtime/airtime.conf)
fi

STOR_DIR=$(getStorDirFromDatabase "$POSTGRES_DB")

FILES=(
     "/etc/airtime"
     "/var/log/airtime"
     "/usr/lib/airtime"
     "/usr/share/airtime"
     "/etc/init/airtime*"
     "/usr/local/bin/airtime-*"
     "/usr/bin/airtime*"
     "/etc/nginx/sites-available/airtime*"
     "/etc/nginx/sites-enabled/airtime*"
    )

echo -e "The following files, directories, and services will be removed:\n"
for i in ${FILES[*]}; do
    echo $i
done
echo "pip airtime-playout"
echo "pip airtime-media-monitor"

echo -e "\nIf your web root is not listed, you will need to manually remove it."

echo -e "\nThis will *permanently* remove Airtime and all related files from your computer. \
Any files in Airtime directories and subdirectories will be deleted. Are you sure you want to proceed? [y/N]: \c"
read IN
if [[ ! ( "$IN" = "y" || "$IN" = "Y" ) ]]; then
    exit 0
fi

if [ -n "${STOR_DIR}" ]; then
    echo -e "\nDo you want to remove your music storage directory ${STOR_DIR} and all of its subdirectories? [y/N]: \c"
    read IN
    if [[ ( "$IN" = "y" || "$IN" = "Y" ) ]]; then
        rm -rf "${STOR_DIR}"
    fi
else
    echo -e "\nNo stor directory found, skipping..."
fi

echo -e "\nUninstalling Airtime..."
removeRabbitmqAirtimeSettings "$RMQ_VHOST" "$RMQ_USER"

for i in ${FILES[*]}; do
    rm -rf $i
done

echo -e "\nDo you want to drop your current Airtime database? [y/N]: \c"
read IN
if [[ "$IN" = "y" || "$IN" = "Y" ]]; then
    echo -e "\nDropping Airtime database..."
    dropAirtimeDatabase "$POSTGRES_DB" "$POSTGRES_USER"
fi

pip uninstall -y airtime-playout airtime-media-monitor
service nginx restart
echo "...Done"
