#!/usr/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 ${functionDir}/helper/timestamp ] && \
	source ${functionDir}/helper/timestamp
  # prepare/adapt collection space
    outDir
  # get needed timestamps (current, previous)
    currentTS
    pastTS
  # prepopulate output artifacts
    artifacts

##
# look for clues
##
for focus in $(ls ${functionDir}/indico)
  do
	echo "# ===== Clue Focus : ${focus} =====" >>${LF} && \
  	entryCMD="${focus}"
  	if which ${entryCMD} >/dev/null 2>&1 
	  then
		status=maybe && echo "## status=${status}" >>${LF}
		source ${functionDir}/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 supportconfig
	cmd="supportconfig"
	echo "# ===== collect dataset via ${cmd}" >>${LF} && \
	which supportconfig >/dev/null 2>&1 && supportconfig -R ${runTSISO} >>${LF} 2>&1
	
	# bundle results
	cd ${collectionDir}
	cmd="tar"
	echo "# ===== bundle data/hints/clue via ${cmd}" >>${LF} && \
		tar -czf ${mID}-${runTS}.tgz ${mID}/${runTS}/* >/dev/null 2>&1 && \
		echo "- Log File :"
		echo "  ${LF}" && \
		echo "- Collected Data :"
      	 	echo "  ${collectionDir}/${mID}-${runTS}.tgz"
  else
	echo "No recent hints found, review ..." >/dev/stderr
	echo "- Log File :" >/dev/stderr
	echo "  ${LF}" >/dev/stderr
	exit 1

fi
