#!/usr/bin/bash

# This file is part of Cockpit.
#
# Copyright (C) 2018 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.

# Run a local bridge, web server, and browser for a particular Cockpit page.
# This is useful for integration into .desktop files, for systems which don't
# have cockpit.socket enabled. The web server and browser run in an unshared
# network namespace, and thus are totally isolated from everything else.
#
# Usage:
#     cockpit-desktop /cockpit/@localhost/system/index.html -t default -r 0
# For testing purposes one can also make cockpit-ws bind to all addresses
# (this is insecure and should not be used in prodcuction):
#     cockpit-desktop /cockpit/@localhost/system/index.html -t default -r 1
#
set -eu

# exec_prefix= is set because the default /usr/libexec contains "${exec_prefix}"
exec_prefix="/usr"
libexecdir="/usr/libexec"

THEME_ID="default"
WEBUI_REMOTE=0
while getopts t:r: option
do
    case "${option}"
        in
        t)THEME_ID=${OPTARG};;
        r)WEBUI_REMOTE=${OPTARG};;
        *) echo "Usage: $0 [-t THEME_ID] [-r WEBUI_REMOTE] <Cockpit path>" >&2
       exit 1 ;;
    esac
done

shift "$((OPTIND-1))"
# Expand the commandline argument into a url
case "$1" in
    /*)
        URL_PATH="$1"
        ;;
    */)
        URL_PATH="/cockpit/@localhost/$1index.html"
        ;;
    */*)
        URL_PATH="/cockpit/@localhost/$1.html"
        ;;
    *)
        URL_PATH="/cockpit/@localhost/$1/index.html"
        ;;
esac

# prepare empty firefox profile dir with theme based on the passed profile id
FIREFOX_THEME_DIR="/usr/share/anaconda/firefox-theme"
FIREFOX_PROFILE_PATH="/tmp/anaconda-firefox-profile"

# make sure the profile directory exists and is empty
if [ -d ${FIREFOX_PROFILE_PATH} ]
then
    echo "Cleaning up existing Anaconda Firefox profile directory."
    rm -rf ${FIREFOX_PROFILE_PATH}
fi
mkdir -p ${FIREFOX_PROFILE_PATH}

# populate the profile directory with our custom Firefox theme
# - theme id is passed as the second argument of this script
THEME_PATH="${FIREFOX_THEME_DIR}/${THEME_ID}"

cp -a "${THEME_PATH}/." ${FIREFOX_PROFILE_PATH}

# FIXME: is this hardcoded resolution necessary ?
BROWSER="/usr/bin/firefox --new-instance --window-size 1024,768 --profile ${FIREFOX_PROFILE_PATH}"

# start browser in a temporary home dir, so that it does not interfere with your real one
BROWSER_HOME=$(mktemp --directory --tmpdir cockpit.desktop.XXXXXX)

WEBUI_ADDRESS="127.0.0.1"
if [[ "$WEBUI_REMOTE" == "1" ]]
then
    WEBUI_ADDRESS="0.0.0.0"
fi

# forward parent stdin and stdout (from bridge) to cockpit-ws
# it pretty well does not matter which port we use in our own namespace, so use standard http
# disable /etc/cockpit/
XDG_CONFIG_DIRS="$BROWSER_HOME" COCKPIT_SUPERUSER="pkexec" /usr/libexec/cockpit-ws -p 80 -a "$WEBUI_ADDRESS" --no-tls --local-session=cockpit-bridge &
WS_PID=$!
exec > >(systemd-cat -t anaconda) 2>&1

# Cleanup function
cleanup() {
    set +e
    kill $WS_PID
    wait $WS_PID
    kill $B_PID
    rm -rf $BROWSER_HOME
}
trap 'cleanup' EXIT INT QUIT PIPE

# if we have netcat, use it for waiting until ws is up
if type nc >/dev/null 2>&1; then
    for _ in `seq 10`; do
        nc -z "$WEBUI_ADDRESS" 80 && break
        sleep 0.5;
    done
else
    # otherwise, just wait a bit
    sleep 3
fi

HOME="$BROWSER_HOME" MOZ_APP_TITLE="" MOZ_APP_REMOTINGNAME="liveinst" XDG_CURRENT_DESKTOP=GNOME MOZ_GTK_TITLEBAR_DECORATION=client $BROWSER http://"$WEBUI_ADDRESS""$URL_PATH" &
B_PID=$!

wait $B_PID
