#!/bin/sh
eval 'if [ -x /usr/local/cpanel/3rdparty/bin/perl ]; then exec /usr/local/cpanel/3rdparty/bin/perl -x -- $0 ${1+"$@"}; else exec /usr/bin/perl -x $0 ${1+"$@"}; fi;'
if 0;

#!/usr/bin/perl
#
# This script is part of the http://cpgs.cpanel.net/ addon.

my $debug=0;
my %maxallowed;
my %processes;
my %enabled;
my $runningcnt=0;

my %loaded_games;
my $loaded_cnt = 0;

my $logfile = '/var/log/cpgsd.log';

push(@INC,'/var/cpanel/cpgs','/var/cpanel/cpgs/games');

if(-d '/var/cpanel/cpgs/games') {
    if(opendir(my $games_dir,'/var/cpanel/cpgs/games')) {
        my @supported_games = readdir($games_dir);
        foreach my $file(@supported_games) {
            if(-f "/var/cpanel/cpgs/games/$file/$file\.pm") {
                eval "use ${file}::${file}";
                if($@) {
                    msglog("Could not load module from \"/var/cpanel/cpgs/games/$file/$file\.pm\" : $@");
                } else {
                    if ( ref( "cPGS::${file}"->can('register_self') ) ) {
                        (undef,$loaded_games{'$file'}{'pkg'},$loaded_games{$file}{'name'}, $loaded_games{$file}{'version'}, $loaded_games{$file}{'descr'}) = "cPGS::$file"->register_self;
                        $loaded_cnt++;
    #                   my $numero = sprintf("%02d",$loaded_cnt);
    #                   my $statline = "[$numero] Registered support for $loaded_games{$file}{'descr'} via $loaded_games{'$file'}{'pkg'} $loaded_games{$file}{'version'}";
    #                   msglog("Line(". __LINE__ .") $statline");
                    }
                }
            }
        }
    }
}

# find max # of allowed servers
if( open( my $cfg_fh,'<','/var/cpanel/gameserv.cfg') ) {
    while(<$cfg_fh>) {
        if(m/(.*)\_maxallowed\|(\d+)/) {
            $maxallowed{$1}=$2;
            $enabled{$1}=$2;
        }
    }
} else {
    msglog("Could not read from /var/cpanel/gameserv.cfg : $!");
    exit;
}

if(open(my $restart_fh,'/var/cpanel/gameserv_restart')) {
    my @gs=(<$restart_fh>);
    close($restart_fh);
    foreach my $line(@gs){
        my $pidfile='';
        my $gdir='';
        if($line =~ /^([a-z\d]+)\_([\w\d\_]+)\_(\d+)$/) {
            my $user = $1;
            my $game = $2;
            my $srvn = $3;

            my @PW = getpwnam($user);
            if(!@PW[0]) {
                msglog("Could not find user \"$user\" on system, removing from /var/cpanel/gameserv_restart.");
                if(open(my $rest_fh,'>','/var/cpanel/gameserv_restart')) {
                    foreach my $ln(@gs) {
                        print $rest_fh "$ln" unless $ln =~ m/^$user\_/;
                    }
                    close($rest_fh);
                } else {
                    msglog("Could not open /var/cpanel/gameserv_restart for writing : $!");
                }
                next;
            }
            if(defined($loaded_games{$game}{'name'})) {
                my $m = "cPGS\:\:$game";
                if(ref($m->can("get_dir_and_pid"))) {
                    eval { ($gdir, $pidfile) = $m->get_dir_and_pid(); };
                    if($@) {
                        msglog("Line(". __LINE__ .") Could not call $m\:\:get_dir_and_pid : $! , $@ ");
                    }
                }
            }
            my $home = @PW[7];
            $gdir = $gdir ? $gdir : $game;

            # if the game isn't installed where it should be, it's safe to remove it from the restart file
            if(! -d "$home/.gameservers/$gdir\_$srvn") {
                if(open(my $rest_fh,'>','/var/cpanel/gameserv_restart')) {
                    foreach my $ln(@gs) {
                        print $rest_fh "$ln" unless $ln =~ m/^$user\_$game\_$srvn/;
                    }
                    close($rest_fh);
                } else {
                    msglog("Could not open /var/cpanel/gameserv_restart for writing : $!");
                }
                next;
            }

            my $ret = system('/var/cpanel/cpgs/cpgs_server_ctl','start',$user,$game,$srvn,1); # trailing one signifies a restart
        }
    }
} else {
    msglog("Could not open /var/cpanel/gameserv_restart : $!");
    if(! -e '/var/cpanel/gameserv_restart') {
        if(open(my $rest,'>>','/var/cpanel/gameserv_restart')) {
            close($rest);
        } else {
            msglog("Could not create empty /var/cpanel/gameserv_restart : $!");
        }
    }
    exit(0);
}

sub msglog {
    my($msg)=@_;
    my $time=time;
    my $localtime=localtime($time);
    if(open(my $log_fh,'>>',$logfile)) {
        print $log_fh "\[$localtime\] ($0) " . $msg . "\n";
        close($log_fh);
    }
}

