#!/usr/bin/sh --
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2010  Rafal Wojtczuk  <rafal@invisiblethingslab.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

# lack of -e and -u is intentional

# we use “is this variable set” a lot, and use variables that we haven’t set, so
# unset these variables first, to ensure that values from the environment don’t
# cause problems.
unset qopen_opts target filename seen_dashdash i

usage() {
    case $0 in
    (qvm-open-in-dvm|*/qvm-open-in-dvm)
        printf 'Usage: %s [--view-only] [--] filename\n' "$0" >&2
        ;;
    (*) printf 'Usage: %s [--view-only] [--] vmname filename\n' "$0";;
    esac >&2
    exit "${1-2}"
}
case $0 in
(qvm-open-in-dvm|*/qvm-open-in-dvm) target=@dispvm;;
(qvm-open-in-vm|*/qvm-open-in-vm) :;;
(*) printf 'Must be invoked as qvm-open-in-vm or qvm-open-in-dvm, not as %s\n' "$0" >&2
    exit 2;;
esac

for i; do
    if [ -z "$seen_dashdash" ]; then
        case $i in
        (--) seen_dashdash=x; continue;;
        (--help) usage 0;;
        (--view-only) qopen_opts=--view-only; continue;;
        (-*) usage;;
        esac
    fi
    if [ -z "${target+x}" ]; then
        target="$i"
    elif [ -z "${filename+x}" ]; then
        filename="$i"
    else
        usage
    fi
done
set +u

if [ -z "$target" ] || [ -z "$filename" ]; then
    usage
fi

if [ -d "$filename" ]; then
    printf 'Error! "%s" is a directory.\n' "$filename"
    usage
elif [ ! -f "$filename" ]; then
    expr "@$filename" : '@[A-Za-z][A-Za-z0-9+]*://'>/dev/null
    # shellcheck disable=SC2181
    if [ $? -ne 0 ]; then
        printf 'Error! "%s" is not a valid file or URL.\n' "$filename"
        usage
    fi
fi

prompt_text='Are you sure you want to create another disposable VM?'
case $target in
(@dispvm|@dispvm:*)
    case ${QREXEC_SERVICE_FULL_NAME-} in
    (qubes.OpenInVM+*|qubes.OpenURL+*)
        if [ -f /usr/bin/zenity ]; then
            zenity --question "--text=$prompt_text" || exit "$?"
        elif [ -f /usr/bin/kdialog ]; then
            kdialog "--yesno=$prompt_text" || exit "$?"
        else
            printf 'Cannot prompt user, exiting to avoid possible infinite VM creation loop.\n' >&2
            exit 1
        fi
        ;;
    esac
    ;;
esac

expr "@$filename" : '@[A-Za-z][A-Za-z0-9+]*://'>/dev/null
# shellcheck disable=SC2016
case $? in
(0) exec /usr/lib/qubes/qrexec-client-vm -- "$target" qubes.OpenURL \
    /bin/sh -c 'printf %s\\n "$1"; exec cat >/dev/null' sh "$filename";;
(1) exec /usr/lib/qubes/qrexec-client-vm -- "$target" qubes.OpenInVM \
    /usr/lib/qubes/qopen-in-vm $qopen_opts -- "$filename";;
(*) exit "$?";;
esac >/dev/null 2>&1 </dev/null
