#!/bin/bash
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
#
#                              Freedom in the Cloud
#
# Searx engine application
#
# License
# =======
#
# Copyright (C) 2014-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'

IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=1
SHOW_CLEARNET_ADDRESS_ON_ABOUT=0

SEARX_REPO="https://github.com/asciimoo/searx"
SEARX_COMMIT='80460be8f69cea5f15c9d5ddbb63e4e48fde2dd0'
SEARX_PATH=/etc
SEARX_PORT=8888
SEARX_ONION_PORT=8094
SEARX_ONION_HOSTNAME=
SEARX_LOGIN_TEXT=$"Search engine login"
SEARX_PASSWORD=
SEARX_BACKGROUND_IMAGE_URL=

searx_variables=(SEARX_LOGIN_TEXT
                 MY_USERNAME
                 SEARX_BACKGROUND_IMAGE_URL
                 SYSTEM_TYPE)

function logging_on_searx {
    echo -n ''
}

function logging_off_searx {
    echo -n ''
}

function searx_set_default_background {
    if [ -f "$HOME/${PROJECT_NAME}/img/backgrounds/searx.jpg" ]; then
        cp "$HOME/${PROJECT_NAME}/img/backgrounds/searx.jpg" /etc/searx/searx/static/themes/courgette/img/bg-body-index.jpg
        chown -R searx:searx ${SEARX_PATH}/searx
    else
        if [ -f "/home/$MY_USERNAME/${PROJECT_NAME}/img/backgrounds/searx.jpg" ]; then
            cp "/home/$MY_USERNAME/${PROJECT_NAME}/img/backgrounds/searx.jpg" "/etc/searx/searx/static/themes/courgette/img/bg-body-index.jpg"
            chown -R searx:searx ${SEARX_PATH}/searx
        fi
    fi

    # remove the github ribbon icon
    if [ -f /etc/searx/searx/static/themes/courgette/img/github_ribbon.png ]; then
        mv /etc/searx/searx/static/themes/courgette/img/github_ribbon.png /etc/searx/searx/static/themes/courgette/img/github_ribbon.png.old
    fi
}

