#!/usr/bin/perl
# cpanel - cp_util/update_php_data_in_profiles    Copyright(c) 2011 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;
use warnings;

use lib '/usr/local/cpanel';
use Cpanel::DataStore ();
use Cpanel::TempFile  ();

my $profile_root = 'profiles/easy/apache/profile';
die "Make sure '.' is the easyapache repo." if !-d $profile_root;

eval '
   use Cpanel::Easy::PHP4;
   use Cpanel::Easy::PHP5;
';
die "Make sure '.' is the easyapache repo: $@" if $@;

my @php4 = Cpanel::Easy::PHP4->versions();
my @php5 = Cpanel::Easy::PHP5->versions();

my $profile_comment = '# make sure any changes here are reflected in Cpanel::Easy::Apache::get_apache_defaults_text() if necessary';
my %profiles        = (
    'custom/cpanel_default.yaml'     => $profile_comment,
    'custom/cpanel_php_sec.yaml'     => $profile_comment,
    'custom/cpanel_php_img.yaml'     => $profile_comment,
    'custom/cpanel_php_enc_img.yaml' => $profile_comment,
    'custom/cpanel_php_enc.yaml'     => $profile_comment,
    'custom/cpanel_no_php.yaml'      => $profile_comment,
    'makecpphp.profile.yaml'         => '#### ATTN EDIT ##
## any changes to options here need to be reflected in Cpanel::Easy::PHP5::cPPHPOpts
##### ATTN EDIT ##',
);

help() if grep /\-\-help/, @ARGV;
help('Requires 2 arguments.') if @ARGV != 2;

my ( $php4_def, $php5_def ) = @ARGV;
$php4_def ||= $php4[-1];
$php5_def ||= $php5[-1];
if ( !grep( { $php4_def eq $_ } @php4 ) || !grep( { $php5_def eq $_ } @php5 ) ) {
    help("Invalid version arguments.");
}

my $tmpobj = Cpanel::TempFile->new();

for my $profile ( sort keys %profiles ) {

    # Of the seven profiles, the makecpphp profile is special, in that the
    # selected version is 'sticky'. In other words, although we add any new php
    # versions to the list, if there was a selected version before the update,
    # it must remain the selected version. Strictly speaking, this should only
    # be the the case when the selected version still actually exists, but we
    # won't differentiate here; see case 56546 for rationale.

    my $special = $profile eq 'makecpphp.profile.yaml';
    my %keep_selected;

    my $yaml_hr = Cpanel::DataStore::load_ref("$profile_root/$profile") or die "Could not read “$profile_root/$profile”: $!";

    for my $k ( keys %{$yaml_hr} ) {
        if ( $k =~ m/^Cpanel::Easy::PHP(4)::(\d+(?:\_\d+)?)$/ || $k =~ m/^Cpanel::Easy::PHP(5)::(\d+(?:\_\d+)?)$/ ) {
            my $four_or_five = $1;
            my $phpversion   = $2;
            if ($special) {
                if ( $yaml_hr->{$k} ) {    # It is the selected version
                    $keep_selected{$four_or_five} = $phpversion;
                }
                else {
                    delete $yaml_hr->{$k};
                }
            }
            else {
                delete $yaml_hr->{$k};
            }
        }
    }

    for my $four (@php4) {
        unless ( $four eq ( $keep_selected{4} || '' ) ) {
            $yaml_hr->{"Cpanel::Easy::PHP4::$four"} = $four eq $php4_def && !exists $keep_selected{4} ? 1 : 0;
        }
    }
    for my $five (@php5) {
        unless ( $five eq ( $keep_selected{5} || '' ) ) {
            $yaml_hr->{"Cpanel::Easy::PHP5::$five"} = $five eq $php5_def && !exists $keep_selected{5} ? 1 : 0;
        }
    }

    my $tmpfile = $tmpobj->file();
    Cpanel::DataStore::store_ref( $tmpfile, $yaml_hr ) or die "Could not write “$tmpfile”: $!";

    open my $rfh, '<', $tmpfile or die "Could not read “$tmpfile”: $!";
    open my $wfh, '>', "$profile_root/$profile" or die "Could not write “$profile_root/$profile”: $!";

    print {$wfh} "$profiles{$profile}\n";
    while ( my $line = <$rfh> ) {
        print {$wfh} $line;
    }

    close $rfh;
    close $wfh;
}

sub help {
    my ($error) = @_;
    print "Error: $error\n\n" if $error;
    print <<"END_HELP";
Usage: $0 php4 php5

   Where "php4" is one of these: "" @php4
   Where "php5" is one of these: "" @php5

   Note: An empty string means “use the latest”, but it must explicitly be given to avoid ambiguity.

   --help - this screen
END_HELP
    exit( $error ? 1 : 0 );
}
