#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Profanity XMPP client
#
# License
# =======
#
# Copyright (C) 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 chat'

IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=1

LIBMESODE_REPO="https://github.com/boothj5/libmesode"
LIBMESODE_COMMIT='b91872cf7e7ed4d2443ab5c622f4cdb395d64dbe'

PROFANITY_REPO="https://github.com/boothj5/profanity"
PROFANITY_COMMIT='f8b855b09f2c4e9b461b0b7854afabbecf6d5b4a'

PROFANITY_OMEMO_PLUGIN_REPO="https://github.com/ReneVolution/profanity-omemo-plugin"
PROFANITY_OMEMO_PLUGIN_COMMIT='78be0c8367c6379829986755c0d1da287c031234'

xmpp_variables=(ONION_ONLY
                INSTALLED_WITHIN_DOCKER
                MY_USERNAME
                DEFAULT_DOMAIN_NAME
                XMPP_DOMAIN_CODE)

function logging_on_profanity {
    echo -n ''
}

function logging_off_profanity {
    echo -n ''
}

function remove_user_profanity {
    remove_username="$1"
}

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

    echo '0'
}

function run_client_profanity {
    torify profanity
}

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

function change_password_profanity {
    curr_username="$1"
    new_user_password="$2"

    read_config_param DEFAULT_DOMAIN_NAME

    ${PROJECT_NAME}-pass -u $curr_username -a xmpp -p "$new_user_password"

    # TODO: this is currently interactive. Really there needs to be a
    # non-interactive password change option for prosodyctl
    clear
    echo ''
    echo $'Currently Prosody requires password changes to be done interactively'
    prosodyctl passwd ${curr_username}@${DEFAULT_DOMAIN_NAME}

    XMPP_CLIENT_DIR=/home/$curr_username/.local/share/profanity
    XMPP_CLIENT_ACCOUNTS=$XMPP_CLIENT_DIR/accounts
    if [ -f $XMPP_CLIENT_ACCOUNTS ]; then
        sed -i "s|password=.*|password=$new_user_password|g" $XMPP_CLIENT_ACCOUNTS
    fi
}

function reconfigure_profanity {
    echo -n ''
}

function upgrade_profanity {
    # update profanity client
    if [ -f /usr/bin/profanity ]; then
        apt-get -y remove --purge profanity
    fi

    rm -rf /tmp/*

    CURR_LIBMESODE_COMMIT=$(cat $COMPLETION_FILE | grep "libmesode commit" | awk -F ':' '{print $2}')
    if [[ "$CURR_LIBMESODE_COMMIT" != "$LIBMESODE_COMMIT" ]]; then
        function_check set_repo_commit
        set_repo_commit $INSTALL_DIR/libmesode "libmesode commit" "$LIBMESODE_COMMIT" $LIBMESODE_REPO
        cd $INSTALL_DIR/libmesode
        ./bootstrap.sh
        ./configure
        make
        make install
        cp /usr/local/lib/libmesode* /usr/lib
    fi

    rm -rf /tmp/*

    CURR_PROFANITY_COMMIT=$(cat $COMPLETION_FILE | grep "profanity commit" | awk -F ':' '{print $2}')
    if [[ "$CURR_PROFANITY_COMMIT" != "$PROFANITY_COMMIT" ]]; then
        function_check set_repo_commit
        set_repo_commit $INSTALL_DIR/profanity "profanity commit" "$PROFANITY_COMMIT" $PROFANITY_REPO
        cd $INSTALL_DIR/profanity
        ./bootstrap.sh
        ./configure --disable-notifications --disable-icons --enable-otr --enable-pgp --enable-plugins --enable-c-plugins --enable-python-plugins --without-xscreensaver
        make
        make install
    fi

    CURR_PROFANITY_OMEMO_PLUGIN_COMMIT=$(cat $COMPLETION_FILE | grep "profanity omemo plugin commit" | awk -F ':' '{print $2}')
    if [[ "$CURR_PROFANITY_OMEMO_PLUGIN_COMMIT" != "$PROFANITY_OMEMO_PLUGIN_COMMIT" ]]; then
        # upgrade omemo plugins for all users
        set_repo_commit $INSTALL_DIR/profanity-omemo-plugin "profanity omemo plugin commit" "$PROFANITY_OMEMO_PLUGIN_COMMIT" $PROFANITY_OMEMO_PLUGIN_REPO
        cd $INSTALL_DIR/profanity-omemo-plugin
        sed -i 's|python setup.py|python2.7 setup.py|g' $INSTALL_DIR/profanity-omemo-plugin/install.sh
        pip uninstall -y profanity-omemo-plugin
        ./install.sh
        for d in /home/*/ ; do
            USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
            if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
                if [ ! -d /home/$USERNAME/.local/share/profanity/plugins ]; then
                    mkdir -p /home/$USERNAME/.local/share/profanity/plugins
                fi
                if [ -f $INSTALL_DIR/profanity-omemo-plugin/omemo.py ]; then
                    rm $INSTALL_DIR/profanity-omemo-plugin/omemo.*
                fi
                cp $INSTALL_DIR/profanity-omemo-plugin/deploy/prof_omemo_plugin.py /home/$USERNAME/.local/share/profanity/plugins
                chown -R $USERNAME:$USERNAME /home/$USERNAME/.local
            fi
        done
        if [ -f /etc/skel/.local/share/profanity/plugins/omemo.py ]; then
            rm /etc/skel/.local/share/profanity/plugins/omemo.*
        fi
        cp $INSTALL_DIR/profanity-omemo-plugin/deploy/prof_omemo_plugin.py /etc/skel/.local/share/profanity/plugins
    fi

    chmod -R 700 /root/.local/lib/python3.4/site-packages/python_axolotl_*
}