function searx_set_background_image_from_url {
    url="$1"

    ext=
    if [ ${#url} -gt 0 ]; then
        if [[ "$url" == *".jpeg" || "$url" == *".jpg" ]]; then
            ext="jpg"
        fi
        if [[ "$url" == *".png" ]]; then
            ext="png"
        fi
        if [[ "$url" == *".gif" ]]; then
            ext="gif"
        fi
    fi

    if [ ${#ext} -gt 0 ]; then
        if [ -d /etc/searx/searx/static/themes/courgette/img ]; then
            cd "/etc/searx/searx/static/themes/courgette/img" || exit 37853539539


            # remove any existing image
            if [ -f bg-body-index.jpg ]; then
                rm bg-body-index.jpg
            fi

            # get the new image
            if [[ "$ext" != 'jpg' ]]; then
                if [ -f bg-body-index.${ext} ]; then
                    rm bg-body-index.${ext}
                fi
                wget "$url" -O bg-body-index.${ext}
                convert bg-body-index.${ext} bg-body-index.jpg
            else
                wget "$url" -O bg-body-index.jpg
            fi
            if [ ! -f bg-body-index.jpg ]; then
                echo "$url"
                echo $'Custom background image for SearX could not be downloaded'
                echo "1"
                return
            fi
            chown -R searx:searx ${SEARX_PATH}/searx
        fi
    else
        echo "2"
        return
    fi

    echo "0"
}

function searx_set_background_image {
    data=$(mktemp 2>/dev/null)
    dialog --title $"SearX Metasearch" \
           --backtitle $"Freedombone Control Panel" \
           --inputbox $'Set a background image URL' 10 60 2>"$data"
    sel=$?
    case $sel in
        0)
            temp_background=$(<"$data")
            if [ ${#temp_background} -gt 0 ]; then
                SEARX_BACKGROUND_IMAGE_URL="$temp_background"
                write_config_param "SEARX_BACKGROUND_IMAGE_URL" "$SEARX_BACKGROUND_IMAGE_URL"
                if [[ $(searx_set_background_image_from_url "$SEARX_BACKGROUND_IMAGE_URL" | tail -n 1) == "0" ]]; then
                    dialog --title $"Set SearX background" \
                           --msgbox $"The background image has been set" 6 60
                fi
            fi
           ;;
    esac
    rm -f "$data"
}

function searx_enable_login {
    dialog --title $"Enable Searx login" \
           --backtitle $"Freedombone Control Panel" \
           --defaultno \
           --yesno $"\\nDo you want to add a login so that random web users can't use your metasearch engine?" 10 60
    sel=$?
    case $sel in
        0) if grep -q '#auth_basic' /etc/nginx/sites-available/searx; then
               sed -i 's|#auth_basic|auth_basic|g' /etc/nginx/sites-available/searx
               systemctl restart nginx
           fi
           read_config_param "$MY_USERNAME"
           SEARX_LOGIN_PASS=$("${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a searx)
           dialog --title $"Enable Searx login" \
                  --msgbox $"Searx logins are now enabled with the password $SEARX_LOGIN_PASS" 6 65
           SEARX_LOGIN_PASS=
           ;;
        1) if ! grep -q '#auth_basic' /etc/nginx/sites-available/searx; then
               sed -i 's|auth_basic|#auth_basic|g' /etc/nginx/sites-available/searx
               systemctl restart nginx
           fi
           dialog --title $"Disable Searx login" \
                  --msgbox $"Searx logins are now disabled. Anyone can access your metasearch engine." 6 65
           ;;
    esac
}

function configure_interactive_searx {
    W=(1 $"Set a background image"
       2 $"Enable login")

    while true
    do
        # shellcheck disable=SC2068
        selection=$(dialog --backtitle $"Freedombone Administrator Control Panel" --title $"SearX" --menu $"Choose an operation, or ESC to exit:" 10 60 2 "${W[@]}" 3>&2 2>&1 1>&3)

        if [ ! "$selection" ]; then
            break
        fi

        case $selection in
            1) searx_set_background_image;;
            2) searx_enable_login;;
        esac
    done
}

function remove_user_searx {
    remove_username="$1"

    "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp searx
    if grep -q "$remove_username:" /etc/nginx/.htpasswd; then
        sed -i "/$remove_username:/d" /etc/nginx/.htpasswd
    fi
}

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

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

    "${PROJECT_NAME}-pass" -u "$new_username" -a searx -p "$new_user_password"

    if grep -q "$new_username:" /etc/nginx/.htpasswd; then
        sed -i "/$new_username:/d" /etc/nginx/.htpasswd
    fi
    echo "$new_user_password" | htpasswd -i -s -c /etc/nginx/.htpasswd "$new_username"

    echo '0'
}

function install_interactive_searx {
    echo -n ''
    APP_INSTALLED=1
}

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

    if grep -q "$new_username:" /etc/nginx/.htpasswd; then
        sed -i "/$new_username:/d" /etc/nginx/.htpasswd
        echo "$new_user_password" | htpasswd -i -s -c /etc/nginx/.htpasswd "$new_username"
        "${PROJECT_NAME}-pass" -u "$new_username" -a searx -p "$new_user_password"
        echo '0'
    fi
    echo '1'
}

function reconfigure_searx {
    echo -n ''
}

