Skip to content
Snippets Groups Projects
freedombone-app-tox 26.9 KiB
Newer Older
Bob Mottram's avatar
Bob Mottram committed
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
Bob Mottram's avatar
Bob Mottram committed
#
# Tox Application
#
# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2014-2019 Bob Mottram <bob@freedombone.net>
Bob Mottram's avatar
Bob Mottram committed
#
# 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 chat'
IN_DEFAULT_INSTALL=0
NOT_ON_HOMEPAGE=1
REQUIRES_APP=
Bob Mottram's avatar
Bob Mottram committed
TOX_PORT=33445
Bob Mottram's avatar
Bob Mottram committed

# upstream is https://github.com/TokTok/c-toxcore
Bob Mottram's avatar
Bob Mottram committed
TOXCORE_REPO="https://code.freedombone.net/bashrc/toxcore"
TOXCORE_COMMIT='7d399cedcfd20f0d91a8caf386ae3c63f4dcf285'
Bob Mottram's avatar
Bob Mottram committed

Bob Mottram's avatar
Bob Mottram committed
TOXID_REPO="https://code.freedombone.net/bashrc/toxid"
Bob Mottram's avatar
Bob Mottram committed
TOX_BOOTSTRAP_ID_FILE=/var/lib/tox-bootstrapd/pubkey.txt
# These are some default nodes, but you can replace them with trusted nodes
# as you prefer. See https://wiki.tox.im/Nodes
TOX_NODES=
#TOX_NODES=(
#  '192.254.75.102,2607:5600:284::2,33445,951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F,Tox RELENG,US'
#  '144.76.60.215,2a01:4f8:191:64d6::1,33445,04119E835DF3E78BACF0F84235B300546AF8B936F035185E2A8E9E0A67C8924F,sonOfRa,DE'
#)
TOXIC_REPO="https://github.com/Tox/toxic"
Bob Mottram's avatar
Bob Mottram committed
TOXIC_COMMIT='68ce17a57fd05599968a299e5dc516e183ebcf75'
Bob Mottram's avatar
Bob Mottram committed
TOXIC_FILE=/usr/local/bin/toxic

Bob Mottram's avatar
Bob Mottram committed
QTOX_REPO="https://code.freedombone.net/bashrc/qTox"
Bob Mottram's avatar
Bob Mottram committed
QTOX_COMMIT='origin/bashrc/freedombone'
Bob Mottram's avatar
Bob Mottram committed

Bob Mottram's avatar
Bob Mottram committed
tox_variables=(SYSTEM_TYPE
               TOXCORE_REPO
               MY_USERNAME
               ONION_ONLY
               INSTALLED_WITHIN_DOCKER
               TOX_PORT
               TOX_NODES)

function upgrade_distro_tox {
    target_distro="$1"

    if [[ "$target_distro" == 'buster' ]]; then
        echo -n ''
    fi
}

function logging_on_tox {
    echo -n ''
}

function logging_off_tox {
    echo -n ''
}

