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

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

SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"
AIRTIMEROOT=${SCRIPT_DIR}
LIBZEND_DIR='/usr/share/php/libzend-framework-php/Zend'

showhelp () {
    echo "Usage: sudo bash install [options]
    -h, --help, -?
                Display usage information
    -V, --version
                Display version information
    -v, --verbose
                More output
    -q, --quiet, --silent
                No output except errors
    -f, --force
                Turn off interactive prompts
    --distribution=DISTRIBUTION
                Linux distribution the installation is being run on
    --release=RELEASE
                Distribution release
    -d, --ignore-dependencies
                Don't install binary dependencies
    -w, --web-user=WEB_USER
                Set the nginx web user. Defaults to www-data. Only change
                this setting if you've changed the default nginx web user
    -r, --web-root=WEB_ROOT
                Set the web root for Airtime files
                This will copy the Airtime application files, but you will need
                to give your web user access to the given directory if it is
                not accessible
    -I, --in-place
                Set the current Airtime directory as the web root
                Note that you will need to give your web user permission to
                access this directory if it is not accessible
    -p, --postgres
                Create a default postgres user named 'airtime' with password
                'airtime'
    -n, --nginx
                Install nginx and deploy a basic configuration for Airtime
    -i, --icecast
                Install Icecast 2 and deploy a basic configuration for Airtime"
    exit 0
}

showversion () {
    version=$(php -r 'require_once(__DIR__ . "/airtime_mvc/application/configs/constants.php"); echo AIRTIME_CODE_VERSION;')
    echo "Airtime Version ${version}"
    exit 0
}

web_user="www-data"
web_root=""
in_place="f"
postgres="f"
nginx="f"
icecast="f"
ignore_dependencies="f"
# Interactive
_i=1
# Verbose
_v=0
# Quiet
_q=0
upgrade="f"
dist=""
code=""

function verbose() {
    if [[ ${_v} -eq 1 ]]; then
        echo -e "$@"
    fi
}

function loud() {
    if [[ ${_q} -eq 0 ]]; then
        echo -e "$@"
    fi
}

# Evaluate commands silently if quiet
function loudCmd() {
    if [[ ${_q} -eq 0 ]]; then
        eval $@
    else
        eval $@ > /dev/null
    fi
}

function checkCommandExists() {
    set +e
    command=$@
    eval hash ${command} 2>/dev/null
    commandFound=$?
    if [[ ! ${commandFound} -eq 0 ]]; then
        echo -e "Error: ${command} not found. Please ensure you have the corresponding dependency installed."
        exit
    fi
    set -e
}