function create_searx_config {
    settings_file=${SEARX_PATH}/searx/searx/settings.yml

    SEARX_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_searx/hostname)
    SEARX_SECRET_KEY=$(get_completion_param "searx key")

    { echo 'general:';
      echo '    debug : False';
      echo '    instance_name : "Freedombone Metasearch"';
      echo '';
      echo 'search:';
      echo '    safe_search : 0 # Filter results. 0: None, 1: Moderate, 2: Strict';
      echo '    autocomplete : "" # Existing autocomplete backends: "dbpedia", "duckduckgo", "startpage", "wikipedia" - leave blank to turn it off by default';
      echo '    language : "all"';
      echo '';
      echo 'server:';
      echo "    port : ${SEARX_PORT}";
      echo '    bind_address : "127.0.0.1" # address to listen on';
      echo "    secret_key : \"${SEARX_SECRET_KEY}\"";
      echo "    base_url : http://${SEARX_ONION_HOSTNAME}/";
      echo '    image_proxy : True # Proxying image results through searx';
      echo '    http_protocol_version : "1.1"  # 1.0 and 1.1 are supported';
      echo '';
      echo 'ui:';
      echo '    static_path : "" # Custom static path - leave it blank if you didnt change';
      echo '    templates_path : "" # Custom templates path - leave it blank if you didnt change';
      echo '    themes_path : "" # Custom ui themes path';
      echo '    default_theme : courgette # ui theme';
      echo '    default_locale : "" # Default interface locale - leave blank to detect from browser information or use codes from the "locales" config section';
      echo '';
      echo 'outgoing: # communication with search engines';
      echo '    request_timeout : 10.0 # seconds';
      echo '    useragent_suffix : "" # suffix of searx_useragent, could contain informations like an email address to the administrator';
      echo '    pool_connections : 100 # Number of different hosts';
      echo '    pool_maxsize : 10 # Number of simultaneous requests by host';
      echo '    proxies :';
      echo '        http : socks5://127.0.0.1:9050';
      echo '';
      echo 'engines:';
      echo '  - name : arch linux wiki';
      echo '    engine : archlinux';
      echo '    categories : general';
      echo '    shortcut : al';
      echo '';
      echo '  - name : archive is';
      echo '    engine : xpath';
      echo '    search_url : https://archive.is/{query}';
      echo '    url_xpath : (//div[@class="TEXT-BLOCK"]/a)/@href';
      echo '    title_xpath : (//div[@class="TEXT-BLOCK"]/a)';
      echo '    content_xpath : //div[@class="TEXT-BLOCK"]/ul/li';
      echo '    categories : general';
      echo '    timeout : 7.0';
      echo '    disabled : True';
      echo '    shortcut : ai';
      echo '';
      echo '  - name : base';
      echo '    engine : base';
      echo '    shortcut : bs';
      echo '';
      echo '  - name : wikipedia';
      echo '    engine : wikipedia';
      echo '    shortcut : wp';
      echo '    categories : general';
      echo '    base_url : "https://{language}.wikipedia.org/"';
      echo '';
      echo '  - name : bitbucket';
      echo '    engine : xpath';
      echo '    paging : True';
      echo '    search_url : https://bitbucket.org/repo/all/{pageno}?name={query}';
      echo '    url_xpath : //article[@class="repo-summary"]//a[@class="repo-link"]/@href';
      echo '    title_xpath : //article[@class="repo-summary"]//a[@class="repo-link"]';
      echo '    content_xpath : //article[@class="repo-summary"]/p';
      echo '    categories : code';
      echo '    timeout : 4.0';
      echo '    disabled : True';
      echo '    shortcut : bb';
      echo '';
      echo '  - name : ccc-tv';
      echo '    engine : xpath';
      echo '    paging : False';
      echo '    search_url : https://media.ccc.de/search/?q={query}';
      echo '    url_xpath : //div[@class="caption"]/h3/a/@href';
      echo '    title_xpath : //div[@class="caption"]/h3/a/text()';
      echo '    content_xpath : //div[@class="caption"]/h4/@title';
      echo '    categories : videos';
      echo '    shortcut : c3tv';
      echo '';
      echo '  - name : crossref';
      echo '    engine : json_engine';
      echo '    paging : True';
      echo '    search_url : http://search.crossref.org/dois?q={query}&page={pageno}';
      echo '    url_query : doi';
      echo '    title_query : title';
      echo '    content_query : fullCitation';
      echo '    categories : science';
      echo '    shortcut : cr';
      echo '';
      echo '  - name : currency';
      echo '    engine : currency_convert';
      echo '    categories : general';
      echo '    shortcut : cc';
      echo '';
      echo '  - name : deezer';
      echo '    engine : deezer';
      echo '    shortcut : dz';
      echo '';
      echo '  - name : deviantart';
      echo '    engine : deviantart';
      echo '    shortcut : da';
      echo '    timeout: 3.0';
      echo '';
      echo '  - name : ddg definitions';
      echo '    engine : duckduckgo_definitions';
      echo '    shortcut : ddd';
      echo '    weight : 2';
      echo '    disabled : True';
      echo '';
      echo '  - name : digbt';
      echo '    engine : digbt';
      echo '    shortcut : dbt';
      echo '    timeout : 6.0';
      echo '    disabled : True';
      echo '';
      echo '  - name : erowid';
      echo '    engine : xpath';
      echo '    paging : True';
      echo '    first_page_num : 0';
      echo '    page_size : 30';
      echo '    search_url : https://www.erowid.org/search.php?q={query}&s={pageno}';
      echo '    url_xpath : //dl[@class="results-list"]/dt[@class="result-title"]/a/@href';
      echo '    title_xpath : //dl[@class="results-list"]/dt[@class="result-title"]/a/text()';
      echo '    content_xpath : //dl[@class="results-list"]/dd[@class="result-details"]';
      echo '    categories : general';
      echo '    shortcut : ew';
      echo '    disabled : True';
      echo '';
      echo '  - name : wikidata';
      echo '    engine : wikidata';
      echo '    shortcut : wd';
      echo '    weight : 2';
      echo '';
      echo '  - name : duckduckgo';
      echo '    engine : duckduckgo';
      echo '    shortcut : ddg';
      echo '    categories : general';
      echo '';
      echo '  - name : etymonline';
      echo '    engine : xpath';
      echo '    paging : True';
      echo '    search_url : http://etymonline.com/?search={query}&p={pageno}';
      echo '    url_xpath : //dt/a[1]/@href';
      echo '    title_xpath : //dt';
      echo '    content_xpath : //dd';
      echo '    suggestion_xpath : //a[@class="crossreference"]';
      echo '    first_page_num : 0';
      echo '    shortcut : et';
      echo '    disabled : True';
      echo '';
      echo '  - name : 500px';
      echo '    engine : www500px';
      echo '    shortcut : px';
      echo '';
      echo '  - name : 1x';
      echo '    engine : www1x';
      echo '    shortcut : 1x';
      echo '    disabled : True';
      echo '';
      echo '  - name : fdroid';
      echo '    engine : fdroid';
      echo '    shortcut : fd';
      echo '    disabled : True';
      echo '';
      echo '  - name : flickr';
      echo '    categories : images';
      echo '    shortcut : fl';
      echo '# You can use the engine using the official stable API, but you need an API key';
      echo '# See : https://www.flickr.com/services/apps/create/';
      echo '#    engine : flickr';
      echo "#    api_key: 'apikey' # required!";
      echo '# Or you can use the html non-stable engine, activated by default';
      echo '    engine : flickr_noapi';
      echo '';
      echo '  - name : frinkiac';
      echo '    engine : frinkiac';
      echo '    shortcut : frk';
      echo '    disabled : True';
      echo '';
      echo '  - name : gigablast';
      echo '    engine : gigablast';
      echo '    shortcut : gb';
      echo '    timeout : 3.0';
      echo '    disabled: True';
      echo '';
      echo '  - name : gitlab';
      echo '    engine : json_engine';
      echo '    paging : True';
      echo '    search_url : https://gitlab.com/api/v4/projects?search={query}&page={pageno}';
      echo '    url_query : web_url';
      echo '    title_query : name_with_namespace';
      echo '    content_query : description';
      echo '    page_size : 20';
      echo '    categories : code';
      echo '    shortcut : gl';
      echo '    timeout : 10.0';
      echo '    disabled : False';
      echo '';
      echo '  - name : github';
      echo '    engine : github';
      echo '    shortcut : gh';
      echo '    categories : code';
      echo '';
      echo '  - name : geektimes';
      echo '    engine : xpath';
      echo '    paging : True';
      echo '    search_url : https://geektimes.ru/search/page{pageno}/?q={query}';
      echo '    url_xpath : //div[@class="search_results"]//a[@class="post__title_link"]/@href';
      echo '    title_xpath : //div[@class="search_results"]//a[@class="post__title_link"]';
      echo '    content_xpath : //div[@class="search_results"]//div[contains(@class, "content")]';
      echo '    categories : code';
      echo '    timeout : 4.0';
      echo '    disabled : True';
      echo '    shortcut : gt';
      echo '';
      echo '  - name : habrahabr';
      echo '    engine : xpath';
      echo '    paging : True';
      echo '    search_url : https://habrahabr.ru/search/page{pageno}/?q={query}';
      echo '    url_xpath : //div[@class="search_results"]//a[contains(@class, "post__title_link")]/@href';
      echo '    title_xpath : //div[@class="search_results"]//a[contains(@class, "post__title_link")]';
      echo '    content_xpath : //div[@class="search_results"]//div[contains(@class, "content")]';
      echo '    categories : code';
      echo '    timeout : 4.0';
      echo '    disabled : True';
      echo '    shortcut : habr';
      echo '';
      echo '  - name : hoogle';
      echo '    engine : json_engine';
      echo '    paging : True';
      echo '    search_url : https://www.haskell.org/hoogle/?mode=json&hoogle={query}&start={pageno}';
      echo '    results_query : results';
      echo '    url_query : location';
      echo '    title_query : self';
      echo '    content_query : docs';
      echo '    page_size : 20';
      echo '    categories : code';
      echo '    shortcut : ho';
      echo '';
      echo '  - name : ina';
      echo '    engine : ina';
      echo '    shortcut : in';
      echo '    timeout : 6.0';
      echo '    disabled : True';
      echo '';
      echo '  - name: kickass';
      echo '    engine : kickass';
      echo '    shortcut : kc';
      echo '    timeout : 4.0';
      echo '    disabled : True';
      echo '';
      echo '  - name : library genesis';
      echo '    engine : xpath';
      echo '    search_url : http://libgen.io/search.php?req={query}';
      echo '    url_xpath : //a[contains(@href,"bookfi.net")]/@href';
      echo '    title_xpath : //a[contains(@href,"book/")]/text()[1]';
      echo '    content_xpath : //td/a[1][contains(@href,"=author")]/text()';
      echo '    categories : general';
      echo '    timeout : 7.0';
      echo '    disabled : True';
      echo '    shortcut : lg';
      echo '';
      echo '  - name : lobste.rs';
      echo '    engine : xpath';
      echo '    search_url : https://lobste.rs/search?utf8=%E2%9C%93&q={query}&what=stories&order=relevance';
      echo '    results_xpath : //li[contains(@class, "story")]';
      echo '    url_xpath : .//span[@class="link"]/a/@href';
      echo '    title_xpath : .//span[@class="link"]/a';
      echo '    content_xpath : .//a[@class="domain"]';
      echo '    categories : code';
      echo '    shortcut : lo';
      echo '';
      echo '  - name : mixcloud';
      echo '    engine : mixcloud';
      echo '    shortcut : mc';
      echo '';
      echo '  - name : nyaa';
      echo '    engine : nyaa';
      echo '    shortcut : nt';
      echo '    disabled : True';
      echo '';
      echo '  - name : openstreetmap';
      echo '    engine : openstreetmap';
      echo '    shortcut : osm';
      echo '';
      echo '  - name : openrepos';
      echo '    engine : xpath';
      echo '    paging : True';
      echo '    search_url : https://openrepos.net/search/node/{query}?page={pageno}';
      echo '    url_xpath : //li[@class="search-result"]//h3[@class="title"]/a/@href';
      echo '    title_xpath : //li[@class="search-result"]//h3[@class="title"]/a';
      echo '    content_xpath : //li[@class="search-result"]//div[@class="search-snippet-info"]//p[@class="search-snippet"]';
      echo '    categories : files';
      echo '    timeout : 4.0';
      echo '    disabled : True';
      echo '    shortcut : or';
      echo '';
      echo '  - name : pdbe';
      echo '    engine : pdbe';
      echo '    shortcut : pdb';
      echo '';
      echo '  - name : photon';
      echo '    engine : photon';
      echo '    shortcut : ph';
      echo '';
      echo '  - name : piratebay';
      echo '    engine : piratebay';
      echo '    shortcut : tpb';
      echo '    url: https://pirateproxy.red/';
      echo '    timeout : 3.0';
      echo '';
      echo '  - name : qwant';
      echo '    engine : qwant';
      echo '    shortcut : qw';
      echo '    categories : general';
      echo '    disabled : True';
      echo '';
      echo '  - name : qwant images';
      echo '    engine : qwant';
      echo '    shortcut : qwi';
      echo '    categories : images';
      echo '';
      echo '  - name : qwant news';
      echo '    engine : qwant';
      echo '    shortcut : qwn';
      echo '    categories : news';
      echo '';
      echo '  - name : qwant social';
      echo '    engine : qwant';
      echo '    shortcut : qws';
      echo '    categories : social media';
      echo '';
      echo '  - name : reddit';
      echo '    engine : reddit';
      echo '    shortcut : re';
      echo '    page_size : 25';
      echo '    timeout : 10.0';
      echo '    disabled : True';
      echo '';
      echo '  - name : scanr structures';
      echo '    shortcut: scs';
      echo '    engine : scanr_structures';
      echo '    disabled : True';
      echo '';
      echo '  - name : soundcloud';
      echo '    engine : soundcloud';
      echo '    shortcut : sc';
      echo '';
      echo '  - name : stackoverflow';
      echo '    engine : stackoverflow';
      echo '    shortcut : st';
      echo '';
      echo '  - name : searchcode doc';
      echo '    engine : searchcode_doc';
      echo '    shortcut : scd';
      echo '';
      echo '  - name : searchcode code';
      echo '    engine : searchcode_code';
      echo '    shortcut : scc';
      echo '    disabled : True';
      echo '';
      echo '  - name : framalibre';
      echo '    engine : framalibre';
      echo '    shortcut : frl';
      echo '    disabled : True';
      echo '';
      echo '  - name : semantic scholar';
      echo '    engine : xpath';
      echo '    paging : True';
      echo '    search_url : https://www.semanticscholar.org/search?q={query}&sort=relevance&page={pageno}&ae=false';
      echo '    results_xpath : //article';
      echo '    url_xpath : .//div[@class="search-result-title"]/a/@href';
      echo '    title_xpath : .//div[@class="search-result-title"]/a';
      echo '    content_xpath : .//div[@class="search-result-abstract"]';
      echo '    shortcut : se';
      echo '    categories : science';
      echo '';
      echo '  - name : spotify';
      echo '    engine : spotify';
      echo '    shortcut : stf';
      echo '';
      echo '  - name : subtitleseeker';
      echo '    engine : subtitleseeker';
      echo '    shortcut : ss';
      echo '# The language is an option. You can put any language written in english';
      echo '# Examples : English, French, German, Hungarian, Chinese...';
      echo '#    language : English';
      echo '';
      echo '  - name : startpage';
      echo '    engine : startpage';
      echo '    shortcut : sp';
      echo '    timeout : 6.0';
      echo '    disabled : True';
      echo '';
      echo '  - name : ixquick';
      echo '    engine : startpage';
      echo "    base_url : 'https://www.ixquick.eu/'";
      echo "    search_url : 'https://www.ixquick.eu/do/search'";
      echo '    shortcut : iq';
      echo '    timeout : 6.0';
      echo '';
      echo '  - name : swisscows';
      echo '    engine : swisscows';
      echo '    shortcut : sw';
      echo '    disabled : True';
      echo '';
      echo '  - name : tokyotoshokan';
      echo '    engine : tokyotoshokan';
      echo '    shortcut : tt';
      echo '    timeout : 6.0';
      echo '    disabled : True';
      echo '';
      echo '  - name : twitter';
      echo '    engine : twitter';
      echo '    shortcut : tw';
      echo '';
      echo '  - name : urbandictionary';
      echo '    engine        : xpath';
      echo '    search_url    : http://www.urbandictionary.com/define.php?term={query}';
      echo '    url_xpath     : //*[@class="word"]/@href';
      echo '    title_xpath   : //*[@class="def-header"]';
      echo '    content_xpath : //*[@class="meaning"]';
      echo '    shortcut : ud';
      echo '';
      echo '  - name : yandex';
      echo '    engine : yandex';
      echo '    shortcut : yn';
      echo '    disabled : True';
      echo '';
      echo '  - name : youtube';
      echo '    shortcut : yt';
      echo '    # You can use the engine using the official stable API, but you need an API key';
      echo '    # See : https://console.developers.google.com/project';
      echo '    #    engine : youtube_api';
      echo '    #    api_key: apikey # required!';
      echo '    # Or you can use the html non-stable engine, activated by default';
      echo '    engine : youtube_noapi';
      echo '';
      echo '  - name : dailymotion';
      echo '    engine : dailymotion';
      echo '    shortcut : dm';
      echo '';
      echo '  - name : vimeo';
      echo '    engine : vimeo';
      echo '    shortcut : vm';
      echo '';
      echo '  - name : wolframalpha';
      echo '    shortcut : wa';
      echo '    # You can use the engine using the official stable API, but you need an API key';
      echo '    # See : http://products.wolframalpha.com/api/';
      echo '    # engine : wolframalpha_api';
      echo '    # api_key: '' # required!';
      echo '    engine : wolframalpha_noapi';
      echo '    timeout: 6.0';
      echo '    categories : science';
      echo '';
      echo '  - name : seedpeer';
      echo '    engine : seedpeer';
      echo '    shortcut: speu'
      echo '    categories: files, music, videos'
      echo '    disabled: True';
      echo '';
      echo '  - name : dictzone';
      echo '    engine : dictzone';
      echo '    shortcut : dc';
      echo '';
      echo '  - name : mymemory translated';
      echo '    engine : translated';
      echo '    shortcut : tl';
      echo '    timeout : 5.0';
      echo '    disabled : True';
      echo '    # You can use without an API key, but you are limited to 1000 words/day';
      echo '    # See : http://mymemory.translated.net/doc/usagelimits.php';
      echo '    # api_key : ""';
      echo '';
      echo '  - name : voat';
      echo '    engine: xpath';
      echo '    shortcut: vo';
      echo '    categories: social media';
      echo '    search_url : https://voat.co/search?q={query}';
      echo '    url_xpath : //p[contains(@class, "title")]/a/@href';
      echo '    title_xpath : //p[contains(@class, "title")]/a';
      echo '    content_xpath : //span[@class="domain"]';
      echo '    timeout : 10.0';
      echo '    disabled : True';
      echo '';
      echo '  - name : 1337x';
      echo '    engine : 1337x';
      echo '    shortcut : 1337x';
      echo '    disabled : True';
      echo '';
      echo 'locales:';
      echo '    en : English';
      echo '    bg :  (Bulgarian)';
      echo '    cs : etina (Czech)';
      echo '    de : Deutsch (German)';
      echo '    de_DE : Deutsch (German_Germany)';
      echo '    el_GR :  (Greek_Greece)';
      echo '    eo : Esperanto (Esperanto)';
      echo '    es : Espaol (Spanish)';
      echo '    fi : Suomi (Finnish)';
      echo '    fr : Franais (French)';
      echo '    he :  (Hebrew)';
      echo '    hu : Magyar (Hungarian)';
      echo '    it : Italiano (Italian)';
      echo '    ja :  (Japanese)';
      echo '    nl : Nederlands (Dutch)';
      echo '    pt : Portugus (Portuguese)';
      echo '    pt_BR : Portugus (Portuguese_Brazil)';
      echo '    ro : Romn (Romanian)';
      echo '    ru :  (Russian)';
      echo '    sk : Slovenina (Slovak)';
      echo '    sv : Svenska (Swedish)';
      echo '    tr : Trke (Turkish)';
      echo '    uk :   (Ukrainian)';
      echo '    zh :  (Chinese)';
      echo '';
      echo 'doi_resolvers :';
      echo "    oadoi.org : 'https://oadoi.org/'";
      echo "    doi.org : 'https://doi.org/'";
      echo "    doai.io : 'http://doai.io/'";
      echo '';
      echo "default_doi_resolver : 'oadoi.org'"; } > $settings_file
}

