#!/usr/bin/eperl -w
######################################################################
#
# Copyright (c) Ensim Corporation 2000, 2001   All Rights Reserved.
#
# This software is furnished under a license and may be used and copied
# only  in  accordance  with  the  terms  of such  license and with the
# inclusion of the above copyright notice. This software or any other
# copies thereof may not be provided or otherwise made available to any
# other person. No title to and ownership of the software is hereby
# transferred.
#
# The information in this software is subject to change without notice
# and  should  not be  construed  as  a commitment by Ensim Corporation.
# Ensim assumes no responsibility for the use or  reliability  of its
# software on equipment which is not supplied by Ensim.
#
######################################################################

use strict;
use Getopt::Std;

use constant SVC_UNHIDDEN_DIR   => "/etc/appliance/svcdb/unhidden";
use constant SVC_HIDDEN_DIR => "/etc/appliance/svcdb/hidden";
use constant SX_SVCPARSER   => "/usr/bin/ensim-python /usr/bin/svcparser.pyc";
use constant SVC_DEFAULT_PLAN_DIR => "/etc/virtualhosting/plans/default";


sub report($$$) {
    my $ok = shift;
    my $service = shift;
    my $action = shift;

    if ($ok) {
	print "[ SUCCESS ] Service database entry for $service $action.\n";
	exit 0;
    } else {
	print "[ FAILURE ] Service database entry for $service not $action.\n";
	exit 1;
    }
}

sub findServiceFile ($) {
    my $svcname = shift;

    if (-e SVC_UNHIDDEN_DIR . "/$svcname") {
	return SVC_UNHIDDEN_DIR . "/$svcname";
    } elsif (-e SVC_HIDDEN_DIR . "/$svcname") {
	return SVC_HIDDEN_DIR . "/$svcname";
    } 
    
    return;
}


my $usage="edit_svcdb { -i | -d } service_name\n";

my(%options);
die $usage unless ( Getopt::Std::getopts("id",\%options) );

my $svcname = shift or die $usage;

if (! scalar %options) {
    die $usage;
}

if ($options{i}) {
    # handle install
    my $opOK = 0;
    my $targetFile = findServiceFile ($svcname);

    if (!$targetFile) {
        # this would be a new service, so it should be automatically unhidden
        $opOK = open STATE_FILE, ">" . SVC_UNHIDDEN_DIR . "/$svcname";
        if ($opOK) {
            close STATE_FILE;
        }
    } else {
        $opOK = 1;
        # what's happening during an upgrade, is that the default plan files
        # get overwritten by the webppliance-<svc> rpms, which is OK, except
        # for hidden services, in which case we have to make sure that the 
        # service is actually disabled in the default plan. 
        # what follows is a check to see whether or not the service is hidden
        # if it is, then sed s/enabled = 1/enabled =0/ in the default plan

        my $svc_hidden_dir = SVC_HIDDEN_DIR ;
        my $svc_default_plan_dir = SVC_DEFAULT_PLAN_DIR;
        my $subst_pattern = "s/enabled *= *1/enabled = 0/";
        if ($targetFile =~ /^$svc_hidden_dir/) {
            `perl -pi -e '$subst_pattern' $svc_default_plan_dir/$svcname` ;
        }
    }
    
    report ($opOK, $svcname, 'installed.');

} elsif ($options{d}) {
    # handle uninstall
    my $targetFile = findServiceFile ($svcname);
    my $opOK = 1;
    
    if ($targetFile) {
	unlink $targetFile;
	$opOK = !(-e $targetFile);
    }

    report ($opOK, $svcname, 'uninstalled');
}

