#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Ghost blog
# Only works with nodejs version ^6.9.0
#
# License
# =======
#
# Copyright (C) 2016-2017 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 writer"

IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=1

GHOST_VERSION=1.6.2
GHOST_DOMAIN_NAME=
GHOST_CODE=
GHOST_ONION_PORT=8104
GHOST_PORT=2368

ghost_variables=(GHOST_DOMAIN_NAME
                 GHOST_CODE
                 GHOST_ADMIN_PASSWORD
                 ONION_ONLY
                 DDNS_PROVIDER
                 MY_USERNAME)

function ghost_bust {
    # kill the started ghost process
    kill_pid=$(ps aux | grep "ghost run" | awk -F ' ' '{print $2}' | head -n 1)
    kill -9 $kill_pid

    kill_pid=$(ps aux | grep "ghost" | awk -F ' ' '{print $2}' | head -n 1)
    kill -9 $kill_pid

    kill_pid=$(ps aux | grep "ghost" | awk -F ' ' '{print $2}' | head -n 1)
    kill -9 $kill_pid
}

function logging_on_ghost {
    echo -n ''
}

function logging_off_ghost {
    echo -n ''
}

function ghost_replace_jquery {
    curr_domain="https://$GHOST_DOMAIN_NAME"
    if [[ "$ONION_ONLY" != 'no' ]]; then
        curr_domain="http://$GHOST_ONION_HOSTNAME"
    fi

    sed -i "s|src=\"https://code.jquery.com/jquery-.*|src=\"$curr_domain/jquery-${jquery_version}.js\"|g" current/content/themes/casper/default.hbs
    sed -i "s|src=\"https://code.jquery.com/jquery-.*|src=\"$curr_domain/jquery-${jquery_version}.js\"></script>|g" current/node_modules/gscan/app/tpl/layouts/default.hbs
    sed -i "s|http://code.jquery.com/jquery.js|$curr_domain/jquery-${jquery_version}.js|g" current/node_modules/jsdom/README.md
    sed -i "s|https://code.jquery.com/jquery.js|$curr_domain/jquery-${jquery_version}.js|g" current/node_modules/jsdom/README.md

    cd /var/www/${GHOST_DOMAIN_NAME}/htdocs/current
    find ./ -type f -exec sed -i -e 's|https://code.jquery.com|$curr_domain|g' {} \;
    find ./ -type f -exec sed -i -e 's|http://code.jquery.com|$curr_domain|g' {} \;
}

function ghost_rss_button {
    # remove feedly -aaargh!
    sed -i 's|http://cloud.feedly.com/#subscription/feed/{{@blog.url}}/rss/|{{@blog.url}}/rss/|g' /var/www/$GHOST_DOMAIN_NAME/htdocs/current/content/themes/casper/partials/site-nav.hbs
    sed -i 's|http://cloud.feedly.com/#subscription/feed/{{url absolute="true"}}/rss/|{{url absolute="true"}}rss/|g' /var/www/$GHOST_DOMAIN_NAME/htdocs/current/content/themes/casper/author.hbs
}

function ghost_remove_offsite_links {
    curr_domain="$GHOST_DOMAIN_NAME"
    if [[ "$ONION_ONLY" != 'no' ]]; then
        curr_domain="$GHOST_ONION_HOSTNAME"
    fi

    ghost_rss_button

    # remove google font links
    cd /var/www/$GHOST_DOMAIN_NAME/htdocs/current
    find ./ -type f -exec sed -i -e 's/fonts.googleapis.com/$curr_domain/g' {} \;

    # copy jquery locally
    previous_jquery_version='1.12.0'
    jquery_version='1.12.4'
    if [ ! -f /var/www/$GHOST_DOMAIN_NAME/htdocs/jquery-${jquery_version}.js ]; then
        cd /var/www/$GHOST_DOMAIN_NAME/htdocs
        wget https://code.jquery.com/jquery-${jquery_version}.js
        jquery_hash=$(sha256sum jquery-${jquery_version}.js | awk -F ' ' '{print $1}')
        if [[ "$jquery_hash" != '430f36f9b5f21aae8cc9dca6a81c4d3d84da5175eaedcf2fdc2c226302cb3575' ]]; then
            echo $'Unexpected jquery hash value'
            exit 258442
        fi
    fi
    ghost_replace_jquery
    previous_jquery_version='1.11.3'
    ghost_replace_jquery
}

