#!/usr/bin/env perl
# editmake-libplib - To be documented.
# License:  Creative Commons Attribution-NonCommercial-ShareAlike 2.5
# Revision: 070818

# Note:  The license indicated above applies to this file.  It doesn't
# apply to the official "libplib" package, or to any file derived from
# that package.

#---------------------------------------------------------------------
#                         license information
#---------------------------------------------------------------------

# This section may not be modified except as approved by the author or
# licensor, or to make non-content changes such as adjustments to par-
# agraph formatting or white space.

# This  version of this software is  distributed  under  the following
# license:
#
#       Creative Commons Attribution-NonCommercial-ShareAlike 2.5

# You may use, modify, and redistribute this software without fees  or
# royalties, but only under the terms and conditions set forth  by the
# license.  In particular, copies and derived works cannot be used for
# commercial purposes.  Additionally, the license propagates to copies
# and derived works. Furthermore, you must provide attribution "in the
# manner  specified  by the  author or licensor".  The latter point is
# discussed below.

# The author [and licensor] hereby specifies that  attribution must be
# handled in the following manner:  a. If the software is interactive,
# any  About or Credits dialog boxes, windows, or output text provided
# by the  original version must be preserved and readily accessible to
# the end user at runtime.  b. If the software is non-interactive,  or
# if it does not  provide  About or Credits dialog boxes, windows,  or
# output text,  the  operating system and/or  desktop environment used
# must provide attribution that is  visible and/or  readily accessible
# to the end user at runtime.

# The following techniques  do not meet the  attribution requirements:
# Attribution through text  files, attribution  through  printed docu-
# mentation,  verbal  attribution, or postings on  external  web sites
# [i.e.,  web  sites  that are not an intrinsic local component of the
# operating system or  desktop environment used].  These  examples are
# provided for illustrative purposes only.

# It should be noted that trademarks are an additional issue.  If this
# software  uses any  trademarks,  trademark-related  restrictions may
# apply.

# This is not a  complete explanation of the  terms and conditions in-
# volved.  For more information, see the Creative Commons Attribution-
# NonCommercial-ShareAlike 2.5 license.

#---------------------------------------------------------------------
#                           important note
#---------------------------------------------------------------------

# This program is  provided on an  AS IS basis with ABSOLUTELY NO WAR-
# RANTY. The entire risk as to the quality and performance of the pro-
# gram is with you. Should the program prove defective, you assume the
# cost of all necessary  servicing, repair or correction.  In no event
# will any of the developers, or any other party,  be liable to anyone
# for damages arising out of  use of the program,  or inability to use
# the program.

#---------------------------------------------------------------------
#                        standard module setup
#---------------------------------------------------------------------

require 5.8.0;
use strict;
use Carp;
use warnings;
                                # Trap warnings
$SIG {__WARN__} = sub { die @_; };

#---------------------------------------------------------------------
#                        load LACSUB module[s]
#---------------------------------------------------------------------

use LACSUB::Base
 ( @LACSUB::Base::EXPORT_OK       );
 
use LACSUB::Filesystem
  ( @LACSUB::Filesystem::EXPORT_OK );

#---------------------------------------------------------------------
#                           basic constants
#---------------------------------------------------------------------

use constant ZERO  => 0;        # Zero
use constant ONE   => 1;        # One

use constant FALSE => 0;        # Boolean FALSE
use constant TRUE  => 1;        # Boolean TRUE

#---------------------------------------------------------------------
#                            main routine
#---------------------------------------------------------------------

sub Main
{
    my $orig_data;              # Copy of original data
    my @list = ();

    &SearchDirTree
    (
        -path      => '.'     ,
        -ref_list  => \@list  ,
        -only_text => TRUE    ,
        -sort      => 'name'
    );

    for my $path (@list)
    {
        next unless $path =~ m@\bMakefile@i;
        $_         = &ReadFile (-path => $path);
        $orig_data = $_;

        s@-g (-O\d)\b@-fPIC $1@g;
        next if $_ eq $orig_data;

        &WriteFile
        (
            -path     => $path ,
            -ref_data => \$_
        );

        print "Made PIC-related changes to $path\n";
    }

    undef;
}

#---------------------------------------------------------------------
#                            main program
#---------------------------------------------------------------------

&Main();                        # Call the main routine
exit ZERO;                      # Normal exit