Bob Mottram's avatar
Bob Mottram committed
function remove_user_tox {
    remove_username="$1"

Bob Mottram's avatar
Bob Mottram committed
    if [ -d "/home/$remove_username/.config/tox" ]; then
        if [ -d "/home/$remove_username/.config/tox/chatlogs" ]; then
            rm -rf "/home/$remove_username/.config/tox/chatlogs"
Bob Mottram's avatar
Bob Mottram committed
        fi
Bob Mottram's avatar
Bob Mottram committed
        rm "/home/$remove_username/.config/tox/"*
Bob Mottram's avatar
Bob Mottram committed
function add_user_tox {
    new_username="$1"
Bob Mottram's avatar
Bob Mottram committed

    # Note: password isn't used
Bob Mottram's avatar
Bob Mottram committed
    #new_user_password="$2"
Bob Mottram's avatar
Bob Mottram committed

    USER_TOX_FILE=/home/${new_username}/.config/tox/data.tox
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f "$USER_TOX_FILE" ]; then
        mkdir -p "/home/${new_username}/.config/tox"
        chown -R "${new_username}":"${new_username}" "/home/${new_username}/.config"
        su -c "toxid -u ${new_username} -n data" - "$new_username"
        su -c "toxid --setuser ${new_username}" - "$new_username"
function run_client_tox {
    # create a tox user
    USER_TOX_FILE=/home/${USER}/.config/tox/data.tox
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f "$USER_TOX_FILE" ]; then
        mkdir -p "/home/${USER}/.config/tox"
        chown -R "${USER}":"${USER}" "/home/${USER}/.config"
        toxid -u "${USER}" -n data
        toxid --setuser "${USER}"
Bob Mottram's avatar
Bob Mottram committed
    toxic -f "$USER_TOX_FILE" --force-tcp --SOCKS5-proxy 127.0.0.1 9050
function install_interactive_tox {
    echo -n ''
Bob Mottram's avatar
Bob Mottram committed
function configure_interactive_tox {
    if [ ! -f $TOX_BOOTSTRAP_ID_FILE ]; then
        return
    fi
    bootstrap_id=$(cat $TOX_BOOTSTRAP_ID_FILE)
Bob Mottram's avatar
Bob Mottram committed
    dialog --title $"Tox Bootstrap Node ID" \
Bob Mottram's avatar
Bob Mottram committed
           --msgbox $"\\n$bootstrap_id\\n\\nTo copy this hold down the shift key, select the ID and then right click and copy." 10 70
function mesh_tox_qtox {
Bob Mottram's avatar
Bob Mottram committed
    # shellcheck disable=SC2154
    if [ ! "${rootdir}$INSTALL_DIR" ]; then
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "${rootdir}$INSTALL_DIR" ]; then
        mkdir -p "${rootdir}$INSTALL_DIR"
    # shellcheck disable=SC2086
    chroot "${rootdir}" $INSTALL_PACKAGES build-essential libatk1.0-0 libbz2-1.0 libc6 libcairo2 libdbus-1-3 libegl1-mesa libfontconfig1 libfreetype6 libgcc1 libgdk-pixbuf2.0-0 libgl1-mesa-glx libglib2.0-0 libgtk2.0-0 libice6 libicu57 libjpeg62-turbo libmng1 libmtdev1 libopenal1 libopus0 libpango-1.0-0 libpangocairo-1.0-0 libpangoft2-1.0-0 libpng16-16 libqrencode3 libsm6 libsodium18 libsqlite3-0 libssl1.1 libstdc++6 libtiff5 libudev1 libvpx4 libwayland-client0 libwayland-cursor0 libwayland-egl1-mesa libwebp6 libx11-6 libx11-xcb1 libxcb-glx0 libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-randr0 libxcb-render-util0 libxcb-render0 libxcb-shape0 libxcb-shm0 libxcb-sync1 libxcb-xfixes0 libxcb-xinerama0 libxcb1 libxext6 libxfixes3 libxi6 libxrender1 libxss1 zlib1g libopus-dev libvpx-dev
    # shellcheck disable=SC2086
    chroot "${rootdir}" $INSTALL_PACKAGES build-essential qt5-qmake qt5-default qttools5-dev-tools libqt5opengl5-dev libqt5svg5-dev libopenal-dev libxss-dev qrencode libqrencode-dev libglib2.0-dev libgdk-pixbuf2.0-dev libgtk2.0-dev libsqlcipher-dev libopus-dev libvpx-dev libavformat-dev libavdevice-dev libswscale-dev libavutil-dev libavcodec-dev libavcodec57 libavfilter-dev libavfilter6
Bob Mottram's avatar
Bob Mottram committed
    # shellcheck disable=SC2086
    chroot "$rootdir" $CLEAN_PACKAGES
    chroot "$rootdir" /bin/rm -rf /var/lib/apt/lists/*
Bob Mottram's avatar
Bob Mottram committed
    # shellcheck disable=SC2086
    chroot "$rootdir" $CLEAN_PACKAGES
Bob Mottram's avatar
Bob Mottram committed
    # ffmpeg
    # shellcheck disable=SC2086
    chroot "${rootdir}" $INSTALL_PACKAGES build-essential
    # shellcheck disable=SC2086
    chroot "${rootdir}" $INSTALL_PACKAGES ffmpeg libmp3lame-dev libvorbis-dev libtheora-dev
    # shellcheck disable=SC2086
    chroot "${rootdir}" $INSTALL_PACKAGES libspeex-dev yasm pkg-config libopenjp2-7-dev
    # shellcheck disable=SC2086
    chroot "${rootdir}" $INSTALL_PACKAGES libx264-dev mjpegtools libmjpegtools-dev libav-tools
    # shellcheck disable=SC2086
    chroot "${rootdir}" $INSTALL_PACKAGES build-essential cmake ffmpeg libexif-dev libgdk-pixbuf2.0-dev libglib2.0-dev libgtk2.0-dev libopenal-dev libqrencode-dev libqt5opengl5-dev libqt5svg5-dev libsqlcipher-dev libxss-dev pkg-config qrencode qt5-default qt5-qmake qttools5-dev qttools5-dev-tools yasm
    if [ -d /repos/qtox ]; then
Bob Mottram's avatar
Bob Mottram committed
        mkdir -p "${rootdir}$INSTALL_DIR/qtox"
Bob Mottram's avatar
Bob Mottram committed
        cp -r -p /repos/qtox/. "${rootdir}$INSTALL_DIR/qtox"
        cd "${rootdir}$INSTALL_DIR/qtox" || exit 26
        git pull
    else
Bob Mottram's avatar
Bob Mottram committed
        git clone "$QTOX_REPO" "${rootdir}$INSTALL_DIR/qtox"
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "${rootdir}$INSTALL_DIR/qtox" ]; then
    cd "${rootdir}${INSTALL_DIR}/qtox" || exit 23
    git checkout $QTOX_COMMIT -b $QTOX_COMMIT
Bob Mottram's avatar
Bob Mottram committed
    chroot "${rootdir}" /bin/bash -x <<EOF
cd ${INSTALL_DIR}/qtox
Bob Mottram's avatar
Bob Mottram committed
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig"
cmake .
make
make install
EOF
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f "${rootdir}/usr/local/bin/qtox" ]; then
Bob Mottram's avatar
Bob Mottram committed
    cp "${rootdir}/usr/local/bin/qtox" "${rootdir}/usr/bin/qtox"
function reconfigure_tox {
function upgrade_tox {
    function_check set_repo_commit
Bob Mottram's avatar
Bob Mottram committed
    set_repo_commit "$INSTALL_DIR/toxcore" "toxcore commit" "$TOXCORE_COMMIT" $TOXCORE_REPO
    if [[ $(commit_has_changed "$INSTALL_DIR/toxcore" "toxcore commit" "$TOXCORE_COMMIT") == "1" ]]; then
        cd "$INSTALL_DIR/toxcore" || exit 53
        sed -i 's|ExecStart=.*|ExecStart=/usr/local/bin/tox-bootstrapd --config /etc/tox-bootstrapd.conf|g' "$rootdir/etc/systemd/system/tox-bootstrapd.service"
Bob Mottram's avatar
Bob Mottram committed
        ./autogen.sh
        if [ ! -d "$INSTALL_DIR/toxcore/_build" ]; then
            mkdir "$INSTALL_DIR/toxcore/_build"
        fi
        cd "$INSTALL_DIR/toxcore/_build" || return
        cmake ..
        make
        make install
        systemctl daemon-reload
        systemctl restart tox-bootstrapd.service
    fi
Bob Mottram's avatar
Bob Mottram committed
    set_repo_commit "$INSTALL_DIR/toxic" "Toxic commit" "$TOXIC_COMMIT" $TOXIC_REPO
    if [[ $(commit_has_changed "$INSTALL_DIR/toxic" "Toxic commit" "$TOXIC_COMMIT") == "1" ]]; then
        cd "$INSTALL_DIR/toxic" || exit 46
        make
        make install
    fi
Bob Mottram's avatar
Bob Mottram committed
function backup_local_tox {
    if [ -d /var/lib/tox-bootstrapd ]; then
        echo $"Backing up Tox"
Bob Mottram's avatar
Bob Mottram committed

        if [ -d /var/lib/tox-bootstrapd ]; then
            cp /etc/tox-bootstrapd.conf /var/lib/tox-bootstrapd
            if [ -d /var/lib/tox-bootstrapd/Maildir ]; then
                rm -rf /var/lib/tox-bootstrapd/Maildir
            fi
        fi
Bob Mottram's avatar
Bob Mottram committed

        function_check backup_directory_to_usb
        backup_directory_to_usb /var/lib/tox-bootstrapd tox
Bob Mottram's avatar
Bob Mottram committed

        echo $"Backup of Tox complete"
    fi
function restore_local_tox {
Bob Mottram's avatar
Bob Mottram committed
    if [ -d "$USB_MOUNT/backup/tox" ]; then
        echo $"Restoring Tox node settings"
        function_check restore_directory_from_usb
        #restore_directory_from_usb / tox
Bob Mottram's avatar
Bob Mottram committed
        if ! restore_directory_from_usb /var/lib/tox-bootstrapd tox; then
            function_check set_user_permissions
            set_user_permissions
            function_check backup_unmount_drive
            backup_unmount_drive
        fi
        cp /var/lib/tox-bootstrapd/tox-bootstrapd.conf /etc/tox-bootstrapd.conf
Bob Mottram's avatar
Bob Mottram committed
        if ! systemctl restart tox-bootstrapd.service; then
            systemctl status tox-bootstrapd.service
            function_check set_user_permissions
            set_user_permissions
            function_check backup_unmount_drive
            backup_unmount_drive
Bob Mottram's avatar
Bob Mottram committed
function backup_remote_tox {
    echo -n ''
}

function restore_remote_tox {
    echo -n ''
function remove_tox_node {
Bob Mottram's avatar
Bob Mottram committed
    firewall_remove ${TOX_PORT}

    function_check remove_onion_service
    remove_onion_service tox ${TOX_PORT}

Bob Mottram's avatar
Bob Mottram committed
    if ! "${PROJECT_NAME}-mesh-install" -f tox_node --remove yes; then
        echo $'Failed to remove tox node'
Bob Mottram's avatar
Bob Mottram committed
    remove_completion_param install_tox_node
    remove_completion_param configure_firewall_for_tox
Bob Mottram's avatar
Bob Mottram committed
}

function remove_tox_avahi {
    cd "$INSTALL_DIR/toxid" || exit 82
Bob Mottram's avatar
Bob Mottram committed
    rm -rf "$INSTALL_DIR/toxid"
    sed -i '/tox_avahi/d' "$COMPLETION_FILE"
Bob Mottram's avatar
Bob Mottram committed
}

function remove_tox_client {
Bob Mottram's avatar
Bob Mottram committed
    if ! "${PROJECT_NAME}-mesh-install" -f tox_client --remove yes; then
        echo $'Could not remove Tox client'
Bob Mottram's avatar
Bob Mottram committed
    sed -i '/install_tox_client/d' "$COMPLETION_FILE"
    sed -i '/Tox /d' "$COMPLETION_FILE"
    sed -i '/Toxic /d' "$COMPLETION_FILE"
Bob Mottram's avatar
Bob Mottram committed
}

function remove_tox {
    remove_tox_client
    remove_tox_avahi
    remove_tox_node
function configure_firewall_for_tox {
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$INSTALLING_MESH" ]; then
Bob Mottram's avatar
Bob Mottram committed
        if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
Bob Mottram's avatar
Bob Mottram committed
            return
        fi
    fi

    if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
        # docker does its own firewalling
        return
    fi
    if [[ $ONION_ONLY != "no" ]]; then
        return
    fi

Bob Mottram's avatar
Bob Mottram committed
    TOX_PORT_MAIN=$(grep "TOX_PORT=" "/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-tox" | head -n 1 | awk -F '=' '{print $2}')
    if [ ${#TOX_PORT_MAIN} -gt 2 ]; then
        TOX_PORT=$TOX_PORT_MAIN
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$TOX_PORT" ]; then
        echo $'No Tox port was specified'
Bob Mottram's avatar
Bob Mottram committed
    firewall_add Tox "${TOX_PORT}"
Bob Mottram's avatar
Bob Mottram committed
    mark_completed "${FUNCNAME[0]}"
Bob Mottram's avatar
Bob Mottram committed
function tox_avahi {
Bob Mottram's avatar
Bob Mottram committed
    if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
        return
    fi

    if [ ! -d /etc/avahi ]; then
        echo $'tox_avahi: avahi is not installed'
    fi

    # install a command to obtain the Tox ID
    cd "$INSTALL_DIR" || exit 13

    if [ -d /repos/toxid ]; then
Bob Mottram's avatar
Bob Mottram committed
        mkdir -p "$INSTALL_DIR/toxid"
Bob Mottram's avatar
Bob Mottram committed
        cp -r -p /repos/toxid/. "$INSTALL_DIR/toxid"
        cd "$INSTALL_DIR/toxid" || exit 46
        git pull
    else
        function_check git_clone
Bob Mottram's avatar
Bob Mottram committed
        git_clone "$TOXID_REPO" "$INSTALL_DIR/toxid"
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "$INSTALL_DIR/toxid" ]; then
    cd "$INSTALL_DIR/toxid" || exit 47
Bob Mottram's avatar
Bob Mottram committed
    if ! make; then
Bob Mottram's avatar
Bob Mottram committed
    if [[ $SYSTEM_TYPE == "mesh"* ]]; then
Bob Mottram's avatar
Bob Mottram committed
        toxavahi
Bob Mottram's avatar
Bob Mottram committed
        # publish regularly
        function_check cron_add_mins
        cron_add_mins 1 'toxavahi 2> /dev/null'
    fi
Bob Mottram's avatar
Bob Mottram committed
    mark_completed "${FUNCNAME[0]}"
Bob Mottram's avatar
Bob Mottram committed
}

function install_tox_node {
Bob Mottram's avatar
Bob Mottram committed
    if [[ $(app_is_installed tox_node) == "1" ]]; then
        return
    fi

    function_check mesh_tox_node
    mesh_tox_node

Bob Mottram's avatar
Bob Mottram committed
    # onion address for bootstrapping
Bob Mottram's avatar
Bob Mottram committed
    add_onion_service tox "${TOX_PORT}" "${TOX_PORT}"

    systemctl restart tox-bootstrapd.service

Bob Mottram's avatar
Bob Mottram committed
    TOX_PUBLIC_KEY=$(grep tox /var/log/syslog | grep "Public Key" | tail -n 1 | awk -F ' ' '{print $8}')
    if [ ${#TOX_PUBLIC_KEY} -lt 30 ]; then
        echo $'Could not obtain the tox node public key'
    fi

    # save the public key for later reference
    echo "$TOX_PUBLIC_KEY" > $TOX_BOOTSTRAP_ID_FILE

    function_check configure_firewall_for_tox
    configure_firewall_for_tox

    function_check configure_firewall_for_tox
    configure_firewall_for_tox
Bob Mottram's avatar
Bob Mottram committed
    install_completed tox_node
Bob Mottram's avatar
Bob Mottram committed
}

function install_tox_client {
Bob Mottram's avatar
Bob Mottram committed
    if [[ $(app_is_installed tox_client) == "1" ]]; then
Bob Mottram's avatar
Bob Mottram committed

    function_check mesh_tox_client
    mesh_tox_client
Bob Mottram's avatar
Bob Mottram committed

Bob Mottram's avatar
Bob Mottram committed
    install_completed tox_client
function mesh_tox_node {
    # obtain commits from the main file
Bob Mottram's avatar
Bob Mottram committed
    TOXCORE_COMMIT_MAIN=$(grep "TOXCORE_COMMIT=" "/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-tox" | head -n 1 | awk -F "'" '{print $2}')
    if [ ${#TOXCORE_COMMIT_MAIN} -gt 10 ]; then
        TOXCORE_COMMIT=$TOXCORE_COMMIT_MAIN
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$TOXCORE_COMMIT" ]; then
        echo $'No Tox commit was specified'
Bob Mottram's avatar
Bob Mottram committed
    TOXID_REPO_MAIN=$(grep "TOXID_REPO=" "/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-tox" | head -n 1 | awk -F '"' '{print $2}')
    if [ ${#TOXID_REPO_MAIN} -gt 5 ]; then
        TOXID_REPO=$TOXID_REPO_MAIN
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$TOXID_REPO" ]; then
        echo $'No ToxID repo was specified'
Bob Mottram's avatar
Bob Mottram committed
    TOX_PORT_MAIN=$(grep "TOX_PORT=" "/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-tox" | head -n 1 | awk -F '=' '{print $2}')
    if [ ${#TOX_PORT_MAIN} -gt 2 ]; then
        TOX_PORT=$TOX_PORT_MAIN
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$TOX_PORT" ]; then
        echo $'No Tox port was specified'
Bob Mottram's avatar
Bob Mottram committed
    TOXCORE_REPO_MAIN=$(grep "TOXCORE_REPO=" "/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-tox" | head -n 1 | awk -F '"' '{print $2}')
    if [ ${#TOXCORE_REPO_MAIN} -gt 10 ]; then
        TOXCORE_REPO=$TOXCORE_REPO_MAIN
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$TOXCORE_REPO" ]; then
        echo $'No Tox repo was specified'
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$TOXCORE_COMMIT" ]; then
        echo $'No Tox commit was specified'
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$TOXCORE_REPO" ]; then
        echo $'No Tox repo was specified'
Bob Mottram's avatar
Bob Mottram committed
    if [ "$rootdir" ]; then
        # shellcheck disable=SC2086
        chroot "${rootdir}" $INSTALL_PACKAGES build-essential libtool autotools-dev
        # shellcheck disable=SC2086
        chroot "${rootdir}" $INSTALL_PACKAGES automake checkinstall check git yasm
        # shellcheck disable=SC2086
        chroot "${rootdir}" $INSTALL_PACKAGES libsodium18 libsodium-dev libcap2-bin
        # shellcheck disable=SC2086
        chroot "${rootdir}" $INSTALL_PACKAGES libconfig9 libconfig-dev autoconf
        # shellcheck disable=SC2086
        chroot "${rootdir}" $INSTALL_PACKAGES libopus-dev libvpx-dev cmake
        $INSTALL_PACKAGES build-essential libtool autotools-dev
        $INSTALL_PACKAGES automake checkinstall check git yasm
        $INSTALL_PACKAGES libsodium18 libsodium-dev libcap2-bin
        $INSTALL_PACKAGES libconfig9 libconfig-dev autoconf
        $INSTALL_PACKAGES libopus-dev libvpx-dev cmake
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "${rootdir}${INSTALL_DIR}" ]; then
        mkdir -p "${rootdir}${INSTALL_DIR}"
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "${rootdir}${INSTALL_DIR}/toxcore" ]; then
        if [ -d /repos/toxcore ]; then
Bob Mottram's avatar
Bob Mottram committed
            mkdir -p "${rootdir}${INSTALL_DIR}/toxcore"
Bob Mottram's avatar
Bob Mottram committed
            cp -r -p /repos/toxcore/. "${rootdir}${INSTALL_DIR}/toxcore"
            cd "${rootdir}${INSTALL_DIR}/toxcore" || exit 24
            git pull
        else
Bob Mottram's avatar
Bob Mottram committed
            if ! git clone "${TOXCORE_REPO}" "${rootdir}${INSTALL_DIR}/toxcore"; then
    cd "${rootdir}$INSTALL_DIR/toxcore" || exit 46
Bob Mottram's avatar
Bob Mottram committed
    git checkout "$TOXCORE_COMMIT" -b "$TOXCORE_COMMIT"
Bob Mottram's avatar
Bob Mottram committed
    if [ "${rootdir}" ]; then
        chroot "${rootdir}" /bin/bash -x <<EOF
Bob Mottram's avatar
Bob Mottram committed
cd ${INSTALL_DIR}/toxcore
Bob Mottram's avatar
Bob Mottram committed
./autogen.sh
mkdir _build
cd _build || exit 1
cmake ..
Bob Mottram's avatar
Bob Mottram committed
make
make install
EOF
Bob Mottram's avatar
Bob Mottram committed
cd ${INSTALL_DIR}/toxcore
Bob Mottram's avatar
Bob Mottram committed
./autogen.sh
mkdir _build
cd _build || exit 1
cmake ..
Bob Mottram's avatar
Bob Mottram committed
make
make install
EOF
Bob Mottram's avatar
Bob Mottram committed
    # shellcheck disable=SC2086
Bob Mottram's avatar
Bob Mottram committed
    cp -l $rootdir/usr/local/lib/libtoxcore* "$rootdir/usr/lib/"
Bob Mottram's avatar
Bob Mottram committed
    cp "${rootdir}${INSTALL_DIR}/toxcore/other/bootstrap_daemon/tox-bootstrapd.service" "$rootdir/etc/systemd/system/"
    sed -i 's|ExecStart=.*|ExecStart=/usr/local/bin/tox-bootstrapd --config /etc/tox-bootstrapd.conf|g' "$rootdir/etc/systemd/system/tox-bootstrapd.service"
    if [ "${rootdir}" ]; then
        chroot "${rootdir}" systemctl enable tox-bootstrapd.service
    else
        systemctl enable tox-bootstrapd.service
    fi

Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f "$rootdir/usr/local/bin/tox-bootstrapd" ]; then
Bob Mottram's avatar
Bob Mottram committed
        echo $"Toxcore compile failed at $((duration / 60)) minutes and $((duration % 60)) seconds elapsed."
        echo $'Unable to make toxcore'
Bob Mottram's avatar
Bob Mottram committed
    echo $"Toxcore compile $((duration / 60)) minutes and $((duration % 60)) seconds elapsed."
Bob Mottram's avatar
Bob Mottram committed
    if [ "${rootdir}" ]; then
        chroot "${rootdir}" /usr/sbin/useradd --home-dir /var/lib/tox-bootstrapd --create-home --system --shell /sbin/nologin --comment $"Account to run Tox's DHT bootstrap daemon" --user-group tox-bootstrapd
        chroot "${rootdir}" /bin/chmod 700 /var/lib/tox-bootstrapd
        chmod 600 /etc/shadow
        chmod 600 /etc/gshadow
        useradd --home-dir /var/lib/tox-bootstrapd --create-home --system --shell /sbin/nologin --comment $"Account to run Tox's DHT bootstrap daemon" --user-group tox-bootstrapd
        chmod 0000 /etc/shadow
        chmod 0000 /etc/gshadow
        chmod 700 /var/lib/tox-bootstrapd
    fi

    # remove Maildir
Bob Mottram's avatar
Bob Mottram committed
    if [ -d "$rootdir/var/lib/tox-bootstrapd/Maildir" ]; then
        rm -rf "$rootdir/var/lib/tox-bootstrapd/Maildir"
    fi

    # create configuration file
    TOX_BOOTSTRAP_CONFIG=$rootdir/etc/tox-bootstrapd.conf
Bob Mottram's avatar
Bob Mottram committed
    { echo "port = $TOX_PORT";
      echo 'keys_file_path = "/var/lib/tox-bootstrapd/keys"';
      echo 'pid_file_path = "/var/run/tox-bootstrapd/tox-bootstrapd.pid"';
      echo 'enable_ipv6 = true';
      echo 'enable_ipv4_fallback = true';
      echo 'enable_lan_discovery = true';
      echo 'enable_tcp_relay = true';
      echo "tcp_relay_ports = [443, 3389, $TOX_PORT]";
      echo 'enable_motd = true';
      echo 'motd = "tox-bootstrapd"'; } > "$TOX_BOOTSTRAP_CONFIG"
Bob Mottram's avatar
Bob Mottram committed
        echo 'bootstrap_nodes = (' >> "$TOX_BOOTSTRAP_CONFIG"
        toxcount=0
        while [ "x${TOX_NODES[toxcount]}" != "x" ]
        do
Bob Mottram's avatar
Bob Mottram committed
            # shellcheck disable=SC2102
            nodes_str=$(echo $TOX_NODES[toxcount])
            toxval_ipv4=$(awk "$nodes_str" -F ',' '{print $1}')
            toxval_ipv6=$(awk "$nodes_str" -F ',' '{print $2}')
            toxval_port=$(awk "$nodes_str" -F ',' '{print $3}')
            toxval_pubkey=$(awk "$nodes_str" -F ',' '{print $4}')
            toxval_maintainer=$(awk "$nodes_str" -F ',' '{print $5}')
            echo "{ // $toxval_maintainer" >> "$TOX_BOOTSTRAP_CONFIG"
            if [[ $toxval_ipv6 != 'NONE' ]]; then
Bob Mottram's avatar
Bob Mottram committed
                echo "  address = \"$toxval_ipv6\"" >> "$TOX_BOOTSTRAP_CONFIG"
Bob Mottram's avatar
Bob Mottram committed
                echo "  address = \"$toxval_ipv4\"" >> "$TOX_BOOTSTRAP_CONFIG"
Bob Mottram's avatar
Bob Mottram committed
            echo "  port = $toxval_port" >> "$TOX_BOOTSTRAP_CONFIG"
            echo "  public_key = \"$toxval_pubkey\"" >> "$TOX_BOOTSTRAP_CONFIG"
            toxcount=$((toxcount + 1))
            if [ "x${TOX_NODES[toxcount]}" != "x" ]; then
Bob Mottram's avatar
Bob Mottram committed
                echo "}," >> "$TOX_BOOTSTRAP_CONFIG"
Bob Mottram's avatar
Bob Mottram committed
                echo "}" >> "$TOX_BOOTSTRAP_CONFIG"
Bob Mottram's avatar
Bob Mottram committed
        echo ')' >> "$TOX_BOOTSTRAP_CONFIG"
Bob Mottram's avatar
Bob Mottram committed
    if [ -f "$rootdir/var/lib/tox-bootstrapd/keys" ]; then
        chmod 700 "$rootdir/var/lib/tox-bootstrapd/keys"
}

function mesh_tox_avahi {
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "$rootdir/etc/avahi" ]; then
        echo $'tox_avahi: avahi is not installed'
Bob Mottram's avatar
Bob Mottram committed
    if [ ! "$TOXID_REPO" ]; then
        echo $'No ToxID repo was specified'
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "${rootdir}${INSTALL_DIR}" ]; then
        mkdir -p "${rootdir}${INSTALL_DIR}"
    if [ -d /repos/toxid ]; then
Bob Mottram's avatar
Bob Mottram committed
        mkdir -p "${rootdir}${INSTALL_DIR}/toxid"
Bob Mottram's avatar
Bob Mottram committed
        cp -r -p /repos/toxid/. "${rootdir}${INSTALL_DIR}/toxid"
        cd "${rootdir}${INSTALL_DIR}/toxid" || exit 24
        git pull
    else
Bob Mottram's avatar
Bob Mottram committed
        git clone "${TOXID_REPO}" "${rootdir}${INSTALL_DIR}/toxid"
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "${rootdir}${INSTALL_DIR}/toxid" ]; then
        echo $'Unable to clone toxid repo'
Bob Mottram's avatar
Bob Mottram committed
    if [ "${rootdir}" ]; then
        chroot "${rootdir}" /bin/bash -x <<EOF
Bob Mottram's avatar
Bob Mottram committed
cd ${INSTALL_DIR}/toxid
make
make install
EOF
Bob Mottram's avatar
Bob Mottram committed
cd ${INSTALL_DIR}/toxid
make
make install
EOF
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f "$rootdir/usr/local/bin/toxid" ]; then
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f "$rootdir/usr/local/bin/toxavahi" ]; then
    MESH_SYNC_COMMAND=$rootdir/usr/bin/mesh-sync
Bob Mottram's avatar
Bob Mottram committed
    { echo '#!/bin/bash';
      echo '/usr/local/bin/toxavahi 2> /dev/null';
      echo '/usr/local/bin/meshavahi 2> /dev/null'; } > "$MESH_SYNC_COMMAND"
    chmod +x "$MESH_SYNC_COMMAND"
Bob Mottram's avatar
Bob Mottram committed
    if ! grep -q "mesh-sync" "${rootdir}/etc/crontab"; then
        { echo "*/1            * *   *   *   root /usr/bin/mesh-sync 2> /dev/null";
          echo "*/1            * *   *   *   root ( sleep 20 ; /usr/bin/mesh-sync 2> /dev/null )";
          echo "*/1            * *   *   *   root ( sleep 40 ; /usr/bin/mesh-sync 2> /dev/null )"; } >> "${rootdir}/etc/crontab"
Bob Mottram's avatar
Bob Mottram committed
    fi
}

function mesh_tox_client {
Bob Mottram's avatar
Bob Mottram committed

Bob Mottram's avatar
Bob Mottram committed
    TOXIC_FILE=$(grep "TOXIC_FILE=" "/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-tox" | head -n 1 | awk -F '=' '{print $2}')

    # obtain commits from the main file
Bob Mottram's avatar
Bob Mottram committed
    TOXIC_COMMIT_MAIN=$(grep "TOXIC_COMMIT=" "/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-tox" | head -n 1 | awk -F "'" '{print $2}')
    if [ ${#TOXIC_COMMIT_MAIN} -gt 10 ]; then
        TOXIC_COMMIT=$TOXIC_COMMIT_MAIN
    fi

Bob Mottram's avatar
Bob Mottram committed
    TOXIC_REPO_MAIN=$(grep "TOXIC_REPO=" "/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-tox" | head -n 1 | awk -F '"' '{print $2}')
    if [ ${#TOXIC_REPO_MAIN} -gt 5 ]; then
        TOXIC_REPO=$TOXIC_REPO_MAIN
    fi

Bob Mottram's avatar
Bob Mottram committed
    if [ "${rootdir}" ]; then
        # shellcheck disable=SC2086
        chroot "${rootdir}" $INSTALL_PACKAGES libncursesw5-dev libconfig-dev libqrencode-dev
        # shellcheck disable=SC2086
        chroot "${rootdir}" $INSTALL_PACKAGES libcurl4-openssl-dev libvpx-dev libopenal-dev
        # shellcheck disable=SC2086
        chroot "${rootdir}" $INSTALL_PACKAGES libqrencode-dev libpng-dev libncurses5-dev libalut-dev
        # shellcheck disable=SC2086
        chroot "${rootdir}" $INSTALL_PACKAGES libnotify-dev python3-dev
        toxic_disable_notify=0
        $INSTALL_PACKAGES libncursesw5-dev libconfig-dev libqrencode-dev
        $INSTALL_PACKAGES libcurl4-openssl-dev libvpx-dev libopenal-dev
        $INSTALL_PACKAGES libqrencode-dev libpng-dev libncurses5-dev libalut-dev python3-dev
        toxic_disable_notify=1
    fi

    TEMP_SCRIPT_NAME=fbtmp728353.sh
    TEMP_SCRIPT=/tmp/$TEMP_SCRIPT_NAME
Bob Mottram's avatar
Bob Mottram committed
    { echo '#!/bin/bash';
      echo "mkdir -p $INSTALL_DIR";
      echo 'if [ -d /repos/toxic ]; then';
Bob Mottram's avatar
Bob Mottram committed
      echo "    mkdir -p $INSTALL_DIR/toxic";
Bob Mottram's avatar
Bob Mottram committed
      echo "    cp -r -p /repos/toxic/. $INSTALL_DIR/toxic";
      echo "    cd $INSTALL_DIR/toxic";
      echo '    git pull';
      echo 'else';
      echo "    git clone $TOXIC_REPO $INSTALL_DIR/toxic";
      echo 'fi';
      echo "cd $INSTALL_DIR/toxic";
      echo "git checkout $TOXIC_COMMIT -b $TOXIC_COMMIT";
      echo "export DISABLE_DESKTOP_NOTIFY=$toxic_disable_notify";
      echo "export DISABLE_AV=$toxic_disable_notify";
      echo "export DISABLE_X11=$toxic_disable_notify";
      echo "export DISABLE_SOUND_NOTIFY=$toxic_disable_notify";
Bob Mottram's avatar
Bob Mottram committed
      echo 'if ! make; then';
Bob Mottram's avatar
Bob Mottram committed
      echo '    exit 1';
      echo 'fi';
      echo 'make install';
      echo 'exit 0'; } > "$TEMP_SCRIPT"
Bob Mottram's avatar
Bob Mottram committed
    cp "$TEMP_SCRIPT" "$rootdir/root/"
Bob Mottram's avatar
Bob Mottram committed
    TOXIC_FILE=$(grep "TOXIC_FILE=" "/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-tox" | head -n 1 | awk -F '=' '{print $2}')
Bob Mottram's avatar
Bob Mottram committed
    if [ "${rootdir}" ]; then
        chroot "${rootdir}" "/root/$TEMP_SCRIPT_NAME"
Bob Mottram's avatar
Bob Mottram committed
    # shellcheck disable=SC2181
Bob Mottram's avatar
Bob Mottram committed
        cat -n /root/fbtmp728353.sh
Bob Mottram's avatar
Bob Mottram committed
        echo $"Toxic client compile failed at $((duration / 60)) minutes and $((duration % 60)) seconds elapsed."
        echo $'Unable to make tox client'
        rm $TEMP_SCRIPT
        exit 74872
    fi
    rm $TEMP_SCRIPT
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f "$rootdir$TOXIC_FILE" ]; then
        echo $"Tox client was not installed to $TOXIC_FILE"
        exit 63278
    fi
    duration=$SECONDS
Bob Mottram's avatar
Bob Mottram committed
    echo $"Toxic client compile $((duration / 60)) minutes and $((duration % 60)) seconds elapsed."
}

function enable_tox_repo {
Bob Mottram's avatar
Bob Mottram committed
    echo 'deb http://download.opensuse.org/repositories/home:/antonbatenev:/tox/Debian_9.0/ /' > "$rootdir/etc/apt/sources.list.d/tox.list"
Bob Mottram's avatar
Bob Mottram committed
    cat >> "$rootdir/root/gettoxkey.sh" <<EOF
Bob Mottram's avatar
Bob Mottram committed
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
wget -q http://download.opensuse.org/repositories/home:antonbatenev:tox/Debian_9.0/Release.key -O- > /root/tox.key
Bob Mottram's avatar
Bob Mottram committed
apt-key add /root/tox.key
rm /root/tox.key
EOF
    chroot "$rootdir" chmod +x /root/gettoxkey.sh
    chroot "$rootdir" /root/gettoxkey.sh
Bob Mottram's avatar
Bob Mottram committed
    # shellcheck disable=SC2086
    chroot "$rootdir" $UPDATE_PACKAGES
    echo "Tox Repository Installed."
function install_tox {
Bob Mottram's avatar
Bob Mottram committed
    if [ "$INSTALLING_MESH" ]; then
        mesh_tox_node
        mesh_tox_avahi
        mesh_tox_client
    else
        avoid_tor_restart=
Bob Mottram's avatar
Bob Mottram committed
        if [ -f "$IMAGE_PASSWORD_FILE" ]; then
            if [[ $ONION_ONLY != 'no' ]]; then
                avoid_tor_restart=1
        fi

        if [ $avoid_tor_restart ]; then
Bob Mottram's avatar
Bob Mottram committed
            "${PROJECT_NAME}-logging" on --onion
Bob Mottram's avatar
Bob Mottram committed
            "${PROJECT_NAME}-logging" on
        fi

        install_tox_node

        if [ $avoid_tor_restart ]; then
Bob Mottram's avatar
Bob Mottram committed
            "${PROJECT_NAME}-logging" off --onion
Bob Mottram's avatar
Bob Mottram committed
            "${PROJECT_NAME}-logging" off
Bob Mottram's avatar
Bob Mottram committed
# NOTE: deliberately no exit 0