#!/bin/ksh
# @(#) dissociate.ksh 1.0 92/09/15
# 92/09/15 John H. DuBois III (john@armory.com)

# Use ksh because its "type" has a useful exit value

PrintPID=0
if [ "$1" = -p ]; then
    PrintPID=1
    shift
fi

if [ $# -eq 0 -o "$1" = -h ]; then
    echo \
"$0: execute a process disconnected from any tty.
Syntax: $0 [-hp] command arg ...
command is executed with stdin, stdout, and stderr of /dev/null.
It will initially belong to its own process group with no controlling tty.
The command is executed asynchronously, so it will not be waited for.
Its parent will be process 1 (init).
A check is done to make sure that command is executable.
Any other errors that prevent command from being executed will not be
reported, since they will occur after error output has been redirected.
Options:
-h: print this help.
-p: print the process ID of the executed command."
    exit 0
fi

if type "$1" > /dev/null; then :; else
    echo "Cannot execute $1.  Aborting." 1>&2
    exit 1
fi

/bin/setpgrp "$@" < /dev/null > /dev/null 2>&1 &
[ $PrintPID = 1 ] && echo $!
