/*
 *   Copyright (c) 1998 David Parsons. 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.
 *  3. All advertising materials mentioning features or use of this
 *     software must display the following acknowledgement:
 *     
 *   This product includes software developed by David Parsons
 *   (orc@pell.chi.il.us)
 *
 *  4. My name may not be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *     
 *  THIS SOFTWARE IS PROVIDED BY DAVID PARSONS ``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 DAVID
 *  PARSONS 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.
 */

static const char rcsid[] = "$Id: s.services.cc 1.6 98/06/18 06:05:22-00:00 orc $";

/*
 * services: manage system services
 */
extern "C" {
#include <dialog.h>
#include <ndialog.h>
#include <stdio.h>
#include <unistd.h>
#include "getrc.h"
}


LIA standard, custom;				/* std and custom services */
ndObject o_standard, o_custom, o_selstd, o_selcust;	/* our display stuff */

static int chk_std(ndObject obj, ndDisplay display);
static int chk_cust(ndObject obj, ndDisplay display);
static int finalcheck(ndObject obj, ndDisplay display);

static void link_standard_services();
static void link_custom_services();

static char run_services_now = 0;


/*
 * newServicesMenu() builds the System Services menu
 *
 * we do some horrible hackery here.  We overlay the standard and
 * custom services menu on top of each other, and flip the visibility
 * as appropriate when the user clicks the [STANDARD] and [ CUSTOM ]
 * buttons.
 */
ndObject
newServicesMenu()
{
    ndObject chain;
    char msg[80];

    standard = newLIA(0,0);
    custom   = newLIA(0,0);

    dialog_msgbox(0, "Building services menu; please wait", -1, -1, 0);
    getrc('m', &standard, &custom);

    o_standard = newList(0,0,40,15,
			 LIAcount(standard),
			 LIAlist(standard),
			 "           Standard services  ", "",
			 CHECK_SELECTED, 0, "services.html#standard");

    o_custom  = newList(0,0,40,15,
			LIAcount(custom),
			LIAlist(custom),
			 "            Custom services  ", "",
			CHECK_SELECTED, 0, "services.html#custom");

    o_selstd  = newButton(3, "STANDARD", chk_std, "services.html");
    o_selcust = newButton(4, " CUSTOM ", chk_cust,"services.html");

    chain = ObjChain(o_standard, o_custom);
    chain = ObjChain(chain, o_selstd);
    chain = ObjChain(chain, o_selcust);

    if (getuid() == 0) {
	chain = ObjChain(chain, newOKButton(1, "  OK  ", finalcheck, "services.html"));
	chain = ObjChain(chain, newCancelButton(2, "CANCEL", 0, "services.html"));
    }
    else
	chain = ObjChain(chain, newOKButton(1, "  OK  ", 0, "services.html"));

    if (getuid() != 0) {
	setReadonly(o_standard);
	setReadonly(o_custom);
    }
    setReadonly(o_selstd);
    hideObj(o_custom);

    return chain;
} /* newServicesMenu */



static int
chk_std(ndObject obj, ndDisplay display)
{
    hideObj(o_custom);
    unhideObj(o_standard);	drawObj(o_standard, display);
    setReadonly(o_selstd);	drawObj(o_selstd, display);
    setWritable(o_selcust);	drawObj(o_selcust, display);

    return 1;
} /* chk_std */


static int
chk_cust(ndObject obj, ndDisplay display)
{
    hideObj(o_standard);
    unhideObj(o_custom);	drawObj(o_custom, display);
    setReadonly(o_selcust);	drawObj(o_selcust,display);
    setWritable(o_selstd);	drawObj(o_selstd, display);

    return 1;
} /* chk_cust */


static ndObject f_yes, f_no;

static int
typeofok(ndObject obj, ndDisplay display)
{
    run_services_now = (obj == f_yes);
    return -1;
} /* typeofok */


/*
 * finalcheck asks if the user wants to start the new services now
 * or later.
 */
static int
finalcheck(ndObject obj, ndDisplay display)
{
    int rc;
    ndObject chain;

#if IMMEDIATE_CHANGE
    f_yes = newOKButton(0, "NOW", typeofok, 0);
    f_no  = newOKButton(1, "NEXT REBOOT", typeofok, 0);

    chain = ObjChain(f_yes, f_no);
    chain = ObjChain(chain, newCancelButton(2, "CANCEL", 0, 0));

    rc = MENU(chain, -1, -1, 0, "Do you want the changes to take effect\n"
				"   now or at the next system reboot?", 0);
    deleteObjChain(chain);

    if (rc == MENU_OK) {
	link_standard_services();
	link_custom_services();
	return 1;
    }
#else
    if (dialog_yesno(0, "These changes will take effect at\n"
			"the next system reboot.  Do them\n"
			"anyway?", -1, -1) == 0) {
	link_standard_services();
	link_custom_services();
	return 1;
    }
#endif
    return 0;
} /* finalcheck */


/*
 * link_standard_services() activates and deactivates standard services
 */
void
link_standard_services()
{
    int x;
    char rcpath[200];
    char initpath[200];

    for (x=0; x < LIAcount(standard); x++) {
	sprintf(rcpath, RC_PATH_PREFIX "m/%s", LIAlist(standard)[x].id);
	sprintf(initpath, RC_INIT_LINK "/%s", LIAlist(standard)[x].id);

	if (LIAlist(standard)[x].selected)
	    symlink(initpath, rcpath);
	else
	    unlink(rcpath);
    }
} /* link_standard_services */


/*
 * link_custom_services() activates and deactivates custom services.
 */
void
link_custom_services()
{
    int x;
    char activerc[200];
    char inactiverc[200];

    for (x=0; x < LIAcount(custom); x++) {
	sprintf(activerc, RC_PATH_PREFIX "m/%s", LIAlist(custom)[x].id);
	sprintf(inactiverc, RC_PATH_PREFIX "m/.%s", LIAlist(custom)[x].id);

	if (LIAlist(custom)[x].selected)
	    rename(inactiverc, activerc);
	else
	    rename(activerc, inactiverc);
    }
} /* link_custom_services */


/*
 * doit() the main() routine
 */
extern "C"
doit(int argc, char **argv)
{
    ndObject chain;
    int rc;

    chain = newServicesMenu();

    rc = MENU(chain, -1, -1, getuid() ? "System services" : "Configure system services", 0, 0);
    deleteObjChain(chain);
    deleteLIA(standard);
    deleteLIA(custom);

    return (rc == MENU_OK) ? 0 : 1;
} /* doit */