function upgrade_searx {
    CURR_SEARX_COMMIT=$(get_completion_param "searx commit")
    if [[ "$CURR_SEARX_COMMIT" == "$SEARX_COMMIT" ]]; then
        return
    fi

    settings_file=${SEARX_PATH}/searx/searx/settings.yml
    background_image=/etc/searx/searx/static/themes/courgette/img/bg-body-index.jpg

    # save the background image
    if [ -f ${background_image} ]; then
        cp ${background_image} ${background_image}.prev
    fi

    # save the settings
    cp ${settings_file} ${settings_file}.prev

    # do the upgrade
    set_repo_commit $SEARX_PATH/searx "searx commit" "$SEARX_COMMIT" $SEARX_REPO

    # restore the background image
    if [ -f ${background_image}.prev ]; then
        cp ${background_image}.prev ${background_image}
        chown -R searx:searx ${SEARX_PATH}/searx
    fi

    # restore the settings
    if [ -f ${settings_file}.prev ]; then
        cp ${settings_file}.prev ${settings_file}
        chown -R searx:searx ${SEARX_PATH}/searx
    fi

    # remove the github ribbon icon
    if [ -f /etc/searx/searx/static/themes/courgette/img/github_ribbon.png ]; then
        mv /etc/searx/searx/static/themes/courgette/img/github_ribbon.png /etc/searx/searx/static/themes/courgette/img/github_ribbon.png.old
    fi
}