while :; do
    case "$1" in
        --help)
            showhelp
            ;;
        --version)
            showversion
            ;;
        --verbose)
            _v=1
            ;;
        --quiet|--silent)
            _q=1
            ;;
        --force)
            _i=0
            ;;
        --distribution)
            if [ "$2" ]; then
                dist=$2
                shift 2
                continue
            else
                echo 'ERROR: Must specify a non-empty "--distribution DISTRIBUTION" argument.' >&2
                exit 1
            fi
            ;;
        --distribution=?*)
            dist=${1#*=} # Delete everything up to "=" and assign the remainder.
            ;;
        --distribution=)
            echo 'ERROR: Must specify a non-empty "--distribution DISTRIBUTION" argument.' >&2
            exit 1
            ;;
        --release)
            if [ "$2" ]; then
                code=$2
                shift 2
                continue
            else
                echo 'ERROR: Must specify a non-empty "--release RELEASE" argument.' >&2
                exit 1
            fi
            ;;
        --release=?*)
            code=${1#*=} # Delete everything up to "=" and assign the remainder.
            ;;
        --release=)
            echo 'ERROR: Must specify a non-empty "--release RELEASE" argument.' >&2
            exit 1
            ;;
        --ignore-dependencies)
            ignore_dependencies="t"
            ;;
        --nginx)
            nginx="t"
            ;;
        --icecast)
            icecast="t"
            ;;
        --postgres)
            postgres="t"
            ;;
        --in-place)
            in_place="t"
            ;;
        --web-user)
            if [ "$2" ]; then
                web_user=$2
                shift 2
                continue
            else
                echo 'ERROR: Must specify a non-empty "--web-user WEB_USER" argument.' >&2
                exit 1
            fi
            ;;
        --web-user=?*)
            web_user=${1#*=} # Delete everything up to "=" and assign the remainder.
            ;;
        --web-user=)
            echo 'ERROR: Must specify a non-empty "--web-user=WEB_USER" argument.' >&2
            exit 1
            ;;
        --web-root)
            if [ "$2" ]; then
                web_root=$(readlink -f $2)
                shift 2
                continue
            else
                echo 'ERROR: Must specify a non-empty "--web-root WEB_ROOT" argument.' >&2
                exit 1
            fi
            ;;
        --web-root=?*)
            web_root=${1#*=} # Delete everything up to "=" and assign the remainder.
            ;;
        --web-root=)
            echo 'ERROR: Must specify a non-empty "--web-root=WEB_ROOT" argument.' >&2
            exit 1
            ;;
        --)
            shift
            break
            ;;
        -?*)
            for ((i = 1; i < ${#1}; i++)); do
                case "${1:$i:1}" in
                    h|\?)
                        showhelp
                        ;;
                    V)
                        showversion
                        ;;
                    v)
                        _v=1
                        ;;
                    q)
                        _q=1
                        ;;
                    f)
                        _i=0
                        ;;
                    d)
                        ignore_dependencies="t"
                        ;;
                    n)
                        nginx="t"
                        ;;
                    i)
                        icecast="t"
                        ;;
                    p)
                        postgres="t"
                        ;;
                    I)
                        in_place="t"
                        ;;
                    w)
                        if [ "$2" ]; then
                            web_user=$2
                            continue
                        else
                            echo 'ERROR: Must specify a non-empty "-w WEB_USER" argument.' >&2
                            exit 1
                        fi
                        ;;
                    r)
                        if [ "$2" ]; then
                            web_root=$(readlink -f $2)
                            continue
                        else
                            echo 'ERROR: Must specify a non-empty "-d WEB_ROOT" argument.' >&2
                            exit 1
                        fi
                        ;;
                    *)
                        echo "$0: error - unrecognized option '${1:$i:1}'" >&2;
                        echo "Try 'install --help' for more information."
                        exit 1
                esac
            done
            ;;
        *)
            break
    esac
    shift
done

if [ -z web_root -a ! -d web_root ]; then
    echo "$web_root doesn't exist!"
    exit 1
fi

echo -e "\n   _____  .________________________.___   _____  ___________   "
echo    "  /  _  \ |   \______   \__    ___/|   | /     \ \_   _____/     "
echo    " /  /_\  \|   ||       _/ |    |   |   |/  \ /  \ |    __)_      "
echo    "/    |    \   ||    |   \ |    |   |   /    Y    \|        \     "
echo    "\____|__  /___||____|_  / |____|   |___\____|__  /_______  /     "
echo -e "        \/            \/                       \/        \/    \n"

IP=$(ifconfig 2>/dev/null | grep -A 1 'encap:Ethernet' | awk '/inet addr:/ {print $2}' | sed 's/addr://' | head -1)

if [ "$ignore_dependencies" = "f" ]; then
    set +e
    loudCmd "apt-get update"
    if [ -z "${dist}" ]; then
        loudCmd "apt-get -y --force-yes install lsb-release"
        dist=`lsb_release -ds | awk '{print tolower($1);}'`
        code=`lsb_release -cs`
    fi

    loud "\n-----------------------------------------------------"
    loud "         * Installing External Dependencies *        "
    loud "-----------------------------------------------------"

    verbose "\n * Reading requirements-${dist,,}-${code,,}.apt..."
    if [ -f ${SCRIPT_DIR}/installer/lib/requirements-${dist,,}-${code,,}.apt ]; then
        loudCmd "DEBIAN_FRONTEND=noninteractive apt-get -y -m install $(grep -vE '^\s*#' ${SCRIPT_DIR}/installer/lib/requirements-${dist,,}-${code,,}.apt | tr '\n' ' ')"
    else
        loudCmd "DEBIAN_FRONTEND=noninteractive apt-get -y -m install $(grep -vE '^\s*#' ${SCRIPT_DIR}/installer/lib/requirements-ubuntu-xenial.apt | tr '\n' ' ')"
    fi

    if [ -d "$LIBZEND_DIR" ]; then
        verbose "\n * Replacing deprecated functions in libzend-framework-php ..."
        find "$LIBZEND_DIR" -type f -exec sed -i "s/iconv_set_encoding('internal_encoding'/ini_set('default_charset'/g" {} +
    fi
    set -e
