#!/bin/bash
# ======================================================================
# $Source: $
# $Revision: $
# $Name: $
# $Date: $
# $State: $
# $Author: $
# ======================================================================

check_program()
{
	if [ `type $1 > /dev/null 2>&1` ]; then
		echo "The '$1' program is missing or not in your PATH."
		echo "This program is included in the '$2' package."
		echo "Please install this program and run this script again."
		return 1
	fi

	return 0
}

sanity_check()
{
	# Check for programs that this script uses.
	check_program ls fileutils || return 1
	check_program egrep grep || return 1
	check_program id sh-utils || return 1
	check_program cut textutils || return 1
	return 0
}

check_dir()
{
	if [ ! -d $1 ]; then
		echo "Your computer does not appear to have a $1 directory."
		echo "Please investigate this problem, and run this script again."
		return 1
	fi

	if [ `ls -ld $1 | egrep -q 'drwxrwxrxt'` ]; then
		echo "The permissions on the directory $1 are not correct."
		echo "Please run 'chmod 1777 $1', and run this script again."
		return 1
	fi

	return 0
}

check_cron_table()
{
	local user_id=$(id -un)
	local cron_table=/var/cron/tabs/$user_id

	if [ ! -f $cron_table ]; then
		echo "Your computer does not appear to have a crontab for $user_id."
		echo "Please generate a crontab for $user_id using 'crontab -e',"
		echo "and run this script again."
		return 1
	fi

	if [ `ls -l $cron_table|egrep -q 'rw-r-----'` ]; then
		echo "The permissions of your crontab file are set to:"
		ls -l $cron_table
		echo "They need to be set to read/write for $user_id and"
		echo "to read-only for group.  You can set these with"
		echo "	chmod 640 $cron_table"
		echo "Please check your crontab's permissions, and run"
		echo "this script again."
		return 1
	fi

	if [ `ls -l $cron_table|cut -d" " -f8|egrep -q SYSTEM` ]; then
		echo "The group membership of your crontab file should be SYSTEM,"
		echo "as documented in the file /usr/doc/Cygwin/cron.README."
		echo "You can change this setting with:"
		echo "	 chgrp SYSTEM $cron_table"
		echo "Please check your crontab's group membership, and"
		echo "run this script again."
		return 1
	fi

	if [ `ls -ln $cron_table|cut -d" " -f8|egrep -q 18` ]; then
		echo "The value of SYSTEM in your /etc/group file needs to"
		echo "be the reserved number '18', as documented in"
		echo "/usr/doc/Cygwin/cron.README.  Please investigate this"
		echo "and run this script again."
		return 1
	fi

	echo "This script did not find any errors in your crontab setup."
	echo "If you are still unable to get cron to work, then try"
	echo "shutting down the cron service, uninstalling it,"
	echo "reinstalling it, and restarting it."
	echo 
	echo "If that does not fix the problem, then report your problem"
	echo "to cygwin@cygwin.com.  Please include a copy of your crontab"
	echo "('crontab -l') and the output of 'cygcheck -srv > cygcheck.txt'."
	echo "cygcheck.txt should be included *as an attachment*, not in the"
	echo "body of mail message."

	return 0
}

main()
{
	sanity_check || return 1

	if [ ! -d /var ]; then
		echo "Your computer does not appear to have a /var directory."
		echo "Please investigate this problem, and run this script again."
		return 1
	fi

	check_dir /var/cron || return 1
	check_dir /var/cron/tabs || return 1
	check_cron_table || return 1

	return 0
}

# Entry point:
main
exit $?

# === End of $RCSfile$ === #

