#!/usr/bin/env perl

use warnings;
use strict;
use util;

my $hostname = `hostname`;
chomp $hostname;
$hostname = $ARGV[0] if @ARGV >= 1;

print "HOSTNAME = $hostname\n";

if ( !defined( $hostname ) || $hostname eq "" ) {
    print "USAGE: pdspkgs hostname [-v]\n";
    exit 1;
}

my @srvs;

for ( getkeys( $hostname ) ) {
    push( @srvs, loadList( "srv", $_ ) );
}

sub fail {
    print STDERR "ERROR: failed for service $_[0]\n";
    exit 1;
}

for my $srv ( @srvs ) {
    if ( $srv =~ m/^!(.*)/ ) {
        system_( "systemctl disable $srv" ) or fail( $srv );
        system_( "systemctl stop $srv" );
    } else {
        system_( "systemctl enable $srv" ) or fail( $srv );
        system_( "systemctl start $srv" );
    }
}
