#!/bin/sh

get_agents()
{
	echo Fetching owners
	ctmsetown -action list

	echo Fetching Agents

	SQL_CMD='SQL'
	SQL_SET=''
	case $CTM_DATABASE_TYPE in
	ORACLE)
		SQL_SET='SET LIN 2000';
		SQL_EXEC=';'
		SQL_EXIT='exit'
		CONCAT_OP='||'
		;;
	POSTGRESS | PGSQL)
		SQL_EXEC=';'
		SQL_EXIT='\q'
		CONCAT_OP='||'	
		;;
	SYBASE)
		SQL_CMD='SQL -w 2000'
		SQL_EXEC='go'
		SQL_EXIT='exit'
		CONCAT_OP='+'
		;;
	*)
		echo Unknown DB type: $CTM_DATABASE_TYPE
		exit 17
		;;
	esac



	echo START
	$SQL_CMD <<EOF
	$SQL_SET
select 'PLATFORM='$CONCAT_OP PLATFORM $CONCAT_OP '@#@OS='$CONCAT_OP OS_NAME $CONCAT_OP  '@#@NODE='$CONCAT_OP NODEID $CONCAT_OP  '@#@TYPE='$CONCAT_OP NODETYPE $CONCAT_OP  '@#@STATUS='$CONCAT_OP  AGSTAT from CMR_NODES;
$SQL_EXEC
$SQL_EXIT
EOF
	echo END
}

get_db_and_platform()
{
	echo "DB=$CTM_DATABASE_TYPE"
	echo "PLATFORM=UNIX"
}


get_jobs()
{
	echo getting cron defs
	if [ "X"$USER = "Xroot" ]; then
		echo getting all jobs
		crontab_dir=/var/spool/cron
		if [ -d /var/spool/cron/tabs ]; then
			crontab_dir=/var/spool/cron/tabs
		elif [ -d /var/spool/cron/crontabs ]; then
			crontab_dir=/var/spool/cron/crontabs
		fi
		
		for tmp_file in `ls -1 $crontab_dir/*`
		do
			echo getting jobs for $tmp_file
			cat $tmp_file
		done
	else
		echo getting jobs for `whoami`
		crontab -l;
		echo crontab returned $?
	fi
	echo got cron defs
}

#main
if [ "X$1" = "XGET_AGENTS" ]; then
	get_agents
elif [ "X$1" = "XGET_DB_PLAT" ]; then
	get_db_and_platform
elif [ "X$1" = "XGET_JOB_DEFS" ]; then
	get_jobs	
fi

exit 0
