#!/bin/bash

# Copyright (c) 2020 jm1hey
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# Version
script_version="1.2"

# The purpose of this script is described in the 'printhelp' function defined below.

# define command name -- must be the same as the script file name
cmdname="scrlaunch"

#define usage and help functions
function printusage {
	printf "\nUsage: %s ARGUMENT(s) | OPTION\n\n" "$cmdname"
	printf "Options:\n"
	printf "\t--help\n"
	printf "\t\tPrint help information and exit\n\n"
	printf "\t--version\n"
	printf "\t\tPrint version information and exit\n\n"
}

function printhelp {
	printf "\n'%s'%s%s%s%s%s%s\n" "$cmdname" " is a Bash script that utilizes 'screen'" \
		" to run commands in the background. The commands passed as arguments are" \
		" executed in a new detached screen instance so that any command line input" \
		" and output takes place inside the screen instance. It is useful for" \
		" launching applications from a terminal instance without tying up its input" \
		" and output. Naturally, GNU Screen must be installed."
	printusage
}

function printversion {
	echo "$script_version"
}

# check for sufficient number of arguments
if [ $# -lt 1 ]
then
	printusage
	exit 1
fi

# check for "--help" argument
for i in "$@"
do
	if [ "$i" == "--help" ]
	then
		printhelp
		exit 0
	fi
done

# check for "--version" argument
for i in "$@"
do
	if [ "$i" == "--version" ]
	then
		printversion
		exit 0
	fi
done

# check if 'screen' command is available
which screen > /dev/null
if [ $? != 0 ]
then
	printf "Error: screen not found\n\n"
	exit 1
fi

# make sure first argument is valid
# first argument should not start with "-"
if [ "${1:0:1}" == "-" ]
then
	printf "Error: Invalid argument: \"%s\"\n\n" "$1"
	exit 1
fi
# check if first argument exists in current environment
which "$1" > /dev/null
if [ $? != 0 ]
then
	printf "Error: \"%s\" not found\n\n" "$1"
	exit 1
fi

# launch using detached screen
screen "-d" "-m"$(for arg in "$@"; do printf " "; printf "%s" "$arg"; done)
exit $?
