#! /bin/sh

##
# aludo
#
#	Capture notable clues of incremental system changes
#	to help trigger data collection, as a hint to lead
#	to a later support assessment
#
#	NOTE: Translate aludo from Esperanto to English -> hint
##

# Usage
#	
#if [ "$1" = "--help" ] || \
#	[ "$1" = "help" ] || \
#	[ "$1" = "-h" ] || \
#  then
#        echo "Usage: `basename $0` [ -v | --verbose ]"
#        exit 1
#fi

# Invocation
#	should be run as root (or via sudo)
if [ "$UID" != "0" ]
  then
	echo "Requires root (or sudo) privileges to run $0" >&2
        exit 1
fi

# Configuration parameters / files
  CF=/usr/etc/aludo.conf
  [ -r ${CF} ] && source ${CF}
  CF=/etc/aludo.conf
  [ -r ${CF} ] && source ${CF}
  CF=./aludo.conf
  [ -r ${CF} ] && source ${CF}

# Source and use helper functions
  [ -r ${functionDir}/helper/collection ] && \
	source ${functionDir}/helper/collection
  [ -r ./helper/collection ] && \
	source ./helper/collection
  [ -r ${functionDir}/helper/timestamp ] && \
	source ${functionDir}/helper/timestamp
  [ -r ./helper/timestamp ] && \
	source ./helper/timestamp
  [ -r ${functionDir}/helper/transport ] && \
	source ${functionDir}/helper/transport
  [ -r ./helper/transport ] && \
	source ./helper/transport
  # prepare/adapt collection space
    outDir
  # get needed timestamps (current, previous)
    currentTS
    pastTS
  # prepopulate output artifacts
    artifacts

##
# look for clues
##
for focus in \
	$([ -d ${functionDir}/indico ] && ls ${functionDir}/indico) \
        $([ -d ./indico ] && ls ./indico)
  do
	echo "# ===== Clue Focus : ${focus} =====" >>${LF} && \
  	entryCMD="${focus}"
  	if which ${entryCMD} >/dev/null 2>&1 
	  then
		status=maybe && echo "## status=${status}" >>${LF}
		[ -r ${functionDir}/indico/${focus} ] && \
			source ${functionDir}/indico/${focus}
		[ -r ./indico/${focus} ] && \
			source ./indico/${focus}
		identify
		if [ ${status} = "true" ] 
		  then
			setup
			acquire
			evaluate
			cleanup
			admission
		fi
	  else
		status="false"
	fi
  done		

# gather relevant clues/evidence 
if [ $(cat ${RF} | wc -l 2>&1) -gt 1 ]
  then
	cd ${outputDir}
	echo "See ... "
	echo "- Hints :"
       	echo "  ${RF}"
	ln -s ${runTSISO} ${runTS}

        # gather auxilliary supportconfig output
	cmd="supportconfig"
	echo "# ===== collect dataset via ${cmd}" >>${LF} && \
	if which supportconfig >/dev/null 2>&1
	  then
		supportconfig -R ${runTSISO} >>${LF} 2>&1
	  else
		echo "suggest installation of supportutils package" >/dev/stderr
	fi

	# bundle results
	cd ${collectionDir}
	cmd="tar"
	echo "# ===== bundle data/hints/clue via ${cmd}" >>${LF} && \
		${cmd} -czf ${mID}-${runTS}.tgz ${mID}/${runTS}/* >/dev/null 2>&1 && \
		echo "- Log File :" && \
		echo "  ${LF}"
  else
	echo "No recent hints found, review ..." >/dev/stderr
	echo "- Log File :" >/dev/stderr
	echo "  ${LF}" >/dev/stderr
	exit 1
fi

# if allowed, transport result bundle to another location
xfer=$(echo ${transport} | tr '[:upper:]' '[:lower:]')
if [ ${xfer} = "y" ] || [ ${transport} = "yes" ] 
  then
  	if which ${transportCMD} >/dev/null 2>&1 
	  then
		echo "# ===== transport results via ${transportCMD}to ${transportURI}" >>${LF} && \
		xfer_${transportCMD}
	  else
		echo "Unable to utilize transportCMD : ${transportCMD}" >/dev/stderr
		exit 1
	fi
  else
	echo "- Collected Data :"
	echo "  ${collectionDir}/${mID}-${runTS}.tgz"

fi