function ghost_replace_proprietary_services {
    replace_file=$1

    sed -i 's|Twitter Profile|GNU Social Profile|g' $replace_file
    sed -i 's|Twitter profile|GNU Social Profile|g' $replace_file
    sed -i 's|Twitter Username|GNU Social Username|g' $replace_file
    sed -i 's|twitter.com|quitter.se|g' $replace_file
    sed -i 's|Facebook Page|Hubzilla Channel|g' $replace_file
    sed -i 's|Facebook Profile|Hubzilla Channel|g' $replace_file
    sed -i 's|Facebook profile|Hubzilla Channel|g' $replace_file
    sed -i 's|www.facebook.com/username|hubzilladomain/username|g' $replace_file
    sed -i 's|www.facebook.com/ghost|hubzilladomain/username|g' $replace_file
    sed -i 's|www.facebook.com/testuser|hubzilladomain/username|g' $replace_file
    sed -i 's|www.facebook.com/testing|hubzilladomain/username|g' $replace_file
    sed -i 's|www.facebook.com/test|hubzilladomain/username|g' $replace_file
    sed -i 's|www.facebook.com/yourUsername|hubzilladomain/username|g' $replace_file
    sed -i 's|www.facebook.com/yourPage|hubzilladomain/username|g' $replace_file
    sed -i 's|Facebook Username|Hubzilla Channel|g' $replace_file
    sed -i 's|www.facebook.com|hubzilladomain|g' $replace_file
    sed -i 's|facebook value|hubzilla value|g' $replace_file

    sed -i '/<section class="share">/,/<\/section>/d' $replace_file
}

function ghost_replace_services {
    ghost_replace_proprietary_services /var/www/${GHOST_DOMAIN_NAME}/htdocs/content/themes/casper/post.hbs
}

function remove_user_ghost {
    remove_username="$1"
}

function add_user_ghost {
    if [[ $(app_is_installed ghost) == "0" ]]; then
        echo '0'
        return
    fi

    new_username="$1"
    new_user_password="$2"

    echo '0'
}

function install_interactive_ghost {
    if [ ! $ONION_ONLY ]; then
        ONION_ONLY='no'
    fi

    if [[ $ONION_ONLY != "no" ]]; then
        GHOST_DOMAIN_NAME='ghost.local'
        write_config_param "GHOST_DOMAIN_NAME" "$GHOST_DOMAIN_NAME"
    else
        function_check interactive_site_details
        interactive_site_details "ghost" "GHOST_DOMAIN_NAME" "GHOST_CODE"
    fi
    APP_INSTALLED=1
}

