#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Mediagoblin application
#
# License
# =======
#
# Copyright (C) 2016-2018 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

VARIANTS='full full-vim media'

IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=1

MEDIAGOBLIN_DOMAIN_NAME=
MEDIAGOBLIN_ADMIN_PASSWORD=
MEDIAGOBLIN_CODE=
MEDIAGOBLIN_ONION_PORT=8108
MEDIAGOBLIN_REPO="http://git.savannah.gnu.org/r/mediagoblin.git"
MEDIAGOBLIN_COMMIT='c4d3293dfa4076719e60fe9e052add07426f9a9a'
MEDIAGOBLIN_BASE_DIR=/var/www/mediagoblin.local/htdocs
MEDIAGOBLIN_PORT=6543

mediagoblin_variables=(ONION_ONLY
                       MY_USERNAME
                       MEDIAGOBLIN_DOMAIN_NAME
                       MEDIAGOBLIN_CODE
                       DEFAULT_DOMAIN_NAME
                       DDNS_PROVIDER)

function logging_on_mediagoblin {
    echo -n ''
}

function logging_off_mediagoblin {
    echo -n ''
}

function mediagoblin_fix_email {
    # This is a crude hack and there may be a better solution
    # The cause of verification problems might be that the mediagoblin user
    # does not have mail access
    read_config_param 'MEDIAGOBLIN_DOMAIN_NAME'
    MEDIAGOBLIN_BASE_DIR=/var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs
    mgfile=$MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin/tools/mail.py
    if [ ! -f $mgfile ]; then
        echo $'Unable to fix email sending'
        exit 792532
    fi
    if ! grep -q 'import os' $mgfile; then
        sed -i '/import sys/a import os' $mgfile
    fi
    sed -i "s|return mhost.sendmail(from_addr, to_addrs, message.as_string())|return os.system(\"echo '\" + message_body + \"' \| mail -s '\" + message['Subject'] + \"' \" + message['To'])|g" $mgfile
}

function install_interactive_mediagoblin {
    if [[ $ONION_ONLY != "no" ]]; then
        MEDIAGOBLIN_DOMAIN_NAME='mediagoblin.local'
        write_config_param "MEDIAGOBLIN_DOMAIN_NAME" "$MEDIAGOBLIN_DOMAIN_NAME"
    else
        function_check interactive_site_details
        interactive_site_details "mediagoblin" "MEDIAGOBLIN_DOMAIN_NAME" "MEDIAGOBLIN_CODE"
    fi
    APP_INSTALLED=1
}

function configure_interactive_mediagoblin {
    read_config_param 'MEDIAGOBLIN_DOMAIN_NAME'
    MEDIAGOBLIN_BASE_DIR=/var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs

    dialog --title $"Mediagoblin" \
           --backtitle $"Freedombone Control Panel" \
           --defaultno \
           --yesno $"\nAllow registration of new users?" 10 60
    sel=$?
    case $sel in
        0)
            sed -i 's|allow_registration.*|allow_registration = true|g' $MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin.ini
            sed -i 's|allow_registration.*|allow_registration = true|g' $MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin_local.ini
            ;;
        1)
            sed -i 's|allow_registration.*|allow_registration = false|g' $MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin.ini
            sed -i 's|allow_registration.*|allow_registration = false|g' $MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin_local.ini
            ;;
        255) return;;
    esac
    systemctl restart mediagoblin
}

function change_password_mediagoblin {
    curr_username="$1"
    new_user_password="$2"
}

function add_user_mediagoblin {
    new_username="$1"
    new_user_password="$2"

    MEDIAGOBLIN_BASE_DIR=/var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin/ && bin/gmg adduser --username $new_username --password $new_username $new_user_password" - mediagoblin
    echo "0"
}

function remove_user_mediagoblin {
    remove_username="$1"
    MEDIAGOBLIN_BASE_DIR=/var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin/ && bin/gmg deleteuser --username $new_username" - mediagoblin
    echo "0"
}