else
    checkCommandExists "nginx"
    checkCommandExists "rabbitmqctl"
    checkCommandExists "psql"
fi

if [ -f /etc/airtime/airtime.conf ]; then
    OLD_CONF=$(grep "[media-monitor]" /etc/airtime/airtime.conf)

    if [ -n "${OLD_CONF}" ]; then
        upgrade="t"

        set +e
        verbose "Stopping airtime services..."
        loudCmd "service airtime-playout stop-with-monit"
        loudCmd "service airtime-media-monitor stop-with-monit"
        loudCmd "service airtime-liquidsoap stop-with-monit"
        verbose "...Done"

        echo "Looks like you have an old version of Airtime. Your current /etc/airtime/airtime.conf \
will be moved to /etc/airtime/airtime.conf.tmp"
        # If we don't remove the existing python files in /usr/lib and the
        # /etc/init.d startup scripts, services won't work properly
        if [ -d /usr/lib/airtime/ ]; then
            rm -rf /usr/lib/airtime/
        fi

        rm /etc/init.d/airtime-*

        if [ "$nginx" = "t" ]; then
            # If the user selects an "in-place" install or passes in a web root,
            # we need to replace the old nginx airtime.conf
            rm /etc/nginx/sites-available/airtime.conf
        fi

        if [ -d /usr/share/airtime -a web_root = /usr/share/airtime ]; then
            rm -rf /usr/share/airtime
        fi

        mv /etc/airtime/airtime.conf /etc/airtime/airtime.conf.tmp
        set -e
    fi
fi

if [ "$nginx" = "f" -a ${_i} -eq 1 ]; then
    echo -e "Install default Airtime nginx configuration? (Y/n): \c"
    read IN
    if [ "$IN" = "y" -o "$IN" = "Y" ]; then
        nginx="t"
    fi
fi

if [ "$in_place" = "t" ]; then
    verbose "\n * Setting current Airtime directory as web root..."
    web_root=${AIRTIMEROOT}/airtime_mvc/public
