#!/bin/sh
#
# gtk3-nocsd - Wrapper to apply gtk3-nocsd to application
#
# Usage: ln -s /path/to/gtk3-nocsd ~/bin/evince
# Or:    gtk3-nocsd evince
#
# Create a symlink to this wrapper script with the name of the program
# you want run without client-side decorations. This wrapper script
# will run the first matching executable in the PATH that is *not* a
# symlink to gtk3-nocsd. This wrapper script is useful if you don't
# want to add gtk3-nocsd to your system-wide LD_PRELOAD or if you only
# want it applied to certain applications.
#
# Alternative mode of operation: call this wrapper directly with the
# program to call as its arguments.
#

OS=$(uname | tr "[:upper:]" "[:lower:]")
RLINK_ARGS=$([ "$OS" != "linux" ] && echo "-f" || echo "-fe")

GTK3_NOCSD_NAME=libgtk3-nocsd.so.0

# Determine if this script are installed in the system path
GTK3_NOCSD_BINARY="$(readlink ${RLINK_ARGS} "$(which "$0")")"
case "${GTK3_NOCSD_BINARY%/*}" in
    /sbin|/bin|/usr/bin|/usr/sbin|/usr/local/bin|/usr/local/sbin)  IN_SYSTEM_PATH=1  ;;
    *)                                                             IN_SYSTEM_PATH=0  ;;
esac

# Determine if the library is in the system path. If that's the case,
# just use the plain library name, because that way loading the library
# on all architectures it's installed is supported (because the linker
# will automatically pick out the right one).
if [ $IN_SYSTEM_PATH -eq 1 ] ; then
    if LC_ALL=C LD_PRELOAD="${GTK3_NOCSD_NAME}" /bin/true 2>&1  | grep LD_PRELOAD | grep -qF "${GTK3_NOCSD_NAME}" ; then
        IN_SYSTEM_PATH=0
    else
        GTK3_NOCSD="${GTK3_NOCSD_NAME}"
    fi
fi

if [ $IN_SYSTEM_PATH -eq 0 ] ; then
    # Figure out where the library is installed. First try the path
    # that was used when building gtk3-nocsd, then try the directory
    # the binary is installed.
    INSTALLED_PATH="/usr/local/lib"
    BINARY_PATH="${GTK3_NOCSD_BINARY%/*}"
    GTK3_NOCSD=""
    for _path in "$INSTALLED_PATH" "$BINARY_PATH" "${BINARY_PATH%/bin}/lib" ; do
        if [ -e "${_path}/${GTK3_NOCSD_NAME}" ] ; then
            GTK3_NOCSD="${_path}/${GTK3_NOCSD_NAME}"
            break
        fi
    done
    if [ -z "$GTK3_NOCSD" ] ; then
        # this will _probably_ not work (unless the library is
        # installed in a system path, but the binary wasn't), but at
        # least the user will have a useful error message
        GTK3_NOCSD="${GTK3_NOCSD_NAME}"
    fi
fi

# This program was called directly, instead of via a symlink.
if [ x"$(basename "$0")"x = x"gtk3-nocsd"x ] ; then
    if [ x"$1"x = x"-h"x ] || [ x"$1"x = x"--help"x ] ; then
        echo "Usage: $0 program [args]"
        exit 0
    fi

    export GTK_CSD=0
    export LD_PRELOAD="${GTK3_NOCSD}${LD_PRELOAD:+:$LD_PRELOAD}"
    exec "$@"
fi

# Find the real program (the first one that's not symlinked to get3-nocsd)
APPNAME="$(basename "$0")"
for APPPATH in $(which -a "$APPNAME") /bin/false; do
    APPPATH_LINK="$(readlink ${RLINK_ARGS} "$APPPATH")"
    [ x"${APPPATH_LINK##*/}"x = x"gtk3-nocsd"x ] || break
done

# Provide error message at all
if [ x"$APPNAME"x != x"false"x ] && [ x"${APPPATH}"x = x"/bin/false"x ] ; then
    APPPATH=/.gtk3-nocsd./"$APPNAME"
fi

# Run the program with CSD disabled
export LD_PRELOAD="${GTK3_NOCSD}${LD_PRELOAD:+:$LD_PRELOAD}"
export GTK_CSD=0
exec "$APPPATH" "$@"