function reconfigure_mediagoblin {
    MEDIAGOBLIN_BASE_DIR=/var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs
    rm -f /var/lib/mediagoblin/*.db
    rm -rf /var/lib/mediagoblin/media/*
    rm -f $MEDIAGOBLIN_BASE_DIR/user_dev
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && ln -s /var/lib/mediagoblin user_dev" - mediagoblin
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin/ && ./bin/gmg dbupdate" - mediagoblin
}

function upgrade_mediagoblin {
    CURR_MEDIAGOBLIN_COMMIT=$(cat $COMPLETION_FILE | grep "mediagoblin commit" | awk -F ':' '{print $2}')
    if [[ "$CURR_MEDIAGOBLIN_COMMIT" != "$MEDIAGOBLIN_COMMIT" ]]; then
        MEDIAGOBLIN_BASE_DIR=/var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs
        su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin/ && git stash && git pull" - mediagoblin
        su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin/ && git checkout $MEDIAGOBLIN_COMMIT -b $MEDIAGOBLIN_COMMIT" - mediagoblin
        su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && git submodule sync" - mediagoblin
        su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && git submodule update --force --init --recursive" - mediagoblin
        su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && ./bootstrap.sh" - mediagoblin
        su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && ./configure" - mediagoblin
        su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && make" - mediagoblin
        su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin/ && ./bin/python setup.py develop --upgrade && ./bin/gmg dbupdate" - mediagoblin
        mediagoblin_fix_email
        su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin/ && ./bin/gmg dbupdate" - mediagoblin
        set_completion_param "mediagoblin commit" "$MEDIAGOBLIN_COMMIT"
    fi
    echo "0"
}

function backup_local_mediagoblin {
    MEDIAGOBLIN_BASE_DIR=/var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs
    source_directory=/var/lib/mediagoblin
    if [ -d $source_directory ]; then
        dest_directory=mediagoblin
        function_check backup_directory_to_usb
        backup_directory_to_usb $MEDIAGOBLIN_BASE_DIR $dest_directory
        backup_directory_to_usb /var/lib/mediagoblin mediagoblindata
    fi
}

function restore_local_mediagoblin {
    MEDIAGOBLIN_BASE_DIR=/var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs
    if [ -d /var/lib/mediagoblin ]; then
        echo $"Restoring Mediagoblin installation"
        function_check get_completion_param
        MEDIAGOBLIN_DOMAIN_NAME=$(get_completion_param "mediagoblin domain")
        temp_restore_dir=/root/tempmediagoblin
        function_check restore_directory_from_usb
        restore_directory_from_usb ${temp_restore_dir} mediagoblin
        if [ -d ${temp_restore_dir}$MEDIAGOBLIN_BASE_DIR ]; then
            cp -r ${temp_restore_dir}$MEDIAGOBLIN_BASE_DIR/* $MEDIAGOBLIN_BASE_DIR/
        else
            cp -r ${temp_restore_dir}/* $MEDIAGOBLIN_BASE_DIR/
        fi
        if [ ! "$?" = "0" ]; then
            function_check restore_directory_from_usb
            set_user_permissions
            function_check backup_unmount_drive
            backup_unmount_drive
            exit 45327
        fi
        restore_directory_from_usb ${temp_restore_dir}2 mediagoblindata
        if [ -d ${temp_restore_dir}2/var/lib/mediagoblin ]; then
            cp -r ${temp_restore_dir}2/var/lib/mediagoblin/* /var/lib/mediagoblin/
        else
            cp -r ${temp_restore_dir}2/* /var/lib/mediagoblin/
        fi
        if [ ! "$?" = "0" ]; then
            function_check set_user_permissions
            set_user_permissions
            function_check backup_unmount_drive
            backup_unmount_drive
            exit 24529
        fi
        rm -rf ${temp_restore_dir}
        rm -rf ${temp_restore_dir}2
        chown -hR mediagoblin:www-data $MEDIAGOBLIN_BASE_DIR
        chown -hR mediagoblin:www-data /var/lib/mediagoblin
        chmod -R g+wx /var/lib/mediagoblin
    fi
}

function backup_remote_mediagoblin {
    MEDIAGOBLIN_BASE_DIR=/var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs
    if [ -d /etc/mediagoblin ]; then
        echo $"Backing up mediagoblin"
        backup_directory_to_friend $MEDIAGOBLIN_BASE_DIR mediagoblin
        backup_directory_to_friend /var/lib/mediagoblin mediagoblindata
    fi
}

function restore_remote_mediagoblin {
    MEDIAGOBLIN_BASE_DIR=/var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs
    if [ -d $SERVER_DIRECTORY/backup/mediagoblin ]; then
        temp_restore_dir=/root/tempmediagoblin
        function_check get_completion_param
        MEDIAGOBLIN_DOMAIN_NAME=$(get_completion_param "mediagoblin domain")
        function_check restore_directory_from_friend
        restore_directory_from_friend $temp_restore_dir mediagoblin
        if [ -d ${temp_restore_dir}$MEDIAGOBLIN_BASE_DIR ]; then
            cp -r ${temp_restore_dir}$MEDIAGOBLIN_BASE_DIR/* $MEDIAGOBLIN_BASE_DIR/
        else
            cp -r ${temp_restore_dir}/* $MEDIAGOBLIN_BASE_DIR/
        fi
        if [ ! "$?" = "0" ]; then
            exit 264824
        fi
        restore_directory_from_friend ${temp_restore_dir}2 mediagoblindata
        if [ -d ${temp_restore_dir}2/var/lib/mediagoblin ]; then
            cp -r ${temp_restore_dir}2/var/lib/mediagoblin/* /var/lib/mediagoblin/
        else
            cp -r ${temp_restore_dir}2/* /var/lib/mediagoblin/
        fi
        if [ ! "$?" = "0" ]; then
            exit 268492
        fi
        rm -rf ${temp_restore_dir}
        rm -rf ${temp_restore_dir}2
        chown -hR mediagoblin:www-data $MEDIAGOBLIN_BASE_DIR
        chown -hR mediagoblin:www-data /var/lib/mediagoblin
        chmod -R g+wx /var/lib/mediagoblin
    fi
}

function remove_mediagoblin {
    if [ ${#MEDIAGOBLIN_DOMAIN_NAME} -eq 0 ]; then
        return
    fi
    MEDIAGOBLIN_BASE_DIR=/var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs
    systemctl stop mediagoblin
    systemctl disable mediagoblin
    rm /etc/systemd/system/mediagoblin.service
    systemctl daemon-reload

    function_check remove_onion_service
    remove_onion_service mediagoblin ${MEDIAGOBLIN_ONION_PORT}
    nginx_dissite $MEDIAGOBLIN_DOMAIN_NAME
    remove_certs $MEDIAGOBLIN_DOMAIN_NAME
    if [ -d /var/lib/mediagoblin ]; then
        rm -rf /var/lib/mediagoblin
    fi
    if [ -d $MEDIAGOBLIN_BASE_DIR ]; then
        rm -rf $MEDIAGOBLIN_BASE_DIR
    fi
    remove_completion_param "install_mediagoblin"
    remove_completion_param "mediagoblin domain"
    sed -i '/mediagoblin/d' $COMPLETION_FILE

    remove_nodejs mediagoblin

    groupdel -f mediagoblin
    userdel -r mediagoblin

    function_check remove_ddns_domain
    remove_ddns_domain $MEDIAGOBLIN_DOMAIN_NAME
}

function install_mediagoblin {
    if [ ! $MEDIAGOBLIN_DOMAIN_NAME ]; then
        return
    fi

    MEDIAGOBLIN_BASE_DIR=/var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs

    function_check install_nodejs
    install_nodejs mediagoblin

    apt-get install -yq python python-dev python-lxml python-imaging python-virtualenv python-setuptools
    apt-get install -yq automake perl sqlite python-paste python-pastedeploy python-pastescript
    apt-get install -yq python3 python3-dev libpython3-dev python3-lxml python3-pil
    groupadd mediagoblin
    apt-get install -yq python-service-identity python-ndg-httpsclient

    useradd -c "GNU MediaGoblin system account" -d /var/lib/mediagoblin -m -r -g www-data mediagoblin
    usermod --append -G mediagoblin mediagoblin
    mkdir -p /var/lib/mediagoblin
    mkdir -p $MEDIAGOBLIN_BASE_DIR
    chown -hR mediagoblin:www-data $MEDIAGOBLIN_BASE_DIR
    chown -hR mediagoblin:www-data /var/lib/mediagoblin
    chmod -R g+wx /var/lib/mediagoblin

    if [ -d /repos/mediagoblin ]; then
        mkdir -p $MEDIAGOBLIN_BASE_DIR/mediagoblin
        cp -r -p /repos/mediagoblin/. $MEDIAGOBLIN_BASE_DIR/mediagoblin
        cd $MEDIAGOBLIN_BASE_DIR/mediagoblin
        git pull
        chown -R mediagoblin:mediagoblin $MEDIAGOBLIN_BASE_DIR/mediagoblin
    else
        su -c "cd $MEDIAGOBLIN_BASE_DIR && git clone $MEDIAGOBLIN_REPO $MEDIAGOBLIN_BASE_DIR/mediagoblin" - mediagoblin
    fi

    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && git checkout $MEDIAGOBLIN_COMMIT -b $MEDIAGOBLIN_COMMIT" - mediagoblin
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && git submodule sync" - mediagoblin
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && git submodule update --force --init --recursive" - mediagoblin
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && ./bootstrap.sh" - mediagoblin
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && ./configure" - mediagoblin
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && make" - mediagoblin
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && bin/easy_install flup==1.0.3.dev-20110405" - mediagoblin
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && ln -s /var/lib/mediagoblin user_dev" - mediagoblin
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && bash -c 'cp -av mediagoblin.ini mediagoblin_local.ini && cp -av paste.ini paste_local.ini'" - mediagoblin
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && perl -pi -e 's|.*sql_engine = .*|sql_engine = sqlite:////var/lib/mediagoblin/mediagoblin.db|' mediagoblin_local.ini" - mediagoblin

    # Video plugin
    apt-get install -yq python-gi python3-gi \
            gstreamer1.0-tools \
            gir1.2-gstreamer-1.0 \
            gir1.2-gst-plugins-base-1.0 \
            gstreamer1.0-plugins-good \
            gstreamer1.0-plugins-bad \
            gstreamer1.0-plugins-ugly \
            gstreamer1.0-libav \
            python-gst-1.0 python3-gst-1.0
    cd $MEDIAGOBLIN_BASE_DIR/mediagoblin
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && echo '[[mediagoblin.media_types.video]]' | tee -a mediagoblin_local.ini" - mediagoblin

    # Audio plugin
    apt-get install -yq python-gst-1.0 gstreamer1.0-plugins-base gstreamer1.0-plugins-bad gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly gstreamer1.0-libav libsndfile1-dev libasound2-dev libgstreamer-plugins-base1.0-dev python-numpy python-scipy
    apt-get -yq install libasound2-dev python3-numpy python3-scipy libsndfile-dev
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && echo '[[mediagoblin.media_types.audio]]' | tee -a mediagoblin_local.ini" - mediagoblin
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin && bin/pip install scikits.audiolab" - mediagoblin

    if [ -f $IMAGE_PASSWORD_FILE ]; then
        MEDIAGOBLIN_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
    else
        if [ ! $MEDIAGOBLIN_ADMIN_PASSWORD ]; then
            MEDIAGOBLIN_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
        fi
    fi
    ${PROJECT_NAME}-pass -u $MY_USERNAME -a mediagoblin -p "$MEDIAGOBLIN_ADMIN_PASSWORD"

    MEDIAGOBLIN_ONION_HOSTNAME=$(add_onion_service mediagoblin 80 ${MEDIAGOBLIN_ONION_PORT})
    set_completion_param "mediagoblin domain" "${MEDIAGOBLIN_DOMAIN_NAME}"
    set_completion_param "mediagoblin onion domain" "$MEDIAGOBLIN_ONION_HOSTNAME"
    set_completion_param "mediagoblin commit" "$MEDIAGOBLIN_COMMIT"

    if [[ $ONION_ONLY == "no" ]]; then
        function_check nginx_http_redirect
        nginx_http_redirect $MEDIAGOBLIN_DOMAIN_NAME
        echo 'server {' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    listen 443 ssl;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    listen [::]:443 ssl;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    include /etc/nginx/mime.types;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    autoindex off;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    default_type  application/octet-stream;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    sendfile on;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        function_check nginx_ssl
        nginx_ssl $MEDIAGOBLIN_DOMAIN_NAME
        function_check nginx_disable_sniffing
        nginx_disable_sniffing $MEDIAGOBLIN_DOMAIN_NAME
        function_check nginx_limits
        nginx_limits $MEDIAGOBLIN_DOMAIN_NAME 800m
        echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    client_header_timeout 10m;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    client_body_timeout 10m;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    send_timeout 10m;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    fastcgi_read_timeout 600;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    fastcgi_send_timeout 600;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    gzip on;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    gzip_min_length 1024;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    gzip_buffers 4 32k;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    gzip_types text/plain application/x-javascript text/javascript text/xml text/css;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo "    server_name $MEDIAGOBLIN_DOMAIN_NAME;" >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    access_log /dev/null;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    error_log /dev/null;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME

        echo '    location /mgoblin_static/ {' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo "        alias $MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin/static/;" >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    }' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    location /mgoblin_media/ {' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo "        alias $MEDIAGOBLIN_BASE_DIR/mediagoblin/user_dev/media/public/;" >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    }' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    location /theme_static/ {' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo "        alias $MEDIAGOBLIN_BASE_DIR/mediagoblin/user_dev/theme_static/;" >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    }' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    location /plugin_static/ {' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo "        alias $MEDIAGOBLIN_BASE_DIR/mediagoblin/user_dev/plugin_static/;" >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    }' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    location / {' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo "        fastcgi_pass 127.0.0.1:$MEDIAGOBLIN_PORT;" >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '        include /etc/nginx/fastcgi_params;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '        fastcgi_param PATH_INFO $fastcgi_script_name;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '        fastcgi_param SCRIPT_NAME "";' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '        fastcgi_read_timeout 30;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '    }' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
        echo '}' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    else
        echo -n '' > /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    fi
    echo 'server {' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo "    listen 127.0.0.1:${MEDIAGOBLIN_ONION_PORT} default_server;" >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    include /etc/nginx/mime.types;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    autoindex off;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    default_type  application/octet-stream;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    sendfile on;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    function_check nginx_disable_sniffing
    nginx_disable_sniffing $MEDIAGOBLIN_DOMAIN_NAME
    function_check nginx_limits
    nginx_limits $MEDIAGOBLIN_DOMAIN_NAME 800m
    echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    client_header_timeout 10m;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    client_body_timeout 10m;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    send_timeout 10m;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    fastcgi_read_timeout 600;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    fastcgi_send_timeout 600;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    gzip on;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    gzip_min_length 1024;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    gzip_buffers 4 32k;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    gzip_types text/plain application/x-javascript text/javascript text/xml text/css;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo "    server_name $MEDIAGOBLIN_ONION_HOSTNAME;" >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    access_log /dev/null;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    error_log /dev/null;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME

    echo '    location /mgoblin_static/ {' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo "        alias $MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin/static/;" >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    }' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    location /mgoblin_media/ {' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo "        alias $MEDIAGOBLIN_BASE_DIR/mediagoblin/user_dev/media/public/;" >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    }' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    location /theme_static/ {' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo "        alias $MEDIAGOBLIN_BASE_DIR/mediagoblin/user_dev/theme_static/;" >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    }' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    location /plugin_static/ {' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo "        alias $MEDIAGOBLIN_BASE_DIR/mediagoblin/user_dev/plugin_static/;" >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    }' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    location / {' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo "        fastcgi_pass 127.0.0.1:$MEDIAGOBLIN_PORT;" >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '        include /etc/nginx/fastcgi_params;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '        fastcgi_param PATH_INFO $fastcgi_script_name;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '        fastcgi_param SCRIPT_NAME "";' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '        fastcgi_read_timeout 30;' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '    }' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    echo '}' >> /etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME

    echo '[Unit]' > /etc/systemd/system/mediagoblin.service
    echo 'Description=Media service' >> /etc/systemd/system/mediagoblin.service
    echo 'Requires=network.target' >> /etc/systemd/system/mediagoblin.service
    echo 'After=network.target' >> /etc/systemd/system/mediagoblin.service
    echo '' >> /etc/systemd/system/mediagoblin.service
    echo '[Service]' >> /etc/systemd/system/mediagoblin.service
    echo 'Type=simple' >> /etc/systemd/system/mediagoblin.service
    echo 'User=mediagoblin' >> /etc/systemd/system/mediagoblin.service
    echo 'Group=mediagoblin' >> /etc/systemd/system/mediagoblin.service
    echo "WorkingDirectory=$MEDIAGOBLIN_BASE_DIR/mediagoblin/" >> /etc/systemd/system/mediagoblin.service
    echo "ExecStart=$MEDIAGOBLIN_BASE_DIR/mediagoblin/lazyserver.sh --server-name=fcgi fcgi_host=127.0.0.1 fcgi_port=$MEDIAGOBLIN_PORT" >> /etc/systemd/system/mediagoblin.service
    echo "Environment=MEDIAGOBLIN_CONFIG=$MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin_local.ini CELERY_ALWAYS_EAGER=true" >> /etc/systemd/system/mediagoblin.service
    echo 'Restart=always' >> /etc/systemd/system/mediagoblin.service
    echo 'RestartSec=10' >> /etc/systemd/system/mediagoblin.service
    echo '' >> /etc/systemd/system/mediagoblin.service
    echo '[Install]' >> /etc/systemd/system/mediagoblin.service
    echo 'WantedBy=multi-user.target' >> /etc/systemd/system/mediagoblin.service
    chmod +x /etc/systemd/system/mediagoblin.service

    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin/ && bin/gmg dbupdate" - mediagoblin
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin/ && bin/gmg adduser --username $MY_USERNAME --password $MY_USERNAME $MEDIAGOBLIN_ADMIN_PASSWORD" - mediagoblin
    su -c "cd $MEDIAGOBLIN_BASE_DIR/mediagoblin/ && bin/gmg makeadmin $MY_USERNAME" - mediagoblin

    sed -i 's|email_debug_mode.*|email_debug_mode = false|g' $MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin_local.ini
    sed -i "s|email_sender_address.*|email_sender_address = \"noreply@${DEFAULT_DOMAIN_NAME}\"|g" $MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin_local.ini

    sed -i 's|allow_reporting.*|allow_reporting = false|g' $MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin_local.ini

    if ! grep -q '[[[skip_transcode]]]' $MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin_local.ini; then
        echo '[[[skip_transcode]]]' >> $MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin_local.ini
        echo 'mime_types = video/webm, video/ogg, video/mp4, audio/ogg, application/ogg, application/x-annodex' >> $MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin_local.ini
        echo 'container_formats = Matroska, Ogg, ISO MP4/M4A' >> $MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin_local.ini
        echo 'video_codecs = VP8 video, VP9 video, Theora, H.264, H.264 / AVC, MPEG-4 video' >> $MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin_local.ini
        echo 'audio_codecs = Vorbis, MPEG-4 AAC' >> $MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin_local.ini
        echo 'dimensions_match = false' >> $MEDIAGOBLIN_BASE_DIR/mediagoblin/mediagoblin_local.ini
    fi

    systemctl enable mediagoblin
    systemctl daemon-reload
    systemctl start mediagoblin

    mediagoblin_fix_email

    if [ -d /var/lib/mediagoblin/media ]; then
        chmod -R +x /var/lib/mediagoblin/media
    fi

    function_check create_site_certificate
    create_site_certificate $MEDIAGOBLIN_DOMAIN_NAME 'yes'

    function_check configure_php
    configure_php

    nginx_ensite $MEDIAGOBLIN_DOMAIN_NAME

    systemctl restart php7.0-fpm
    systemctl restart nginx

    function_check add_ddns_domain
    add_ddns_domain $MEDIAGOBLIN_DOMAIN_NAME

    chown -hR mediagoblin:www-data /var/lib/mediagoblin
    chmod -R g+wx /var/lib/mediagoblin

    APP_INSTALLED=1
}

# NOTE: deliberately no exit 0
