#!/usr/bin/env bash 
#-----------------------------------------------------------------------
# File:         fns-start-programs
version=3.0.5
# Licence:      GPL 2
#
# Description:  shell script to execute command strings which will be 
#               found in <infile>
#
# Parameters:   fns-start-programs <type> <infile>
#
# Remarks:      <type> is a name printed in [] to stderr
#
# Author:       Thomas Funk <t.funk@web.de>    
# Created:      08/13/2012
# Changed:      12/19/2014
#-----------------------------------------------------------------------



if [ "$1" = "-h" ] || [ "$1" = "--help" ]
then
    echo "------------------------------------------------------------------------"
    echo "Usage: `basename $0` <type> <infile>"
    echo "       <type>          name printed in [] to stderr"
    echo "       <infile>        execute command(s) found in infile"
    echo "       -h|--help       short help"
    echo "       -v|--version    version"
    echo
    echo "Example:"
    echo "`basename $0` autostart $FVWM_USERDIR/.autostart"
    echo
    echo "Output in stderr:"
    echo "[Fvwm-Nightshade][autostart]: 'bla' not running. Starting now."
    echo "------------------------------------------------------------------------"
    exit 0
elif [ "$1" = "-v" ] || [ "$1" = "--version" ]
then
    echo `basename $0` V $version
    exit 0
fi

if [ $# -lt 2 ]
then
    echo Not enough parameter! Exiting.
    exit 1
elif  [ $# -gt 2 ]
then
    echo Too much parameter! Exiting.
    exit 1
fi

type=$1
infile=$2

# check whether infile exist
if [ -f $infile ]
then
    # read startup file
    commands=$(grep -v "^ *\(#.*\|\)$" $infile)
    while IFS= read -r
    do
    #echo $REPLY
    # check if prog already running
    prog_started=`ps -ef|grep -c "$REPLY" |grep -v grep`
    if [ $prog_started -gt 1 ]
    then
            echo [Fvwm-Nightshade][$type]: \'$REPLY\' is already running. Skipping. 2>&1
        else
            echo [Fvwm-Nightshade][$type]: \'$REPLY\' not running. Starting now. 2>&1
            $REPLY &
            sleep 1
        fi
    done <<< "$commands"
    unset IFS
else
    echo [Fvwm-Nightshade][$type]: No file \'$infile\' found 2>&1
    exit 1
fi
exit 0