function backup_local_profanity {
    echo -n ''
}

function restore_local_profanity {
    echo -n ''
}

function backup_remote_profanity {
    echo -n ''
}

function restore_remote_profanity {
    echo -n ''
}

function remove_profanity {
    if [ -f /usr/local/bin/profanity ]; then
        cd $INSTALL_DIR/profanity
        make uninstall

        remove_completion_param install_profanity
        sed -i '/profanity/d' $COMPLETION_FILE
    fi
}

function install_profanity {
    # xmpp must already be installed
    if [ ! -d /etc/prosody ]; then
        return
    fi
    # install profanity from source in order to get OMEMO support
    if [ ! -d $INSTALL_DIR ]; then
        mkdir -p $INSTALL_DIR
    fi

    apt-get -yq install automake autoconf autoconf-archive libtool build-essential
    apt-get -yq install libncursesw5-dev libglib2.0-dev libcurl3-dev sqlite3
    apt-get -yq install libotr5-dev libgpgme11-dev python-dev libreadline-dev

    # dependency for profanity not available in debian
    if [ -d /repos/libmesode ]; then
        mkdir $INSTALL_DIR/libmesode
        cp -r -p /repos/libmesode/. $INSTALL_DIR/libmesode
        cd $INSTALL_DIR/libmesode
        git pull
    else
        git_clone $LIBMESODE_REPO $INSTALL_DIR/libmesode
    fi

    cd $INSTALL_DIR/libmesode
    git checkout $LIBMESODE_COMMIT -b $LIBMESODE_COMMIT
    ./bootstrap.sh
    ./configure
    make
    make install
    cp /usr/local/lib/libmesode* /usr/lib

    # build profanity
    if [ -d /repos/profanity ]; then
        mkdir $INSTALL_DIR/profanity
        cp -r -p /repos/profanity/. $INSTALL_DIR/profanity
        cd $INSTALL_DIR/profanity
        git pull
    else
        git_clone $PROFANITY_REPO $INSTALL_DIR/profanity
    fi

    cd $INSTALL_DIR/profanity
    git checkout $PROFANITY_COMMIT -b $PROFANITY_COMMIT
    ./bootstrap.sh
    ./configure --disable-notifications --disable-icons --enable-otr --enable-pgp --enable-plugins --enable-c-plugins --enable-python-plugins --without-xscreensaver
    make
    make install

    if [ ! -f /usr/local/bin/profanity ]; then
        echo $'Unable to build profanity'
        exit 7825272
    fi

    # install the omemo plugin
    apt-get -yq install python-pip python-setuptools clang libffi-dev libssl-dev python-dev

    if [ -d /repos/profanity-omemo ]; then
        mkdir $INSTALL_DIR/profanity-omemo-plugin
        cp -r -p /repos/profanity-omemo/. $INSTALL_DIR/profanity-omemo-plugin
        cd $INSTALL_DIR/profanity-omemo-plugin
        git pull
    else
        git_clone $PROFANITY_OMEMO_PLUGIN_REPO $INSTALL_DIR/profanity-omemo-plugin
    fi

    cd $INSTALL_DIR/profanity-omemo-plugin
    git checkout $PROFANITY_OMEMO_PLUGIN_COMMIT -b $PROFANITY_OMEMO_PLUGIN_COMMIT
    if [ ! -f $INSTALL_DIR/profanity-omemo-plugin/deploy/prof_omemo_plugin.py ]; then
        echo $'prof_omemo_plugin.py not found'
        exit 389225
    fi
    sed -i 's|python setup.py|python2.7 setup.py|g' $INSTALL_DIR/profanity-omemo-plugin/install.sh
    ./install.sh

    mkdir -p /etc/skel/.local/share/profanity/plugins
    cp $INSTALL_DIR/profanity-omemo-plugin/deploy/prof_omemo_plugin.py /etc/skel/.local/share/profanity/plugins
    cp $INSTALL_DIR/profanity-omemo-plugin/deploy/prof_omemo_plugin.py /home/$MY_USERNAME/.local/share/profanity/plugins
    chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.local

    XMPP_CLIENT_DIR=/home/$MY_USERNAME/.local/share/profanity
    XMPP_CLIENT_ACCOUNTS=$XMPP_CLIENT_DIR/accounts
    if [ ! -d $XMPP_CLIENT_DIR ]; then
        mkdir -p $XMPP_CLIENT_DIR
    fi

    XMPP_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_xmpp/hostname)
    MY_GPG_PUBLIC_KEY_ID=$(gpg_pubkey_from_email "$MY_USERNAME" "$MY_USERNAME@$DEFAULT_DOMAIN_NAME")

    if [[ $ONION_ONLY == 'no' ]]; then
        echo "[${MY_USERNAME}@${DEFAULT_DOMAIN_NAME}]" > $XMPP_CLIENT_ACCOUNTS
        echo 'enabled=true' >> $XMPP_CLIENT_ACCOUNTS
        echo "jid=${MY_USERNAME}@${DEFAULT_DOMAIN_NAME}" >> $XMPP_CLIENT_ACCOUNTS
        echo "server=$XMPP_ONION_HOSTNAME" >> $XMPP_CLIENT_ACCOUNTS
        # There is a bug where profanity doesn't refresh the screen
        # after gpg-agent has asked for a password, so for now
        # don't set the gpg key by default
        #echo "pgp.keyid=$MY_GPG_PUBLIC_KEY_ID" >> $XMPP_CLIENT_ACCOUNTS
        echo "pgp.keyid=" >> $XMPP_CLIENT_ACCOUNTS
        echo 'resource=profanity' >> $XMPP_CLIENT_ACCOUNTS
        echo "muc.service=chat.${DEFAULT_DOMAIN_NAME}" >> $XMPP_CLIENT_ACCOUNTS
        echo "muc.nick=${MY_USERNAME}" >> $XMPP_CLIENT_ACCOUNTS
        echo 'presence.last=online' >> $XMPP_CLIENT_ACCOUNTS
        echo 'presence.login=online' >> $XMPP_CLIENT_ACCOUNTS
        echo 'priority.online=0' >> $XMPP_CLIENT_ACCOUNTS
        echo 'priority.chat=0' >> $XMPP_CLIENT_ACCOUNTS
        echo 'priority.away=0' >> $XMPP_CLIENT_ACCOUNTS
        echo 'priority.xa=0' >> $XMPP_CLIENT_ACCOUNTS
        echo 'priority.dnd=0' >> $XMPP_CLIENT_ACCOUNTS
        if [ ${#XMPP_PASSWORD} -gt 2 ]; then
            echo "password=$XMPP_PASSWORD" >> $XMPP_CLIENT_ACCOUNTS
        fi
    fi

    if [ -f /var/lib/tor/hidden_service_xmpp/hostname ]; then
        echo "[${MY_USERNAME}@${XMPP_ONION_HOSTNAME}]" >> $XMPP_CLIENT_ACCOUNTS
        if [[ $ONION_ONLY == 'no' ]]; then
            echo 'enabled=false' >> $XMPP_CLIENT_ACCOUNTS
        else
            echo 'enabled=true' >> $XMPP_CLIENT_ACCOUNTS
        fi
        echo "jid=${MY_USERNAME}@${XMPP_ONION_HOSTNAME}" >> $XMPP_CLIENT_ACCOUNTS
        echo "server=$XMPP_ONION_HOSTNAME" >> $XMPP_CLIENT_ACCOUNTS
        # There is a bug where profanity doesn't refresh the screen
        # after gpg-agent has asked for a password, so for now
        # don't set the gpg key by default
        #echo "pgp.keyid=$MY_GPG_PUBLIC_KEY_ID" >> $XMPP_CLIENT_ACCOUNTS
        echo "pgp.keyid=" >> $XMPP_CLIENT_ACCOUNTS
        echo 'resource=profanity' >> $XMPP_CLIENT_ACCOUNTS
        echo "muc.service=${XMPP_ONION_HOSTNAME}" >> $XMPP_CLIENT_ACCOUNTS
        echo "muc.nick=${MY_USERNAME}" >> $XMPP_CLIENT_ACCOUNTS
        echo 'presence.last=online' >> $XMPP_CLIENT_ACCOUNTS
        echo 'presence.login=online' >> $XMPP_CLIENT_ACCOUNTS
        echo 'priority.online=0' >> $XMPP_CLIENT_ACCOUNTS
        echo 'priority.chat=0' >> $XMPP_CLIENT_ACCOUNTS
        echo 'priority.away=0' >> $XMPP_CLIENT_ACCOUNTS
        echo 'priority.xa=0' >> $XMPP_CLIENT_ACCOUNTS
        echo 'priority.dnd=0' >> $XMPP_CLIENT_ACCOUNTS
        if [ ${#XMPP_PASSWORD} -gt 2 ]; then
            echo "password=$XMPP_PASSWORD" >> $XMPP_CLIENT_ACCOUNTS
        fi
    fi

    if [ ! -d /home/$MY_USERNAME/.config/profanity ]; then
        mkdir -p /home/$MY_USERNAME/.config/profanity
    fi
    echo '[connection]' > /home/$MY_USERNAME/.config/profanity/profrc
    if [[ $ONION_ONLY == 'no' ]]; then
        echo "account=${MY_USERNAME}@${DEFAULT_DOMAIN_NAME}" >> /home/$MY_USERNAME/.config/profanity/profrc
    else
        echo "account=${MY_USERNAME}@${XMPP_ONION_HOSTNAME}" >> /home/$MY_USERNAME/.config/profanity/profrc
    fi
    echo '' >> /home/$MY_USERNAME/.config/profanity/profrc
    echo '[plugins]' >> /home/$MY_USERNAME/.config/profanity/profrc
    echo 'load=prof_omemo_plugin.py;' >> /home/$MY_USERNAME/.config/profanity/profrc
    echo '' >> /home/$MY_USERNAME/.config/profanity/profrc
    echo '[otr]' >> /home/$MY_USERNAME/.config/profanity/profrc
    echo 'policy=opportunistic' >> /home/$MY_USERNAME/.config/profanity/profrc
    echo 'log=off' >> /home/$MY_USERNAME/.config/profanity/profrc
    echo '' >> /home/$MY_USERNAME/.config/profanity/profrc
    echo '[pgp]' >> /home/$MY_USERNAME/.config/profanity/profrc
    echo 'log=off' >> /home/$MY_USERNAME/.config/profanity/profrc
    echo '' >> /home/$MY_USERNAME/.config/profanity/profrc
    echo '[ui]' >> /home/$MY_USERNAME/.config/profanity/profrc
    echo 'enc.warn=true' >> /home/$MY_USERNAME/.config/profanity/profrc

    chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.local
    chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.config

    chmod -R 700 /root/.local/lib/python3.4/site-packages/python_axolotl_*
    APP_INSTALLED=1
}

# NOTE: deliberately no exit 0