elif [ -n "$web_root" ]; then
    verbose "\n * Creating nginx web root directory..."
    cp -R ${AIRTIMEROOT}/airtime_mvc/* ${web_root}
    web_root=${web_root}/public/
else
    verbose "\n * Creating default nginx web root directory /usr/share/airtime/..."
    web_root="/usr/share/airtime"
    mkdir -p ${web_root}
    cp -R ${AIRTIMEROOT}/airtime_mvc/* ${web_root}
    web_root=${web_root}/public/
fi
verbose "...Done"

if [ "$nginx" = "t" ]; then
    loud "\n-----------------------------------------------------"
    loud "                * Configuring nginx *                "
    loud "-----------------------------------------------------"

    airtimeconfigfile='airtime.conf'

    # If we're upgrading (installing over an existing Airtime install) and we've been told to
    # install nginx, we should overwrite any existing configuration. If we don't do this, doing
    # an in-place installation over an old Airtime install (which installs to /usr/share by default)
    # will fail
    if [ "$upgrade" = "t" -o ! -f /etc/nginx/sites-available/${airtimeconfigfile} ]; then
        verbose "\n * Creating nginx config for Airtime..."
        if [ ${_i} -eq 1 ]; then
            echo -e "Please enter server name: \c"
            read server_name
        fi
        if [ -z "$server_name" ]; then
            server_name="$IP"
        fi
        sed -e "s@WEB_ROOT@${web_root}@g;s@SERVER_NAME@${server_name}@g" ${SCRIPT_DIR}/installer/nginx/airtime.conf > /etc/nginx/sites-available/${airtimeconfigfile}
        if [ ! -f "/etc/nginx/sites-enabled/${airtimeconfigfile}" ]; then
            ln -s /etc/nginx/sites-available/${airtimeconfigfile} /etc/nginx/sites-enabled/${airtimeconfigfile}
        fi
    else
        verbose "\nnginx config for Airtime already exists, skipping"
    fi
else
    server_name="$IP"
fi

if [ "$icecast" = "f" -a ${_i} -eq 1 ]; then
    echo -e "Install default Airtime Icecast configuration? (Y/n): \c"
    read IN
    if [ "$IN" = "y" -o "$IN" = "Y" ]; then
        icecast="t"
    fi
fi

if [ "$icecast" = "t" ]; then
    loud "\n-----------------------------------------------------"
    loud "               * Configuring Icecast *                "
    loud "-----------------------------------------------------"

    verbose "\n * Enabling Icecast 2..."
    sed -i 's/ENABLE=false/ENABLE=true/g' /etc/default/icecast2
    set +e
    loudCmd "service icecast2 start"
    set -e
    verbose "...Done"
fi

loud "\n-----------------------------------------------------"
loud "          * Installing Airtime Services *            "
loud "-----------------------------------------------------"

verbose "\n * Installing necessary python services..."
loudCmd "pip install setuptools"
verbose "...Done"

verbose "\n * Creating /run/airtime..."
mkdir -p /run/airtime
chmod 755 /run/airtime
chown -R ${web_user}:${web_user} /run/airtime
verbose "...Done"

verbose "\n * Installing log writer..."
loudCmd "python ${AIRTIMEROOT}/python_apps/std_err_override/setup.py install --install-scripts=/usr/bin"
verbose "...Done"

verbose "\n * Installing API client..."
loudCmd "python ${AIRTIMEROOT}/python_apps/api_clients/setup.py install --install-scripts=/usr/bin"
verbose "...Done"

verbose "\n * Installing media-monitor..."
loudCmd "python ${AIRTIMEROOT}/python_apps/media-monitor/setup.py install --install-scripts=/usr/bin"
verbose "...Done"

verbose "\n * Installing pypo..."
loudCmd "python ${AIRTIMEROOT}/python_apps/pypo/setup.py install --install-scripts=/usr/bin"
verbose "...Done"

for i in /etc/init/airtime*.template; do
    chmod 644 $i
    sed -i "s/WEB_USER/${web_user}/g" $i
    mv $i ${i%.template}
done

cp "${SCRIPT_DIR}/utils/airtime-import/airtime-import" /usr/bin/airtime-import
chmod +x /usr/bin/airtime-import

set +e
loudCmd "systemctl daemon-reload"
loudCmd "systemctl enable airtime-playout" # Start at bootup
loudCmd "systemctl enable airtime-media-monitor" # Start at bootup
loudCmd "systemctl enable airtime-liquidsoap" # Start at bootup
set -e

if [ ! -d /var/log/airtime ]; then
    loud "\n-----------------------------------------------------"
    loud "              * Installing Log Files *               "
    loud "-----------------------------------------------------"

    verbose "\n * Creating /var/tmp/airtime..."
    mkdir -p /var/tmp/airtime/show-recorder/

    verbose "\n * Copying logrotate files..."
    cp ${AIRTIMEROOT}/airtime_mvc/build/airtime-php.logrotate /etc/logrotate.d/airtime-php
    cp ${AIRTIMEROOT}/python_apps/pypo/pypo/liquidsoap_scripts/airtime-liquidsoap.logrotate /etc/logrotate.d/airtime-liquidsoap
fi

verbose "\n * Setting permissions on /var/log/airtime..."
chmod -R a+x /var/log/airtime
chown -R ${web_user}:${web_user} /var/log/airtime/

verbose "\n * Setting permissions on /var/tmp/airtime..."
chmod -R a+x /var/tmp/airtime
chown -R ${web_user}:${web_user} /var/tmp/airtime/

loud "\n-----------------------------------------------------"
loud "              * Configuring PostgreSQL *              "
loud "-----------------------------------------------------"

# Ensure postgres is running - It isn't after you install the postgres package on Ubuntu 15.04
loudCmd "service postgresql start"

setupAirtimePostgresUser() {
    POSTGRES_USER="$1"
    if [ -z "$POSTGRES_USER" ]; then
        POSTGRES_USER=airtime
    fi
    POSTGRES_PASSWORD="$2"
    if [ -z "$POSTGRES_PASSWORD" ]; then
        POSTGRES_PASSWORD=airtime
    fi
    # here-doc to execute this block as postgres user
    su postgres <<EOF
    set +e
    psql -d postgres -tAc "CREATE USER $POSTGRES_USER WITH ENCRYPTED PASSWORD '$POSTGRES_PASSWORD'; ALTER USER $POSTGRES_USER CREATEDB;"
    set -e
# don't indent this!
EOF
}

if [ "$postgres" = "t" ]; then
    echo -e "Enter username for postgres Airtime database: \c"
    read POSTGRES_USER
    echo -e "Enter password for postgres Airtime database: \c"
    read -s POSTGRES_PASSWORD
    setupAirtimePostgresUser "$POSTGRES_USER" "$POSTGRES_PASSWORD"
    unset POSTGRES_USER POSTGRES_PASSWORD
elif [ ${_i} -eq 1 ]; then
    echo -e "Create default airtime postgres user? (Y/n): \c"
    read IN
    if [ "$IN" = "y" -o "$IN" = "Y" ]; then
        setupAirtimePostgresUser
    fi
fi

loud "\n-----------------------------------------------------"
loud "               * Configuring RabbitMQ *               "
loud "-----------------------------------------------------"

RABBITMQ_VHOST=/airtime
EXCHANGES="airtime-pypo|pypo-fetch|airtime-media-monitor|media-monitor"

# Ignore errors in this check to avoid dying when vhost isn't found
set +e
rabbitmqctl list_vhosts | grep -w ${RABBITMQ_VHOST} > /dev/null
RESULT="$?"
set -e

# Only run these if the vhost doesn't exist
if [ "$RESULT" != "0" ]; then
    echo -e "Enter RabbitMQ Airtime virtual host username: \c"
    read RABBITMQ_USER
    if [ -z "$RABBITMQ_USER" ]; then
        RABBITMQ_USER=airtime
    fi
    echo -e "Enter RabbitMQ Airtime virtual host password: \c"
    read -s RABBITMQ_PASSWORD
    if [ -z "$RABBITMQ_PASSWORD" ]; then
        RABBITMQ_PASSWORD=airtime
    fi

    verbose "\n * Creating RabbitMQ virtual host and user ..."
    rabbitmqctl add_vhost ${RABBITMQ_VHOST}
    rabbitmqctl add_user ${RABBITMQ_USER} ${RABBITMQ_PASSWORD}
    verbose "\n * Setting RabbitMQ user permissions..."
    loudCmd "rabbitmqctl set_permissions -p ${RABBITMQ_VHOST} ${RABBITMQ_USER} \"$EXCHANGES\" \"$EXCHANGES\" \"$EXCHANGES\""
    unset RABBITMQ_VHOST RABBITMQ_USER RABBITMQ_PASSWORD EXCHANGES
else
    verbose "\nRabbitMQ user already exists, skipping creation"
fi

if [ ! -d "/etc/airtime" ]; then
    loud "\n-----------------------------------------------------"
    loud "                * Installing Airtime *               "
    loud "-----------------------------------------------------"

    verbose "\n * Creating /etc/airtime/ directory..."
    mkdir -p /etc/airtime
fi
chown -R ${web_user}:${web_user} /etc/airtime

if [ ! -d "/srv/airtime" ]; then
    mkdir -p /srv/airtime
fi
chown -R ${web_user}:${web_user} /srv/airtime


# We only generate the locales for Airtime if you're allowing us
# to install our dependencies, so that we won't automatically do this
# when this install script runs from our DEB package.
if [ "$ignore_dependencies" = "f" ]; then
    loud "\n-----------------------------------------------------"
    loud "                * Installing Locales *               "
    loud "-----------------------------------------------------"

    set +e
    verbose "\n * Generating locales"
    for i in `ls ${web_root}/../locale | grep ".._.."`; do
        if [ "$dist" = "debian" ]; then
            grep -qi "^$i" /etc/locale.gen
            if [ $? -ne 0 ]; then
                verbose "$i.UTF-8 UTF-8" >> /etc/locale.gen
            fi
        else
            loudCmd "locale-gen \"$i.utf8\""
        fi
    done
    set -e

    if [ "$dist" = "debian" ]; then
        loudCmd "/usr/sbin/locale-gen"
    fi
fi

verbose "\n * Reloading nginx..."
loudCmd "service nginx reload 2>/dev/null"

echo -e "\n-----------------------------------------------------"
echo    "                * Basic Setup DONE! *                "
echo    "                                                     "
echo    " To get started with Airtime, visit ${server_name}   "
echo    " or, if you've set up your own web configuration,    "
echo    " the Airtime webroot on your webserver               "
echo    "-----------------------------------------------------"
