#!/bin/sh +m

###########################################################################
# Runs a command in the background without waiting for any output.
# Prints the pid of the command in stdout.
#
# Copyright (C) 2011 Alkis Georgopoulos <alkisg@gmail.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 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# On Debian GNU/Linux systems, the complete text of the GNU General
# Public License can be found in `/usr/share/common-licenses/GPL'.
###########################################################################

case $# in
    0)
        echo "Usage: $0 <command, file or URL>" >&2
        exit 1
        ;;
    1) # If there's only one parameter, it might be a file or URL
        which -- "$1" >/dev/null || set "xdg-open" "$1"
        ;;
esac

# Do some logging, either in ~/.xsession-errors or on the console.
echo "$(LANG=C date '+%c'), epoptes-client executing: $@" >&2

# The command is ran with stdin and stdout descriptors redirected to /dev/null,
# so that it doesn't interfere with the output of other commands.
# stderr isn't changed, i.e. ~/.xsession-errors will be used.
"$@" 0</dev/null >/dev/null &

# Print the pid.
echo $!