function change_password_ghost {
    GHOST_USERNAME="$1"
    GHOST_PASSWORD="$2"
    if [ ${#GHOST_PASSWORD} -lt 8 ]; then
        echo $'Ghost password is too short'
        return
    fi
    #${PROJECT_NAME}-pass -u $GHOST_USERNAME -a ghost -p "$GHOST_PASSWORD"
}

function reconfigure_ghost {
    echo -n ''
}

function upgrade_ghost {
    CURR_GHOST_VERSION=$(get_completion_param "ghost version")
    if [[ "${CURR_GHOST_VERSION}" == "${GHOST_VERSION}" ]]; then
        return
    fi

    read_config_param GHOST_DOMAIN_NAME

    if [ ! -d /var/www/$GHOST_DOMAIN_NAME/htdocs ]; then
        return
    fi

    systemctl stop ghost
    ghost_bust

    cd /var/www/$GHOST_DOMAIN_NAME/htdocs

    npm i -g ghost-cli
    /usr/local/bin/ghost update
    ghost_bust

    ghost_replace_services
    ghost_remove_offsite_links

    chown root:root /usr/local/bin/ghost
    chown -R root:root /usr/local/lib
    chown -R ghost: /var/www/${GHOST_DOMAIN_NAME}/htdocs
    systemctl restart ghost
    sed -i "s|ghost version.*|ghost version:${GHOST_VERSION}|g" ${COMPLETION_FILE}
}

function backup_local_ghost {
    GHOST_DOMAIN_NAME='ghost.local'
    if grep -q "ghost domain" $COMPLETION_FILE; then
        GHOST_DOMAIN_NAME=$(get_completion_param "ghost domain")
    fi

    ghost_path=/var/www/${GHOST_DOMAIN_NAME}/htdocs/content
    if [ -d $ghost_path ]; then
        suspend_site ${GHOST_DOMAIN_NAME}
        systemctl stop ghost
        backup_directory_to_usb $ghost_path ghostcontent
        systemctl start ghost
        restart_site
    fi
}

function restore_local_ghost {
    GHOST_DOMAIN_NAME='ghost.local'
    if grep -q "ghost domain" $COMPLETION_FILE; then
        GHOST_DOMAIN_NAME=$(get_completion_param "ghost domain")
    fi
    if [ $GHOST_DOMAIN_NAME ]; then
        suspend_site ${GHOST_DOMAIN_NAME}
        systemctl stop ghost

        temp_restore_dir=/root/tempghostcontent
        function_check restore_directory_from_usb
        restore_directory_from_usb $temp_restore_dir ghostcontent
        if [ -d $temp_restore_dir ]; then
            if [ -d $temp_restore_dir/var/www/$GHOST_DOMAIN_NAME/htdocs/content ]; then
                cp -r $temp_restore_dir/var/www/$GHOST_DOMAIN_NAME/htdocs/content/* /var/www/$GHOST_DOMAIN_NAME/htdocs/content/
            else
                cp -r $temp_restore_dir/* /var/www/$GHOST_DOMAIN_NAME/htdocs/content/
            fi
            chown -R ghost:ghost /var/www/$GHOST_DOMAIN_NAME/htdocs/content
            rm -rf $temp_restore_dir
        fi

        systemctl start ghost
        restart_site
    fi
}

function backup_remote_ghost {
    GHOST_DOMAIN_NAME='ghost.local'
    if grep -q "ghost domain" $COMPLETION_FILE; then
        GHOST_DOMAIN_NAME=$(get_completion_param "ghost domain")
    fi

    temp_backup_dir=/var/www/${GHOST_DOMAIN_NAME}/htdocs/content
    if [ -d $temp_backup_dir ]; then
        suspend_site ${GHOST_DOMAIN_NAME}
        backup_directory_to_friend $temp_backup_dir ghostcontent
        restart_site
    else
        echo $"Ghost domain specified but not found in /var/www/${GHOST_DOMAIN_NAME}"
        exit 2578
    fi
}

function restore_remote_ghost {
    GHOST_DOMAIN_NAME='ghost.local'
    if grep -q "ghost domain" $COMPLETION_FILE; then
        GHOST_DOMAIN_NAME=$(get_completion_param "ghost domain")
    fi
    suspend_site ${GHOST_DOMAIN_NAME}

    systemctl stop ghost

    temp_restore_dir=/root/tempghostcontent
    function_check restore_directory_from_friend
    restore_directory_from_friend $temp_restore_dir ghostcontent
    if [ -d $temp_restore_dir ]; then
        if [ -d $temp_restore_dir/var/www/$GHOST_DOMAIN_NAME/htdocs/content ]; then
            cp -r $temp_restore_dir/var/www/$GHOST_DOMAIN_NAME/htdocs/content/* /var/www/$GHOST_DOMAIN_NAME/htdocs/content/
        else
            cp -r $temp_restore_dir/* /var/www/$GHOST_DOMAIN_NAME/htdocs/content/
        fi
        chown -R ghost: /var/www/$GHOST_DOMAIN_NAME/htdocs
        rm -rf $temp_restore_dir
    fi

    systemctl start ghost
    restart_site
}

function remove_ghost {
    if [ ${#GHOST_DOMAIN_NAME} -eq 0 ]; then
        return
    fi

    systemctl stop ghost
    systemctl disable ghost
    rm /etc/systemd/system/ghost.service
    systemctl daemon-reload

    function_check remove_nodejs
    remove_nodejs ghost

    read_config_param "GHOST_DOMAIN_NAME"
    nginx_dissite $GHOST_DOMAIN_NAME
    remove_certs ${GHOST_DOMAIN_NAME}
    if [ -f /etc/nginx/sites-available/$GHOST_DOMAIN_NAME ]; then
        rm -f /etc/nginx/sites-available/$GHOST_DOMAIN_NAME
    fi
    if [ -d /var/www/$GHOST_DOMAIN_NAME ]; then
        rm -rf /var/www/$GHOST_DOMAIN_NAME
    fi
    remove_config_param GHOST_DOMAIN_NAME
    remove_config_param GHOST_CODE
    function_check remove_onion_service
    remove_onion_service ghost ${GHOST_ONION_PORT}
    remove_completion_param "install_ghost"
    sed -i '/Ghost/d' $COMPLETION_FILE
    sed -i '/ghost/d' $COMPLETION_FILE

    groupdel -f ghost
    userdel -r ghost

    function_check remove_ddns_domain
    remove_ddns_domain $GHOST_DOMAIN_NAME
}

function install_ghost {
    if [ ! $ONION_ONLY ]; then
        ONION_ONLY='no'
    fi

    if [ ! $GHOST_DOMAIN_NAME ]; then
        echo $'The ghost domain name was not specified'
        exit 5062
    fi

    # for the avatar changing command
    apt-get -yq install unzip wget

    if [ ! -d /var/www/$GHOST_DOMAIN_NAME/htdocs ]; then
        mkdir -p /var/www/$GHOST_DOMAIN_NAME/htdocs
    fi
    cd /var/www/$GHOST_DOMAIN_NAME/htdocs

    function_check install_nodejs
    install_nodejs ghost

    # Some dependencies of ghost
    npm install -g jison@0.4.13 --save
    npm install moment-timezone@0.5.13
    npm install express@4.15.3
    npm install lodash@4.17.4
    npm install uuid@3.0.1
    npm install bluebird@3.5.0
    npm install chalk@1.1.3
    npm install intl-messageformat@1.3.0
    npm install validator@7.0.0
    npm install express-hbs@1.0.4
    npm install glob@7.1.2
    npm install unidecode@0.1.8
    npm install csv-parser@1.11.0
    npm install archiver@1.3.0
    npm install fs-extra@3.0.1
    npm install extract-zip-fork@1.5.1
    npm install moment@2.18.1
    npm install nodemailer@4.0.1
    npm install html-to-text@3.3.0
    npm install gscan@1.1.0
    npm install body-parser@1.17.2
    npm install compression@1.6.2
    npm install morgan@1.8.2
    npm install semver@5.3.0
    npm install path-match@1.2.4
    npm install downsize@0.0.8
    npm install rss@1.2.2
    npm install cheerio@1.0.0-rc.1
    npm install passport@0.3.2
    npm install xml@1.0.1
    npm install multer@1.3.0
    npm install oauth2orize@1.8.0
    npm install connect-slashes@1.3.1
    npm install cors@2.8.3
    npm install netjet@1.1.3
    npm install jsonpath@0.2.11
    npm install image-size@0.5.4
    npm install passport-oauth2-client-password@0.1.2
    npm install passport-http-bearer@1.0.1
    npm install amperize@0.3.4
    npm install bcryptjs@2.4.3
    npm install knex@0.12.9
    npm install bookshelf@0.10.2
    npm install cookie-session@1.2.0
    npm install ghost-gql@0.0.6
    npm install intl@1.2.5
    npm install sanitize-html@1.14.1
    npm install showdown-ghost@0.3.6
    npm install superagent@3.5.2
    npm install mysql@2.1.1
    npm install mariasql@0.2.6
    npm install debug@2.6.8
    npm install nconf@0.8.4

    npm install -g knex-migrator
    knex-migrator init

    # now install ghost itself
    npm install -g ghost-cli

    GHOST_ONION_HOSTNAME=$(add_onion_service ghost 80 ${GHOST_ONION_PORT})

    ghost install local --port ${GHOST_PORT}

    ghost_bust

    # NOTE: this has to be http, not https
    sed -i "s|\"url\":|\"url\": \"http://${GHOST_DOMAIN_NAME}/\",|g" /var/www/${GHOST_DOMAIN_NAME}/htdocs/config.development.json

    adduser --system --home=/var/www/${GHOST_DOMAIN_NAME}/htdocs/ --group ghost

    echo '[Unit]' > /etc/systemd/system/ghost.service
    echo 'Description=Ghost Blog' >> /etc/systemd/system/ghost.service
    echo 'After=syslog.target' >> /etc/systemd/system/ghost.service
    echo 'After=network.target' >> /etc/systemd/system/ghost.service
    echo '' >> /etc/systemd/system/ghost.service
    echo '[Service]' >> /etc/systemd/system/ghost.service
    echo 'Type=simple' >> /etc/systemd/system/ghost.service
    echo 'User=ghost' >> /etc/systemd/system/ghost.service
    echo 'Group=ghost' >> /etc/systemd/system/ghost.service
    echo "WorkingDirectory=/var/www/${GHOST_DOMAIN_NAME}/htdocs" >> /etc/systemd/system/ghost.service
    echo "ExecStart=/usr/local/bin/ghost run -D" >> /etc/systemd/system/ghost.service
    echo "ExecStop=/usr/local/bin/ghost stop" >> /etc/systemd/system/ghost.service
    echo "ExecRestart=/usr/local/bin/ghost restart" >> /etc/systemd/system/ghost.service
    echo 'Restart=always' >> /etc/systemd/system/ghost.service
    echo 'RestartSec=60' >> /etc/systemd/system/ghost.service
    echo "Environment=NODE_ENV=development PORT=${GHOST_PORT}" >> /etc/systemd/system/ghost.service
    echo '' >> /etc/systemd/system/ghost.service
    echo '[Install]' >> /etc/systemd/system/ghost.service
    echo 'WantedBy=multi-user.target' >> /etc/systemd/system/ghost.service

    ghost_remove_offsite_links

    chown -R ghost: /var/www/${GHOST_DOMAIN_NAME}/htdocs

    systemctl enable ghost
    systemctl daemon-reload
    systemctl start ghost

    if [[ ${ONION_ONLY} == "no" ]]; then
        function_check nginx_http_redirect
        nginx_http_redirect ${GHOST_DOMAIN_NAME}
        echo 'server {' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '    listen 443 ssl;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '    listen [::]:443 ssl;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo "    root /var/www/${GHOST_DOMAIN_NAME}/htdocs;" >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo "    server_name ${GHOST_DOMAIN_NAME};" >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '    access_log /dev/null;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo "    error_log /dev/null;" >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        function_check nginx_ssl
        nginx_ssl ${GHOST_DOMAIN_NAME}
        function_check nginx_disable_sniffing
        nginx_disable_sniffing ${GHOST_DOMAIN_NAME}
        echo '    add_header Strict-Transport-Security max-age=0;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '    location / {' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        function_check nginx_limits
        nginx_limits ${GHOST_DOMAIN_NAME} '10G'
        echo "        proxy_pass http://localhost:${GHOST_PORT};" >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '    }' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '    fastcgi_buffers 64 4K;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '    error_page 403 /core/templates/403.php;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '    error_page 404 /core/templates/404.php;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '    location = /robots.txt {' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '        allow all;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '        log_not_found off;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '        access_log /dev/null;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '    }' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        nginx_keybase $GHOST_DOMAIN_NAME
        echo '}' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
        echo '' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    else
        echo -n '' > /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    fi
    echo 'server {' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo "    listen 127.0.0.1:${GHOST_ONION_PORT} default_server;" >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo "    root /var/www/$GHOST_DOMAIN_NAME/htdocs;" >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo "    server_name $GHOST_ONION_HOSTNAME;" >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '    access_log /dev/null;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo "    error_log /dev/null;" >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    function_check nginx_disable_sniffing
    nginx_disable_sniffing ${GHOST_DOMAIN_NAME}
    echo '    add_header Strict-Transport-Security max-age=0;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '    location / {' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    function_check nginx_limits
    nginx_limits ${GHOST_DOMAIN_NAME} '10G'
    echo "        proxy_pass http://localhost:${GHOST_PORT};" >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '    }' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '    fastcgi_buffers 64 4K;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '    error_page 403 /core/templates/403.php;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '    error_page 404 /core/templates/404.php;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '    location = /robots.txt {' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '        allow all;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '        log_not_found off;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '        access_log /dev/null;' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '    }' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    echo '' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}
    nginx_keybase ${GHOST_DOMAIN_NAME}
    echo '}' >> /etc/nginx/sites-available/${GHOST_DOMAIN_NAME}

    function_check create_site_certificate
    create_site_certificate $GHOST_DOMAIN_NAME 'yes'

    ghost_replace_services

    function_check nginx_ensite
    nginx_ensite $GHOST_DOMAIN_NAME

    systemctl restart nginx

    ${PROJECT_NAME}-pass -u $MY_USERNAME -a ghost -p "$GHOST_ADMIN_PASSWORD"

    function_check add_ddns_domain
    add_ddns_domain $GHOST_DOMAIN_NAME

    chown root:root /usr/local/bin/ghost
    chown -R root:root /usr/local/lib
    chown -R ghost: /var/www/${GHOST_DOMAIN_NAME}/htdocs
    set_completion_param "ghost domain" "$GHOST_DOMAIN_NAME"
    if ! grep -q "ghost version:" ${COMPLETION_FILE}; then
        echo "ghost version:${GHOST_VERSION}" >> ${COMPLETION_FILE}
    else
        sed -i "s|ghost version.*|ghost version:${GHOST_VERSION}|g" ${COMPLETION_FILE}
    fi
    APP_INSTALLED=1
}

# NOTE: deliberately no exit 0
