/**
 * File:	modules/Fax.ycp
 * Package:	Communication
 * Summary:     Configuration of a ISDN Fax machine
 * Authors:	Karsten Keil <kkeil@suse.de>
 *
 * $Id: Fax.ycp 20711 2005-01-11 15:34:25Z jsuchome $
 */

{

module "Fax";
textdomain "phone-services";

import "Package";
import "Users";
import "UsersCache";

/**
 * All Fax machine configured user data
 */
global map<string,map> aconfig = $[];

/**
 * All Fax machine global settings
 */
global map<string,string> gconfig = $[];

/**
 * Data was modified?
 */
global boolean modified = false;

/**
 * List of available users
 */
global list users = [];

/**
 * All configurations read at the start
 */
map<string,map> aconfig_init = $[];
map<string,string> gconfig_init = $[];

/**
 * config file location
 */
string conf_file = "/etc/capisuite/fax.conf";

/**
 * needed packages
 */

list<string> need_pkgs = [ "capisuite" ];

/**
 * Read config settings
 * @return true if success
 */
global define boolean Read() {

    /* read config */
    path cf  = add(.phone-services.section, conf_file);
    if(SCR::Read(.target.size, conf_file) > 0) {
	aconfig = listmap(string s, SCR::Dir(cf), {
	    path pp = add(add(.phone-services.v, conf_file), s);
	    return $[ s : listmap(string vs, SCR::Dir(pp), {
		return $[ vs : SCR::Read(add(pp, vs)) ];
	    }) ];
	});
    }
    y2debug("aconfig=%1", aconfig);

    users = [];

    aconfig = filter(string k, map v, aconfig, {
	if (k == "GLOBAL") {
	    gconfig = (map<string,string>) v;
	    return(false);
	} else {
	    users = add(users, k);
	    return(true);
	}
    });

    y2debug("aconfig=%1", aconfig);
    y2debug("gconfig=%1", gconfig);

    /* save values to check for changes later */
    gconfig_init = gconfig;
    aconfig_init = aconfig;

    /* readin Userdatabase */

    Users::SetGUI (false);
    Users::Read ();
    Users::ReadNewSet ("nis");
    y2debug("UsersCache::usernames (local): %1", UsersCache::GetUsernames("local"));

    users = (list<string>) merge (
	UsersCache::GetUsernames("local"),
	UsersCache::GetUsernames("nis")
    );
    Users::SetGUI (true);

    y2debug("users=%1", users);

    return true;
}

/**
 * Write fax settings and apply changes
 * @return true if success
 */
global define boolean Write() {

    if(!modified) return true;
    y2milestone("Writing configuration");

    /* Check if there is anything to do */
    if((aconfig_init == aconfig) && (gconfig_init == gconfig)) {
	y2debug("config not modified");
	return true;
    }

    Package::InstallAll(need_pkgs);

    /* build a list of deleted entries */
    list<string> deleted = [];
    foreach(string u, any m, aconfig_init, {
	if (aconfig[u]:$[] == $[])
	    deleted = add(deleted, u);
    });
    y2debug("deleted %1", deleted);

    /* create if not exists, otherwise backup */
    if(SCR::Read(.target.size, conf_file) < 0)
	SCR::Write(.target.string, conf_file, "");
    else
	SCR::Execute(.target.bash, "/bin/cp "+conf_file+" "+conf_file+".YaST2save");

    any ret = false;

    /* update the global config */
    path cf = add(.phone-services.v, conf_file);
    path cfs = add(cf, "GLOBAL");

    foreach(string k, any v, gconfig, {
	path p = add(cfs, k);
	ret = SCR::Write(p, v);
    });

    /* remove deleted user sections */
    if (deleted != []) {
	cf = add(.phone-services.section, conf_file);
	foreach (string d, deleted, {
	    cfs = add(cf, d);
	    ret = SCR::Write(cfs, nil);
	});
    }

    /* update the users config */
    cf = add(.phone-services.v, conf_file);
    foreach(string s, map m, aconfig, {
	cfs = add(cf, s);
	foreach(string k, any v, (map<string,any>) m, {
	    path p = add(cfs, k);
	    ret = SCR::Write(p, v);
	});
    });

    y2debug("write %1 ret %2",cfs, ret);

    /* flush it on disc */
    SCR::Write(.phone-services, nil);

    modified = false;
    if (ret == true) {
	/* start capisuite */
	if (SCR::Read(.target.size, "/etc/init.d/capisuite") > 0)
	{
		SCR::Execute(.target.bash, "/sbin/insserv -d capisuite");
		SCR::Execute(.target.bash, "/etc/init.d/capisuite start");
	}
	/* create a system wide kdeprintfaxrc if KDE is installed and no config exist */
	string command = 
	    "if [ -e /opt/kde3/share/config -a ! -e /opt/kde3/share/config/kdeprintfaxrc ]; then " +
		"echo \"[System]\" > /opt/kde3/share/config/kdeprintfaxrc; " +
		"echo \"HylaFax=/usr/bin/capisuitefax -d %number %files\" >> /opt/kde3/share/config/kdeprintfaxrc; " +
		"echo \"System=hylafax\" >> /opt/kde3/share/config/kdeprintfaxrc; " +
	    "fi";
	SCR::Execute(.target.bash, command);
    }
    return ret == true;
}

/* EOF */
}
