/*-
* Copyright (C) 2005-2007 Nick Withers. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include "downtime.h"

#include <errno.h>
#include <gtk/gtk.h>
#include <pwd.h>
#include <stdio.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>

#ifdef BSD
#include <sysexits.h>
#endif

char *shutdown_path;

int
main(int argc, char *argv[])
{
	user_specs_t specs;

	gtk_init(&argc, &argv); /* Parse GTK command-line parameters */

	g_set_prgname(PRGNAME); /* Set the program name */

	init_user_specs(&specs);

	/* Allow for the specification of a configuration file. Done a little primitively... */
	if (argc == 2)
		specs.config_path = argv[1];
	else
		specs.config_path = get_config_path();

	if (specs.config_path == NULL)
	{
		specs.remember = FALSE;

		if (errno == 0)
			show_warning_dialog("Couldn't find the current user's passwd entry and therefore can't locate the user's %s configuration file. Default settings will be used and the ability to remember settings will be disabled", PRGNAME);
		else
			show_warning_dialog("Couldn't determine the location of the %s configuration file. Default settings will be used and the ability to remember settings will be disabled. Error: \"%s\" (%d)", PRGNAME, strerror(errno), errno);
	}
	else if (!load_user_specs(&specs, specs.config_path)) /* If we couldn't load configuration information from the file at config_path */
	{
		if (errno == ENOENT)
			show_stderr("Couldn't find configuration file \"%s\". Default shutdown parameters will be used", specs.config_path); /* If we couldn't read the configuration file because no such file exists, don't warn the user graphically, just print a brief message to stdout */
		else
			show_warning_dialog("Couldn't parse configuration file \"%s\". Error: \"%s\" (%d)", specs.config_path, strerror(errno), errno);
	}

	#ifndef WITH_SHUTDOWN
	if ((shutdown_path = find_executable_in_path(SHUTDOWN_FILENAME)) == NULL)
		shutdown_path = DEFAULT_SHUTDOWN_PATH;
	#else
	shutdown_path = WITH_SHUTDOWN;
	#endif

	if (access(shutdown_path, F_OK) != 0)
		show_error_dialog(EX_UNAVAILABLE, PRGNAME " was unable to find the shutdown binary, required for operation! The search path was checked, and the /sbin/shutdown binary does not exist");

	{
		struct stat sb;

		if (stat(shutdown_path, &sb) != 0)
			show_warning_dialog("Couldn't stat(2) \"%s\". Error: \"%s\" (%d)", shutdown_path, strerror(errno), errno);
		else if (!((sb.st_mode & S_ISUID) && sb.st_uid == 0)) /* If the shutdown(8) binary isn't setuid root */
			show_warning_dialog("You may have to launch " PRGNAME " as the superuser to be able to successfully perform a shutdown");
		else
		{
			if (access(shutdown_path, X_OK) != 0)
				show_warning_dialog("You do not appear to have \"execute\" permissions on the shutdown(8) binary and may not be able to successfully perform a shutdown.\n\nRunning \"chmod o+x %s\" may resolve this, but please be aware of the security ramifications of doing so.", shutdown_path);
		}
	}

	show_main_window(&specs);

	return (EXIT_SUCCESS);
}


syntax highlighted by Code2HTML, v. 0.9.1