#!/bin/bash
#       Copyright (C) 2017 HikariKnight <rshikariknight@gmail.com>
#       and contributors found in the AUTHORS file.
#       Use of this script is governed by a GPL v2 license
#       that can be found in the LICENSE file.
#       Source code and contact info at https://github.com/HikariKnight/runescape

# Script to add support for the jagex-jav protocol on Xfce4 without
# making permanent changes to the system installed xdg-open

# Make a tmp folder
tmpdir=$(mktemp -td xdg-open.XXXX)
exitcode=$?

if [ $exitcode -eq 0 ]; then
    # Copy xdg-open
    cp "/usr/bin/xdg-open" "$tmpdir"

    # Patch xdg-open
    sed -i -e 's/else DE=""/elif xprop -root 2> \/dev\/null | grep -i ^xfce_desktop_window >\/dev\/null 2>\&1; then DE=xfce;\nelse DE=\"\"/' "$tmpdir/xdg-open"

    # Execute xdg-open
    env "$tmpdir/xdg-open" "$@"

    rm -rf "$tmpdir"
    
else
    # Pass the exitcode to caller
    exit $exitcode
fi
