#!/bin/bash
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
#
#                              Freedom in the Cloud
#
# Visit ipfs sites by entering a nickname
#
# License
# =======
#
# 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/>.

PROJECT_NAME='freedombone'

export TEXTDOMAIN=${PROJECT_NAME}-mesh-visit-site
export TEXTDOMAINDIR="/usr/share/locale"

IPFS_URL='http://127.0.0.1:8080/ipns'

# The browser application to use
BROWSER=firefox
BROWSER_OPTIONS='-url'

# An optional suffix to be appended to the URL
SUFFIX=$1

IPFS_USERS_FILE=/tmp/.ipfs-users
if [ ! -f $IPFS_USERS_FILE ]; then
    exit 0
fi
USERS_FILE=/tmp/Users.txt
if [ ! -f $USERS_FILE ]; then
    exit 0
fi

USERS_FILE_LINES=$(wc -l $USERS_FILE | awk -F ' ' '{print $1}')

if [ "$USERS_FILE_LINES" -gt 200 ]; then
    # If there are more than a Dunbar number of peers then ask for the peer name or ID
    data=$(zenity --entry --title="Visit IPFS site" --text="Enter the nickname or Tox ID for the site you wish to visit")
    sel=$?
    case $sel in
    0)
            TOX_USERNAME_OR_ID="$data"
            if [ ${#TOX_USERNAME_OR_ID} -gt 0 ]; then
        if ! grep -q "$TOX_USERNAME_OR_ID" $USERS_FILE; then
                    TOX_ID="$TOX_USERNAME_OR_ID"
        else
                    TOX_ID=$(grep "$TOX_USERNAME_OR_ID" "$USERS_FILE" | head -n 1 | sed "s|$TOX_USERNAME_OR_ID ||g" | sed -e 's/^[[:space:]]*//')
        fi

        if [ ${#TOX_ID} -gt 5 ]; then
                    if ! grep -q "$TOX_ID" $IPFS_USERS_FILE; then
            zenity --info --title $"Visit a site" --text $"An IPFS site was not found for '$TOX_USERNAME_OR_ID'" --width 500
            exit 3
                    fi
                    IPFS_FULL_URL=${IPFS_URL}/$(grep "$TOX_ID" "$IPFS_USERS_FILE" | head -n 1 | awk -F ':' '{print $2}')
                    pkill $BROWSER
                    setsid sh -c "$BROWSER $BROWSER_OPTIONS $IPFS_FULL_URL$SUFFIX" > /dev/null 2>&1 < /dev/null &

                    # Need to wait a while for the browser to begin opening
                    sleep 3
        fi
            else
        exit 1
            fi
        ;;
    esac
else
    if [[ "$SUFFIX" == '/Blog' ]]; then
        echo "$(toxid -s) $(toxid)" > /tmp/.visit_site_self
    else
        touch /tmp/.visit_site_self
    fi

    # If there are a relatively small number of users then choose from a list
    TOX_ID=$(
    # shellcheck disable=SC2002
    cat /tmp/.visit_site_self "$USERS_FILE" | \
        awk -F ' ' '{
        for(i=1;i<=NF;i++){
            print $i;
        }
}' | \
        zenity --list \
           --title='Visit the site of another peer' \
           --column='Nickname' --column='Tox ID' \
           --print-column=2 --hide-column=2 --width=300 --height=400)
    rm /tmp/.visit_site_self
    if [ ! "$TOX_ID" ]; then
        exit 0
    fi
    IPFS_FULL_URL=${IPFS_URL}/$(grep "$TOX_ID" "$IPFS_USERS_FILE" | head -n 1 | awk -F ':' '{print $2}')
    pkill $BROWSER
    setsid sh -c "$BROWSER $BROWSER_OPTIONS $IPFS_FULL_URL$SUFFIX" > /dev/null 2>&1 < /dev/null &

    # Need to wait a while for the browser to begin opening
    sleep 3
fi

exit 0