#!/bin/sh
#
# Copyright (c) 2023,2024,2026 Nick Holland <nick@holland-consulting.net>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

# unrelated to the above license, I'd love to hear from you if you
# use this and find it useful...or if you find flaws or simple improvements.

# History:
# 2026-01-05 -- fixed quoting of "$" in help output so the help screen
#   examples actually work.  

function helpexit {
    more <<-__ENDHELP

	 Processes a set of parameters, which may be individual items and/or
	 a file name ending with .list, which is then expanded.  Within the
	 *.list file could be other .list files (recursion limited to five 
	 levels, to prevent infinite loops).
	
	 For example: 
	  +------------------------------------------------------------------
	  |# Test for listfile. comments should be ignored
	  |One
	  |    two
	  |# ignore this line
	  |three  # comments to be ignored
	  |four five  # ignore2
	  |              
	  |		
	  |# above lines were several spaces and several tabs
	  |#ignore3
	  |# ignore4
	  |six
	  +------------------------------------------------------------------
	
	  \$ listfile zero testfile.list SEVEN
	  zero One two three four five six SEVEN
	
	 if the first parameter is "-l", the space delimiter will be replaceed
	 with a newline (one item per line)
	
	 Here is a simple uptime checker that will take individual machines or 
	 .list files:
	  +------------------------------------------------------------------
	  |#/bin/sh
	  |
	  |LIST="\$(listfile \$*)"
	  |COUNT=0
	  |
	  |for i in \$LIST; do
	  |    COUNT=\$(( \$COUNT + 1 ))
	  |    echo -e "\n\$i -------- \$COUNT"
	  |    if ! ssh \$i uptime; then
	  |        echo -n "DIDN'T WORK!  (hit enter)"
	  |        read zzz
	  |     fi
	  |done
	  +------------------------------------------------------------------
	
	__ENDHELP
    exit
    }

LAYER=0 # layers of recursion
MAXLAYERS=5 # at some point, we've probably got a loop going.  exit.

if [[ $1 == "-h" ]]; then
    helpexit 
fi

# Set the separatator
if [[ $1 == "-l" ]]; then
    shift
    SEPARATOR="\n"
else
    SEPARATOR=" "
fi

function dotlist {
# $1 is recursion level $2- is the stuff to process
    typeset RL
    RL=$1; shift
    # echo "\n=== $RL $* ==="
    if [[ $RL -gt  $MAXLAYERS ]]; then
	echo "\n==likely loop.  Exiting==" >&2
	exit
    fi
    for E in $*; do
	if [[ $E == *.list && -f $E ]]; then
	    dotlist $(( $RL + 1 )) $(sed "s/#.*//" $E)
	else
	    echo -en "$E$SEPARATOR"
	fi
    done
} #end function dotlist

# do it
dotlist 0 $*
