#!/usr/bin/perl

# cpanel - generate_freebsd_php_ldflags_patch.pl  Copyright(c) 2012 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

# This script is used to generate patch 'php5-freebsd-ldflags.patch'. Whenever
# you upgrade php 5 in easyapache, run this generate script from within
# /home/cpeasyapache/src/php.5.x.x. It will modify files in place within that
# dir. Now diff the new /home/cpeasyapache/src/php5.x.x against snapshot
# /home/cpeasyapache/src/php.5.x.x.orig (which you would have saved earlier).
# Use 'diff -r -U 3' to generate a patch, which is now the new contents of
# php5-freebsd-ldflags.patch.
# Run Easyapache with php5 selected. Verify that the build completes
# successfully. In particular, verify that the php5-freebsd-ldflags.patch patch
# applies successfully within the step 'applying compatability patches' which is
# within the opt 'PHP 5.3.9 support'.

unless ( -e 'configure' && -e 'configure.in' && -e 'acinclude.m4' && -e 'aclocal.m4' ) {
    die "Change to target PHP source directory before running this script";
}

foreach my $target ( 'acinclude.m4', 'aclocal.m4', 'configure.in', 'configure' ) {
    open my $target_fh, '+<', $target || die "Could not open $target for read/write: $!";
    my @contents = readline($target_fh);
    seek( $target_fh, 0, 0 );
    my $in_php_add_libpath_definition = 0;
    my $in_php_ldflags_add_usr_lib    = 0;
    my $in_freebsd_libtool            = 0;

    print "Rewriting $target...\n";
    foreach my $ln (@contents) {
        if ($in_php_add_libpath_definition) {

            # First line of the PHP_ADD_LIBPATH function is the one to change
            $ln                            = '  if test "$1" != "/usr/$PHP_LIBDIR" && test "$1" != "/usr/lib" && test "$1" != "/usr/local/lib"; then' . "\n";
            $in_php_add_libpath_definition = 0;
        }
        elsif ($in_php_ldflags_add_usr_lib) {

            # Same with php_ldflags_add_usr_lib logic
            $ln                         = '  PHP_RPATHS="$PHP_RPATHS /usr/local/lib /usr/lib"' . "\n";
            $in_php_ldflags_add_usr_lib = 0;
        }
        elsif ( $ln =~ /^\s*AC_DEFUN\(\[PHP_ADD_LIBPATH\]/ ) {
            $in_php_add_libpath_definition = 1;
        }
        elsif ( $ln =~ /^\s*if test -n "\$php_ldflags_add_usr_lib"; then\s*$/ ) {
            $in_php_ldflags_add_usr_lib = 1;
        }
        elsif ( $target eq 'configure' ) {

            # Here we rewrite configure as if autoconf had run with those changes in place
            if ($in_freebsd_libtool) {
                $ln                 = qq{    sys_lib_dlsearch_path_spec="/lib /usr/lib /usr/local/lib"\n} . $ln;
                $in_freebsd_libtool = 0;
            }
            elsif ( $ln =~ /^(\s*)ac_link='\$\{CC-cc\} -o conftest\$\{ac_exeext\} \$CFLAGS \$CPPFLAGS \$LDFLAGS conftest.\$ac_ext \$LIBS 1>\&5'\s*$/ ) {
                $ln = $1 . q{ac_link='${CC-cc} -o conftest${ac_exeext} $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS -L/usr/local/lib 1>&5'} . "\n";
            }
            elsif ( $ln =~ /^(\s*)ac_link='\$\{CXX-g\+\+\} -o conftest\$\{ac_exeext\} \$CXXFLAGS \$CPPFLAGS \$LDFLAGS conftest.\$ac_ext \$LIBS 1>\&5'\s*$/ ) {
                $ln = $1 . q{ac_link='${CXX-g++} -o conftest${ac_exeext} $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS -L/usr/local/lib 1>&5'} . "\n";
            }
            elsif ( $ln =~ m{^(\s*)if test "(\S*)" != "/usr/\$PHP_LIBDIR" && test "\S*" != "/usr/lib"; then\s*$} ) {
                $ln = $1 . q{if test "} . $2 . q{" != "/usr/$PHP_LIBDIR" && test "} . $2 . q{" != "/usr/lib" && test "} . $2 . q{" != "/usr/local/lib"; then} . "\n";
            }
            elsif ( $ln =~ m/^\s*\*\) # from 3\.2 on\s*$/ || $ln =~ m/^\s*\*\) # from 4\.6 on, and DragonFly\s*$/ ) {
                $in_freebsd_libtool = 1;
            }
        }
        print $target_fh $ln;
    }
    truncate( $target_fh, tell($target_fh) );
    close $target_fh;
}

