#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# mediagoblin functions
#
# License
# =======
#
# Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
#
# 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=''

MEDIAGOBLIN_DOMAIN_NAME=
MEDIAGOBLIN_CODE=
MEDIAGOBLIN_REPO="git://git.savannah.gnu.org/mediagoblin.git"
MEDIAGOBLIN_COMMIT='d1ac2d52fd8859c3f32fa38e4836ffe9615e5bba'
MEDIAGOBLIN_ADMIN_PASSWORD=
MEDIAGOBLIN_ONION_PORT=8096

mediagoblin_variables=(ONION_ONLY
                       MEDIAGOBLIN_DOMAIN_NAME
                       MEDIAGOBLIN_CODE
                       MEDIAGOBLIN_REPO
                       MEDIAGOBLIN_COMMIT
                       DDNS_PROVIDER)

function install_interactive_mediagoblin {
    if [[ $ONION_ONLY != "no" ]]; then
        MEDIAGOBLIN_DOMAIN_NAME='media.local'
    else
        MEDIAGOBLIN_DETAILS_COMPLETE=
        while [ ! $MEDIAGOBLIN_DETAILS_COMPLETE ]
        do
            data=$(tempfile 2>/dev/null)
            trap "rm -f $data" 0 1 2 5 15
            if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
                dialog --backtitle $"Freedombone Configuration" \
                       --title $"Mediagoblin Configuration" \
                       --form $"\nPlease enter your Mediagoblin details, or just select Ok if you don't need a Mediagoblin site:" 11 55 2 \
                       $"Domain:" 1 1 "$(grep 'MEDIAGOBLIN_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 16 33 40 \
                       $"Code:" 2 1 "$(grep 'MEDIAGOBLIN_CODE' temp.cfg | awk -F '=' '{print $2}')" 2 16 33 255 \
                       2> $data
            else
                dialog --backtitle $"Freedombone Configuration" \
                       --title $"Mediagoblin Configuration" \
                       --form $"\nPlease enter your Mediagoblin details, or just select Ok if you don't need a Mediagoblin site:" 11 55 2 \
                       $"Domain:" 1 1 "$(grep 'MEDIAGOBLIN_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 16 33 40 \
                       2> $data
            fi
            sel=$?
            case $sel in
                1) exit 1;;
                255) exit 1;;
            esac
            MEDIAGOBLIN_DOMAIN_NAME=$(cat $data | sed -n 1p)
            if [ $MEDIAGOBLIN_DOMAIN_NAME ]; then
                TEST_DOMAIN_NAME=$MEDIAGOBLIN_DOMAIN_NAME
                validate_domain_name
                if [[ $TEST_DOMAIN_NAME != $MEDIAGOBLIN_DOMAIN_NAME ]]; then
                    MEDIAGOBLIN_DOMAIN_NAME='invalid'
                    dialog --title $"Domain name validation" --msgbox "$TEST_DOMAIN_NAME" 15 50
                else
                    if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
                        MEDIAGOBLIN_CODE=$(cat $data | sed -n 2p)
                        validate_freedns_code "$MEDIAGOBLIN_CODE"
                        if [ ! $VALID_CODE ]; then
                            MEDIAGOBLIN_DOMAIN_NAME='invalid'
                        fi
                    fi
                fi
            fi
            if [ $MEDIAGOBLIN_DOMAIN_NAME ]; then
                if [[ $MEDIAGOBLIN_DOMAIN_NAME == 'invalid' ]]; then
                    MEDIAGOBLIN_DOMAIN_NAME=
                else
                    MEDIAGOBLIN_DETAILS_COMPLETE="yes"
                fi
            fi
        done

        # save the results in the config file
        write_config_param "MEDIAGOBLIN_CODE" "$MEDIAGOBLIN_CODE"
    fi
    write_config_param "MEDIAGOBLIN_DOMAIN_NAME" "$MEDIAGOBLIN_DOMAIN_NAME"
    APP_INSTALLED=1
}

function change_password_mediagoblin {
    echo -n ''
}

