#!/bin/sh
##
##  perl-openpkg -- OpenPKG utility for use in Perl module packages
##  Copyright (c) 2002-2003 Ralf S. Engelschall <rse@engelschall.com> 
##

#   hard-coded OpenPKG instance prefix
l_prefix="@l_prefix@"

#   make sure we are running in the RPM environment
if [ ".$RPM_BUILD_ROOT" = . ]; then
    echo "perl-openpkg:ERROR: \$RPM_BUILD_ROOT not set" 1>&2
    exit 1
fi
if [ ".$RPM_BUILD_DIR" = . ]; then
    echo "perl-openpkg:ERROR: \$RPM_BUILD_DIR not set" 1>&2
    exit 1
fi

#   helper function for logging
log () {
    echo "perl-openpkg: $*" 1>&2
}

cmd="$1"
shift
case $cmd in
    prolog )
        #   prepare Perl module installation area
        log "prepare Perl module installation area"
        rm -rf $RPM_BUILD_ROOT
        l_shtool=`${l_prefix}/bin/rpm --eval '%{l_shtool}'`
        ${l_shtool} mkdir -f -p -m 755 ${RPM_BUILD_ROOT}${l_prefix}/lib/perl

        #   prepare Perl executable wrapper
        log "prepare Perl executable wrapper"
        eval `${l_prefix}/bin/perl -V:installarchlib -V:installprivlib -V:installsitearch -V:installsitelib`
        perl="${RPM_BUILD_DIR}/perl"
        echo "#!/bin/sh" >$perl
        echo "exec ${l_prefix}/bin/perl \\" >>$perl
        echo " -I${RPM_BUILD_ROOT}${installarchlib}  \\" >>$perl
        echo " -I${RPM_BUILD_ROOT}${installprivlib}  \\" >>$perl
        echo " -I${RPM_BUILD_ROOT}${installsitearch} \\" >>$perl
        echo " -I${RPM_BUILD_ROOT}${installsitelib}  \\" >>$perl
        echo " \"\$@\"" >>$perl
        chmod a+x $perl
        ;;

    install )
        #   determine build parameters
        log "determine build parameters"
        perl="${RPM_BUILD_DIR}/perl"
        perl_args="PREFIX=${RPM_BUILD_ROOT}${l_prefix} INSTALLDIRS=site"
        perl_args="${perl_args} PERL=${perl} FULLPERL=${perl}"
        make=`${l_prefix}/bin/rpm --eval '%{l_make} %{l_mflags}'`
        make_args="PERL=${perl} FULLPERL=${perl}"

        #   optionally enter sub-directory of module
        oldpwd=`pwd`
        if [ ".$1" = ".-d" ]; then
            shift
            dir="$1"
            shift
            if [ -d $dir ]; then
                log "entering sub-directory $dir"
                cd $dir
            else
                dir=`echo "$dir" | sed -e 's;^.*/\([^/]*\)\.tar\.gz$;\1;' -e 's;^.*/\([^/]*\)\.tgz$;\1;'`
                if [ -d $dir ]; then
                    log "entering sub-directory $dir"
                    cd $dir
                fi
            fi
        fi

        #   configuring Perl module
        log "configuring Perl module"
        chmod -R u+rw Makefile.PL
        cp Makefile.PL Makefile.PL.orig
        sed -e "s:\$^X:'$perl':g" <Makefile.PL.orig >Makefile.PL
        $perl Makefile.PL ${1+"$@"} $perl_args </dev/null

        #   building Perl module
        log "building Perl module"
        $make $make_args pure_all

        #   installing Perl module
        log "installing Perl module"
        $make $make_args pure_install
        ;;

    epilog )
        #   pruning installation area
        log "pruning installation area"
        find ${RPM_BUILD_ROOT}${l_prefix} \
            -name perllocal.pod -print | xargs rm -f
        find ${RPM_BUILD_ROOT}${l_prefix} \
            -name .packlist -print | xargs rm -f
        find ${RPM_BUILD_ROOT}${l_prefix} \
            -type d -depth -print | (xargs rmdir >/dev/null 2>&1 || true)

        #   determining installation files
        log "determining installation files"
        eval `${l_prefix}/bin/perl -V:installarchlib -V:installprivlib -V:installsitearch -V:installsitelib`
        l_rpmtool=`${l_prefix}/bin/rpm --eval '%{l_rpmtool}'`
        eval ${l_rpmtool} files -v -ofiles -r${RPM_BUILD_ROOT} \
            `${l_prefix}/bin/rpm --eval '%{l_files_std}'` \
            "\"%not %dir ${l_prefix}/lib/perl\"" \
            "\"%not %dir ${l_prefix}/lib/perl/*\"" \
            "\"%not %dir $installarchlib\"" \
            "\"%not %dir $installprivlib\"" \
            "\"%not %dir $installsitearch\"" \
            "\"%not %dir $installsitelib\"" \
            "\"%not %dir $installarchlib/auto\"" \
            "\"%not %dir $installprivlib/auto\"" \
            "\"%not %dir $installsitearch/auto\"" \
            "\"%not %dir $installsitelib/auto\"" \
            "\"%not ${l_prefix}/man\""

        #   cleanup
        rm -f ${RPM_BUILD_DIR}/perl
        ;;
esac

