#!/usr/bin/perl
# cpanel - autofixer2/fix_shebangs                 Copyright(c) 2013 cPanel, Inc.
#                                                            All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use strict;

BEGIN { unshift @INC, "/usr/local/cpanel"; }

use Cpanel::Version          ();
use Cpanel::Version::Compare ();

if ( Cpanel::Version::Compare::compare( Cpanel::Version::get_version_full(), "<", "11.36" ) ) {
    print "Nothing to do.";
    exit 0;
}

my @files = qw(
  /usr/local/cpanel/whostmgr/docroot/cgi/addon_modsec.cgi
  /usr/local/cpanel/scripts/addservlets2
  /usr/local/cpanel/scripts/gentomcatlist2
  /usr/local/cpanel/scripts/remservlets
  /usr/sbin/starttomcat
  /usr/sbin/stoptomcat
  /etc/cron.hourly/modsecparse.pl
);

my $nupdated = 0;

foreach my $file (@files) {
    next unless -e $file;

    open( my $fh, "<", $file ) or next;
    my @lines = <$fh>;
    close($fh);

    next unless $lines[0] =~ m{^#!/usr/bin/perl};
    $lines[0] =~ s{^#!/usr/bin/perl}{#!/usr/local/cpanel/3rdparty/bin/perl};

    open( my $wfh, ">", $file ) or next;
    print {$wfh} @lines;
    close($wfh);
    $nupdated++;
}
print "Successfully updated affected scripts.\n" if $nupdated;
