#!/bin/bash
# daemonize: daemonize a process

. q

function daemonize() {
	local b=$(basename "$1")
	
	q killall "$b"

	(
#		cd /
		stem="$HOME/.logs/$b"
		echo $$ > "$stem.pid"
		exec "$@" >> "$stem.log" 2>&1
	) &
}

if [ "$0" = "$BASH_SOURCE" ]; then
	daemonize "$@"
fi



# This is also useful (at the moment) for launching apps from the command line without messing up the terminal / jobs.
# use abbrev `d` for that purpose.


# TODO: this likely isn't correct, need double fork or something?

# better still, I should use an existing daemonize tool?

# ideas for options:

# maybe push back on this and keep it simple: YAGNI / UNIX way

# move to / option

# default is to kill others "there can be only one"
# -m to allow multiple
# ??  to fail if another exists
# -i  to ask and either kill / overwrite or fail