function backup_local_searx {
    echo -n ''
}

function restore_local_searx {
    echo -n ''
}

function backup_remote_searx {
    echo -n ''
}

function restore_remote_searx {
    echo -n ''
}

function remove_searx {
    systemctl stop searx
    systemctl disable searx
    rm /etc/systemd/system/searx.service
    systemctl daemon-reload

    function_check remove_onion_service
    remove_onion_service searx ${SEARX_ONION_PORT}
    userdel -r searx
    nginx_dissite searx
    if [ -f /etc/nginx/sites-available/searx ]; then
        rm /etc/nginx/sites-available/searx
    fi
    if [ -d ${SEARX_PATH}/searx ]; then
        rm -rf ${SEARX_PATH}/searx
    fi
    remove_completion_param install_searx
    sed -i '/Search engine /d' "$COMPLETION_FILE"
    sed -i '/searx /d' "$COMPLETION_FILE"
}

function install_searx {
    # Note: currently socks5 outgoing proxies to other search engines does not work
    if [ ! -d /etc/nginx ]; then
        echo $'Webserver is not installed'
        exit 62429
    fi

    $INSTALL_PACKAGES python-pip libyaml-dev python-werkzeug python-babel python-lxml
    $INSTALL_PACKAGES git build-essential libxslt-dev python-dev python-virtualenv zlib1g-dev uwsgi uwsgi-plugin-python imagemagick
    $INSTALL_PACKAGES apache2-utils python-setuptools
    $INSTALL_PACKAGES python-service-identity python-ndg-httpsclient

    $REMOVE_PACKAGES_PURGE apache2-bin*
    if [ -d /etc/apache2 ]; then
        rm -rf /etc/apache2
        echo $'Removed Apache installation'
    fi

    pip install --upgrade pip

    if ! pip install certifi; then
        echo $'Failed to install certifi'
        exit 737692
    fi

    if ! pip install pyyaml; then
        echo $'Failed to install pyyaml'
        exit 469242
    fi

    if ! pip install flask --upgrade; then
        echo $'Failed to install flask'
        exit 888575
    fi

    if ! pip install flask_restless --upgrade; then
        echo $'Failed to install flask_restless'
        exit 54835
    fi

    if ! pip install flask_babel --upgrade; then
        echo $'Failed to install flask_babel'
        exit 63738
    fi

    if ! pip install requests --upgrade; then
        echo $'Failed to install requests'
        exit 357282
    fi

    if ! pip install pygments --upgrade; then
        echo $'Failed to install pygments'
        exit 357282
    fi

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

    # clone the repo
    cd $SEARX_PATH || exit 26482468

    if [ -d /repos/searx ]; then
        mkdir searx
        cp -r -p /repos/searx/. searx
        cd searx || exit 2468246
        git pull
    else
        function_check git_clone
        git_clone $SEARX_REPO searx
    fi

    git checkout $SEARX_COMMIT -b $SEARX_COMMIT
    set_completion_param "searx commit" "$SEARX_COMMIT"

    # create an onion service
    USE_V2_ONION_ADDRESS=1
    SEARX_ONION_HOSTNAME=$(add_onion_service searx 80 ${SEARX_ONION_PORT})

    # an unprivileged user to run as
    useradd -d ${SEARX_PATH}/searx/ -s /bin/false searx
    adduser searx debian-tor

    # daemon
    { echo '[Unit]';
      echo 'Description=Searx (search engine)';
      echo 'After=syslog.target';
      echo 'After=network.target';
      echo '';
      echo '[Service]';
      echo 'Type=simple';
      echo 'User=searx';
      echo 'Group=searx';
      echo "WorkingDirectory=${SEARX_PATH}/searx";
      echo "ExecStart=/usr/bin/python ${SEARX_PATH}/searx/searx/webapp.py";
      echo 'Restart=always';
      echo 'Environment="USER=searx"';
      echo '';
      echo '[Install]';
      echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/searx.service

    # create a webserver file
    { echo 'server {';
      echo "    listen 127.0.0.1:${SEARX_ONION_PORT} default_server;";
      echo "    root ${SEARX_PATH}/searx;";
      echo "    server_name ${SEARX_ONION_HOSTNAME};";
      echo '    access_log /dev/null;';
      echo "    error_log /var/log/searx_error.log $WEBSERVER_LOG_LEVEL;";
      echo ''; } > /etc/nginx/sites-available/searx
    function_check nginx_security_options
    nginx_security_options searx
    { echo '    add_header Strict-Transport-Security max-age=0;';
      echo '';
      echo '    location / {'; } >> /etc/nginx/sites-available/searx
    function_check nginx_limits
    nginx_limits searx '1M'
    { echo "        proxy_pass http://localhost:${SEARX_PORT};";
      echo "        #auth_basic \"${SEARX_LOGIN_TEXT}\";";
      echo '        #auth_basic_user_file /etc/nginx/.htpasswd;';
      echo '    }';
      echo '';
      echo '    fastcgi_buffers 64 4K;';
      echo '';
      echo '    error_page 403 /core/templates/403.php;';
      echo '    error_page 404 /core/templates/404.php;';
      echo '';
      echo '    location = /robots.txt {';
      echo '        allow all;';
      echo '        log_not_found off;';
      echo '        access_log /dev/null;';
      echo '    }';
      echo '}'; } >> /etc/nginx/sites-available/searx

    # replace the secret key
    if ! grep -q "searx key" "$COMPLETION_FILE"; then
        SEARX_SECRET_KEY="$(create_password 30)"
        set_completion_param "searx key" "${SEARX_SECRET_KEY}"
    fi

    create_searx_config

    searx_set_default_background

    chown -R searx:searx ${SEARX_PATH}/searx

    # enable the site
    nginx_ensite searx

    # restart the web server
    systemctl restart php7.0-fpm
    systemctl restart nginx

    # start the daemon
    systemctl enable searx.service
    systemctl daemon-reload
    systemctl start searx.service

    SEARX_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
    "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a searx -p "$SEARX_PASSWORD"
    if grep -q "$MY_USERNAME:" /etc/nginx/.htpasswd; then
        sed -i "/$MY_USERNAME:/d" /etc/nginx/.htpasswd
    fi
    echo "$SEARX_PASSWORD" | htpasswd -i -s -c /etc/nginx/.htpasswd "$MY_USERNAME"

    APP_INSTALLED=1
}

# NOTE: deliberately no exit 0