function reconfigure_mediagoblin {
    echo -n ''
}

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

    MEDIAGOBLIN_WORKING_DIRECTORY=/var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs

    # update to a new commit if needed
    function_check set_repo_commit
    set_repo_commit $MEDIAGOBLIN_WORKING_DIRECTORY "Mediagoblin commit" "$MEDIAGOBLIN_COMMIT" $MEDIAGOBLIN_REPO
    if [ -d $MEDIAGOBLIN_WORKING_DIRECTORY ]; then
        chown -hR mediagoblin:www-data $MEDIAGOBLIN_WORKING_DIRECTORY
    fi
}

function backup_local_mediagoblin {
    echo -n ''
}

function restore_local_mediagoblin {
    echo -n ''
}

function backup_remote_mediagoblin {
    function_check suspend_site
    suspend_site ${MEDIAGOBLIN_DOMAIN_NAME}

    echo $"Backing up Mediagoblin"

    backup_directory_to_friend /var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs mediagoblin

    function_check restart_site
    restart_site

    echo $"Backup of Mediagoblin complete"
}

function restore_remote_mediagoblin {
    if [ -d $SERVER_DIRECTORY/backup/mediagoblin ]; then
        echo $"Restoring Mediagoblin installation"
        temp_restore_dir=/root/tempmediagoblin
        function_check restore_directory_from_friend
        restore_directory_from_friend $temp_restore_dir mediagoblin
        cp -r $temp_restore_dir/* /
        if [ ! "$?" = "0" ]; then
            exit 5626
        fi
        rm -rf $temp_restore_dir
        echo $"Restore of Mediagoblin complete"
    fi
    chown -hR mediagoblin:www-data /var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs
}

function remove_mediagoblin {
    echo -n ''
}

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

    MEDIAGOBLIN_WORKING_DIRECTORY=/var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs

    # update to a new commit if needed
    function_check set_repo_commit
    set_repo_commit $MEDIAGOBLIN_WORKING_DIRECTORY "Mediagoblin commit" "$MEDIAGOBLIN_COMMIT" $MEDIAGOBLIN_REPO
    if [ -d $MEDIAGOBLIN_WORKING_DIRECTORY ]; then
        chown -hR mediagoblin:www-data $MEDIAGOBLIN_WORKING_DIRECTORY
    fi

    apt-get -y install git-core python python-dev python-lxml python-pil
    apt-get -y install python-virtualenv npm nodejs-legacy automake
    apt-get -y install fcgiwrap

    #apt-get -y install postgresql postgresql-client python-psycopg2
    #apt-get -y install python-gst-1.0 libjpeg62-turbo-dev gstreamer1.0-plugins-base python-gobject
    #apt-get -y install gstreamer1.0-plugins-good gstreamer1.0-libav libav-tools gstreamer0.10-tools
    #apt-get -y install python-numpy python-scipy libsndfile1-dev python-gst0.10-dev
    #apt-get -y install gstreamer0.10-plugins-base gstreamer0.10-plugins-good gstreamer1.0-tools
    #su -c "createuser -A -D mediagoblin" - postgres
    #su -c "createdb -E UNICODE -O mediagoblin mediagoblin" - postgres

    useradd -c "GNU MediaGoblin system account" -d /var/lib/mediagoblin -m -r -g www-data mediagoblin
    groupadd mediagoblin
    usermod --append -G mediagoblin mediagoblin

    if [ ! -d $MEDIAGOBLIN_WORKING_DIRECTORY ]; then
        mkdir -p $MEDIAGOBLIN_WORKING_DIRECTORY
    fi

    function_check git_clone
    git_clone $MEDIAGOBLIN_REPO $MEDIAGOBLIN_WORKING_DIRECTORY
    cd $MEDIAGOBLIN_WORKING_DIRECTORY
    git checkout $MEDIAGOBLIN_COMMIT -b $MEDIAGOBLIN_COMMIT
    git submodule init
    git submodule update

    if ! grep -q "Mediagoblin commit" $COMPLETION_FILE; then
        echo "Mediagoblin commit:$MEDIAGOBLIN_COMMIT" >> $COMPLETION_FILE
    else
        sed -i "s/Mediagoblin commit.*/Mediagoblin commit:$MEDIAGOBLIN_COMMIT/g" $COMPLETION_FILE
    fi

    chown -hR mediagoblin:www-data $MEDIAGOBLIN_WORKING_DIRECTORY
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bootstrap.sh" - mediagoblin
    if [ ! "$?" = "0" ]; then
        exit 278826
    fi

    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./configure" - mediagoblin
    if [ ! "$?" = "0" ]; then
        exit 462826
    fi

    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && make" - mediagoblin
    if [ ! "$?" = "0" ]; then
        exit 738229
    fi

    if [ ! -d $MEDIAGOBLIN_WORKING_DIRECTORY/user_dev ]; then
        mkdir $MEDIAGOBLIN_WORKING_DIRECTORY/user_dev
    fi
    chmod 750 $MEDIAGOBLIN_WORKING_DIRECTORY/user_dev
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install flup==1.0.3.dev-20110405" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade billiard" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade Paste" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade amqp" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade anyjson" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade py-bcrypt" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade wtforms" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade python-dateutil" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade alembic" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade waitress" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade imagesize" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade alabaster" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade snowballstemmer" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade docutils" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade Pygments" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade beautifulsoup4" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade WebOb" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade py" - mediagoblin
    su -c "cd $MEDIAGOBLIN_WORKING_DIRECTORY && ./bin/easy_install --upgrade execnet" - mediagoblin

    # create some directories
    mkdir /var/log/mediagoblin
    chown -hR mediagoblin:www-data /var/log/mediagoblin
    mkdir /var/run/mediagoblin
    chown -hR mediagoblin:www-data /var/run/mediagoblin

    if [ ! -f $MEDIAGOBLIN_WORKING_DIRECTORY/mediagoblin.ini ]; then
        echo $'mediagoblin.ini not found'
        exit 737529
    fi
    if [ ! -f $MEDIAGOBLIN_WORKING_DIRECTORY/paste.ini ]; then
        echo $'paste.ini not found'
        exit 52762
    fi

    cp -av mediagoblin.ini mediagoblin_local.ini
    cp -av paste.ini paste_local.ini

    chown -hR mediagoblin:www-data $MEDIAGOBLIN_WORKING_DIRECTORY

    # init with systemd
    echo '[Unit]' > /etc/systemd/system/mediagoblin-celeryd.service
    echo 'Description=Mediagoblin Celeryd' >> /etc/systemd/system/mediagoblin-celeryd.service
    echo 'After=syslog.target' >> /etc/systemd/system/mediagoblin-celeryd.service
    echo 'After=network.target' >> /etc/systemd/system/mediagoblin-celeryd.service
    echo '' >> /etc/systemd/system/mediagoblin-celeryd.service
    echo '[Service]' >> /etc/systemd/system/mediagoblin-celeryd.service
    echo 'User=mediagoblin' >> /etc/systemd/system/mediagoblin-celeryd.service
    echo 'Group=mediagoblin' >> /etc/systemd/system/mediagoblin-celeryd.service
    echo 'Type=simple' >> /etc/systemd/system/mediagoblin-celeryd.service
    echo "WorkingDirectory=$MEDIAGOBLIN_WORKING_DIRECTORY" >> /etc/systemd/system/mediagoblin-celeryd.service
    echo "Environment=MEDIAGOBLIN_CONFIG=$MEDIAGOBLIN_WORKING_DIRECTORY/mediagoblin_local.ini \\" >> /etc/systemd/system/mediagoblin-celeryd.service
    echo '    CELERY_CONFIG_MODULE=mediagoblin.init.celery.from_celery' >> /etc/systemd/system/mediagoblin-celeryd.service
    echo "ExecStart=$MEDIAGOBLIN_WORKING_DIRECTORY/bin/celery worker \\" >> /etc/systemd/system/mediagoblin-celeryd.service
    echo '    --logfile=/var/log/mediagoblin/celery.log \' >> /etc/systemd/system/mediagoblin-celeryd.service
    echo '    --loglevel=INFO' >> /etc/systemd/system/mediagoblin-celeryd.service
    echo 'PIDFile=/var/run/mediagoblin/mediagoblin-celeryd.pid' >> /etc/systemd/system/mediagoblin-celeryd.service
    echo '' >> /etc/systemd/system/mediagoblin-celeryd.service
    echo '[Install]' >> /etc/systemd/system/mediagoblin-celeryd.service
    echo 'WantedBy=multi-user.target' >> /etc/systemd/system/mediagoblin-celeryd.service

    echo '[Unit]' > /etc/systemd/system/mediagoblin-paster.service
    echo 'Description=Mediagoblin' >> /etc/systemd/system/mediagoblin-paster.service
    echo 'After=syslog.target' >> /etc/systemd/system/mediagoblin-paster.service
    echo 'After=network.target' >> /etc/systemd/system/mediagoblin-paster.service
    echo '' >> /etc/systemd/system/mediagoblin-paster.service
    echo '[Service]' >> /etc/systemd/system/mediagoblin-paster.service
    echo 'Type=forking' >> /etc/systemd/system/mediagoblin-paster.service
    echo 'User=mediagoblin' >> /etc/systemd/system/mediagoblin-paster.service
    echo 'Group=mediagoblin' >> /etc/systemd/system/mediagoblin-paster.service
    echo 'Environment=CELERY_ALWAYS_EAGER=false' >> /etc/systemd/system/mediagoblin-paster.service
    echo "WorkingDirectory=$MEDIAGOBLIN_WORKING_DIRECTORY" >> /etc/systemd/system/mediagoblin-paster.service
    echo "ExecStart=$MEDIAGOBLIN_WORKING_DIRECTORY/bin/paster serve \\" >> /etc/systemd/system/mediagoblin-paster.service
    echo "    $MEDIAGOBLIN_WORKING_DIRECTORY/paste_local.ini \\" >> /etc/systemd/system/mediagoblin-paster.service
    echo '    --pid-file=/var/run/mediagoblin/mediagoblin.pid \' >> /etc/systemd/system/mediagoblin-paster.service
    echo '    --log-file=/var/log/mediagoblin/mediagoblin.log \' >> /etc/systemd/system/mediagoblin-paster.service
    echo '    --daemon \' >> /etc/systemd/system/mediagoblin-paster.service
    echo '    --server-name=fcgi fcgi_host=127.0.0.1 fcgi_port=26543' >> /etc/systemd/system/mediagoblin-paster.service
    echo "ExecStop=$MEDIAGOBLIN_WORKING_DIRECTORY/bin/paster serve \\" >> /etc/systemd/system/mediagoblin-paster.service
    echo '    --pid-file=/var/run/mediagoblin/mediagoblin.pid \' >> /etc/systemd/system/mediagoblin-paster.service
    echo "    $MEDIAGOBLIN_WORKING_DIRECTORY/paste_local.ini stop" >> /etc/systemd/system/mediagoblin-paster.service
    echo 'PIDFile=/var/run/mediagoblin/mediagoblin.pid' >> /etc/systemd/system/mediagoblin-paster.service
    echo '' >> /etc/systemd/system/mediagoblin-paster.service
    echo '[Install]' >> /etc/systemd/system/mediagoblin-paster.service
    echo 'WantedBy=multi-user.target' >> /etc/systemd/system/mediagoblin-paster.service
    systemctl daemon-reload
    systemctl enable mediagoblin-celeryd
    systemctl enable mediagoblin-paster
    systemctl daemon-reload
    systemctl start mediagoblin-celeryd
    systemctl start mediagoblin-paster

    MEDIAGOBLIN_ONION_HOSTNAME=$(add_onion_service mediagoblin 80 ${MEDIAGOBLIN_ONION_PORT})
    if [[ $MEDIAGOBLIN_ONION_HOSTNAME == *"not found"* ]]; then
        echo $'Problem creating onion address for mediagoblin'
        exit 672652
    fi

    # web config
    MEDIAGOBLIN_VIRTUAL_HOST=/etc/nginx/sites-available/$MEDIAGOBLIN_DOMAIN_NAME
    function_check nginx_http_redirect
    nginx_http_redirect $MEDIAGOBLIN_DOMAIN_NAME
    if [[ $ONION_ONLY == 'no' ]]; then
        echo 'server {' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  listen 443 ssl;' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  #################################################' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  # Stock useful config options, but ignore them :)' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  #################################################' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  include /etc/nginx/mime.types;' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  autoindex off;' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  default_type  application/octet-stream;' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  sendfile on;' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
        function_check nginx_ssl
        nginx_ssl $MEDIAGOBLIN_DOMAIN_NAME
        function_check nginx_disable_sniffing
        nginx_disable_sniffing $MEDIAGOBLIN_DOMAIN_NAME
        echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  # Gzip' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  gzip on;' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  gzip_min_length 1024;' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  gzip_buffers 4 32k;' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  gzip_types text/plain application/x-javascript text/javascript text/xml text/css;' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  #####################################' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  # Mounting MediaGoblin stuff' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  # This is the section you should read' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  #####################################' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo "  server_name $MEDIAGOBLIN_DOMAIN_NAME;" >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  access_log /var/log/nginx/mediagoblin.log;' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  error_log /var/log/nginx/mediagoblin.error.log;' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  # MediaGoblins stock static files: CSS, JS, etc.' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  location /mgoblin_static/ {' >> $MEDIAGOBLIN_VIRTUAL_HOST
        function_check nginx_limits
        nginx_limits $MEDIAGOBLIN_DOMAIN_NAME '20m'
        echo "    alias $MEDIAGOBLIN_WORKING_DIRECTORY/mediagoblin/static/;" >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  }' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  # Instance specific media:' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  location /mgoblin_media/ {' >> $MEDIAGOBLIN_VIRTUAL_HOST
        function_check nginx_limits
        nginx_limits $MEDIAGOBLIN_DOMAIN_NAME '20m'
        echo "    alias $MEDIAGOBLIN_WORKING_DIRECTORY/user_dev/media/public/;" >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  }' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  # Theme static files (usually symlinked in)' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  location /theme_static/ {' >> $MEDIAGOBLIN_VIRTUAL_HOST
        function_check nginx_limits
        nginx_limits $MEDIAGOBLIN_DOMAIN_NAME '20m'
        echo "    alias $MEDIAGOBLIN_WORKING_DIRECTORY/user_dev/theme_static/;" >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  }' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  # Plugin static files (usually symlinked in)' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  location /plugin_static/ {' >> $MEDIAGOBLIN_VIRTUAL_HOST
        function_check nginx_limits
        nginx_limits $MEDIAGOBLIN_DOMAIN_NAME '20m'
        echo "    alias $MEDIAGOBLIN_WORKING_DIRECTORY/user_dev/plugin_static/;" >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  }' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  # Mounting MediaGoblin itself via FastCGI.' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  location / {' >> $MEDIAGOBLIN_VIRTUAL_HOST
        function_check nginx_limits
        nginx_limits $MEDIAGOBLIN_DOMAIN_NAME '20m'
        echo '    fastcgi_pass 127.0.0.1:26543;' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '    include /etc/nginx/fastcgi_params;' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '    # our understanding vs nginxs handling of script_name vs' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '      # path_info dont match :)' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '    fastcgi_param PATH_INFO $fastcgi_script_name;' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '    fastcgi_param SCRIPT_NAME "";' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '  }' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '}' >> $MEDIAGOBLIN_VIRTUAL_HOST
        echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
    else
        echo -n '' > $MEDIAGOBLIN_VIRTUAL_HOST
    fi

    echo 'server {' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo "  listen 127.0.0.1:${MEDIAGOBLIN_ONION_PORT} default_server;" >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  #################################################' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  # Stock useful config options, but ignore them :)' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  #################################################' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  include /etc/nginx/mime.types;' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  autoindex off;' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  default_type  application/octet-stream;' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  sendfile on;' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
    function_check nginx_disable_sniffing
    nginx_disable_sniffing $MEDIAGOBLIN_DOMAIN_NAME
    echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  # Gzip' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  gzip on;' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  gzip_min_length 1024;' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  gzip_buffers 4 32k;' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  gzip_types text/plain application/x-javascript text/javascript text/xml text/css;' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  #####################################' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  # Mounting MediaGoblin stuff' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  # This is the section you should read' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  #####################################' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo "  server_name $MEDIAGOBLIN_ONION_HOSTNAME;" >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  access_log /var/log/nginx/mediagoblin.log;' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  error_log /var/log/nginx/mediagoblin.error.log;' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  # MediaGoblins stock static files: CSS, JS, etc.' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  location /mgoblin_static/ {' >> $MEDIAGOBLIN_VIRTUAL_HOST
    function_check nginx_limits
    nginx_limits $MEDIAGOBLIN_DOMAIN_NAME '20m'
    echo "    alias $MEDIAGOBLIN_WORKING_DIRECTORY/mediagoblin/static/;" >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  }' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  # Instance specific media:' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  location /mgoblin_media/ {' >> $MEDIAGOBLIN_VIRTUAL_HOST
    function_check nginx_limits
    nginx_limits $MEDIAGOBLIN_DOMAIN_NAME '20m'
    echo "    alias $MEDIAGOBLIN_WORKING_DIRECTORY/user_dev/media/public/;" >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  }' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  # Theme static files (usually symlinked in)' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  location /theme_static/ {' >> $MEDIAGOBLIN_VIRTUAL_HOST
    function_check nginx_limits
    nginx_limits $MEDIAGOBLIN_DOMAIN_NAME '20m'
    echo "    alias $MEDIAGOBLIN_WORKING_DIRECTORY/user_dev/theme_static/;" >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  }' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  # Plugin static files (usually symlinked in)' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  location /plugin_static/ {' >> $MEDIAGOBLIN_VIRTUAL_HOST
    function_check nginx_limits
    nginx_limits $MEDIAGOBLIN_DOMAIN_NAME '20m'
    echo "    alias $MEDIAGOBLIN_WORKING_DIRECTORY/user_dev/plugin_static/;" >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  }' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  # Mounting MediaGoblin itself via FastCGI.' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  location / {' >> $MEDIAGOBLIN_VIRTUAL_HOST
    function_check nginx_limits
    nginx_limits $MEDIAGOBLIN_DOMAIN_NAME '20m'
    echo '    fastcgi_pass 127.0.0.1:26543;' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '    include /etc/nginx/fastcgi_params;' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '    # our understanding vs nginxs handling of script_name vs' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '      # path_info dont match :)' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '    fastcgi_param PATH_INFO $fastcgi_script_name;' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '    fastcgi_param SCRIPT_NAME "";' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '  }' >> $MEDIAGOBLIN_VIRTUAL_HOST
    echo '}' >> $MEDIAGOBLIN_VIRTUAL_HOST

    function_check create_site_certificate
    create_site_certificate $MEDIAGOBLIN_DOMAIN_NAME

    nginx_ensite $MEDIAGOBLIN_DOMAIN_NAME
    systemctl restart php5-fpm
    systemctl restart nginx

    add_ddns_domain $MEDIAGOBLIN_DOMAIN_NAME

    if ! grep -q "Mediagoblin domain" $COMPLETION_FILE; then
        echo "Mediagoblin domain:$MEDIAGOBLIN_DOMAIN_NAME" >> $COMPLETION_FILE
    else
        sed -i "s/Mediagoblin domain.*/Mediagoblin domain:$MEDIAGOBLIN_DOMAIN_NAME/g" $COMPLETION_FILE
    fi
    APP_INSTALLED=1
}

# NOTE: deliberately no exit 0
