#!/bin/sh
#
######################################################
# Build script for Core 13.x                          #
#                                                    #
# See .info for details                              #
######################################################

######################################################
# Prepare extension creation                         #
######################################################
#download and install perl
tce-load -wi perl5.tcz
#download and install the compile tools
tce-load -wi compiletc.tcz
tce-load -wi squashfs-tools.tcz

#tce-load -i XML-NamespaceSupport.tcz
#tce-load -i XML-SAX-Base.tcz
tce-load -i perl_XML-NamespaceSupport.tcz
tce-load -i perl_XML-SAX-Base.tcz

######################################################
# Configure extension creation parameters            #
######################################################
# Variables
TODAY=`date +%Y/%m/%d`
#PACKAGE="XML-SAX"
DOWNLOAD="XML-SAX"
PACKAGE="perl_XML-SAX"
VERSION="1.02"
DESCRIPTION="perl xml::sax module"
AUTHORS="Grant McLean"
# HOMEPAGE="http://search.cpan.org/~grantm/XML-SAX/SAX.pm"
HOMEPAGE="https://metacpan.org/pod/XML::SAX"
LICENSE="See Original-site"
ME="rhermsen"
TAGS="perl xml sax"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}
PERLVER=5.32.0

# Workdir
sudo rm -r /tmp/${PACKAGE} 2>/dev/null
mkdir /tmp/${PACKAGE}
cd /tmp/${PACKAGE}

# Source
wget http://search.cpan.org/CPAN/authors/id/G/GR/GRANTM/${DOWNLOAD}-${VERSION}.tar.gz

tar xf /tmp/${PACKAGE}/${DOWNLOAD}-${VERSION}.tar.gz
cd /tmp/${PACKAGE}/${DOWNLOAD}-${VERSION}

######################################################
# Compile extension                                  #
######################################################
sed -i 's/(PERL)/(PERLRUNINST)/g' /tmp/${PACKAGE}/${DOWNLOAD}-${VERSION}/Makefile.PL
perl Makefile.PL
find . -name Makefile -type f -exec sed -i 's/-O2//g' {} \;
make
sudo rm -r ${DESTDIR} 2>/dev/null
mkdir -p  ${DESTDIR}

make DESTDIR=${DESTDIR} install
#sudo make install

sudo rm -r ${TMPDIR} 2>/dev/null
sudo rm -r ${TMPDIR}-doc 2>/dev/null
#mkdir -p  ${TMPDIR}/usr/local/lib/perl5/$PERLVER/i486-linux-thread-multi/
#mkdir -p  ${TMPDIR}/usr/local/lib/perl5/site_perl/$PERLVER/XML/SAX/PurePerl/
mkdir -p  ${TMPDIR}/usr/local/lib/perl5/site_perl/XML/SAX/PurePerl/Reader
mkdir -p ${TMPDIR}/usr/local/share/doc/${PACKAGE}/
mkdir -p ${TMPDIR}/usr/local/tce.installed

#mv ${DESTDIR}/usr/local/lib/perl5/$PERLVER/i486-linux-thread-multi/* ${TMPDIR}/usr/local/lib/perl5/$PERLVER/i486-linux-thread-multi/
mv ${DESTDIR}/usr/local/lib/perl5/site_perl/XML/*.pm ${TMPDIR}/usr/local/lib/perl5/site_perl/XML/
mv ${DESTDIR}/usr/local/lib/perl5/site_perl/XML/SAX/*.pm ${TMPDIR}/usr/local/lib/perl5/site_perl/XML/SAX/
mv ${DESTDIR}/usr/local/lib/perl5/site_perl/XML/SAX/PurePerl/*.pm ${TMPDIR}/usr/local/lib/perl5/site_perl/XML/SAX/PurePerl/
mv ${DESTDIR}/usr/local/lib/perl5/site_perl/XML/SAX/PurePerl/Reader/*.pm ${TMPDIR}/usr/local/lib/perl5/site_perl/XML/SAX/PurePerl/Reader/
#mv ${DESTDIR}/usr/local/lib/perl5/site_perl/$PERLVER/i486-linux-thread-multi/auto/XML/ ${TMPDIR}/usr/local/lib/perl5/site_perl/$PERLVER/i486-linux-thread-multi/auto/XML/
cp /tmp/${PACKAGE}/${DOWNLOAD}-${VERSION}/LICENSE $TMPDIR/usr/local/share/doc/${PACKAGE}/

###################################################
# Create info file                                #
###################################################
cd /tmp/submit/
cat <<EOF> ${PACKAGE}.tcz.info
Title:          ${PACKAGE}.tcz
Description:    ${DESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   ${ME}
Tags:           ${TAGS}
Comments:       XML::SAX
                XML::SAX is a SAX parser access API for Perl.
                It includes classes and APIs required for implementing 
                SAX drivers, along with a factory class for returning
                any SAX parser installed on the user's system.
Change-log:     2018/01/11 First Version, 0.99
                2018/01/14 recompiled on tc-9.x, 1.00
                2018/04/20 recompiled on tc-9.x with .pod moved to -doc, 1.00
                2019/02/02 recompiled on tc-10.x, 1.00
                ${TODAY} recompiled with Perl-${PERLVER} on tc-13.x, ${VERSION}
Current:        ${TODAY} recompiled with Perl-${PERLVER} on tc-13.x, ${VERSION}
EOF

###################################################
# Create .dep file                                #
###################################################
cat <<EOF> ${PACKAGE}.tcz.dep
perl_XML-NamespaceSupport.tcz
perl_XML-SAX-Base.tcz
EOF

###################################################
# Create install script file                      #
###################################################
cat <<EOF> $TMPDIR/usr/local/tce.installed/${PACKAGE}
#!/bin/sh
echo '[XML::SAX::PurePerl]' >> /usr/local/lib/perl5/site_perl/XML/SAX/ParserDetails.ini
echo 'http://xml.org/sax/features/namespaces = 1' >> /usr/local/lib/perl5/site_perl/XML/SAX/ParserDetails.ini
echo >> /usr/local/lib/perl5/site_perl/XML/SAX/ParserDetails.ini
EOF

mkdir -p  ${TMPDIR}-doc/usr/local/share/man/man3/
mkdir -p ${TMPDIR}-doc/usr/local/share/doc/${PACKAGE}-doc/
mkdir -p ${TMPDIR}-doc/usr/local/lib/perl5/site_perl/XML/SAX/
mkdir -p ${TMPDIR}-doc/usr/local/tce.installed

mv ${DESTDIR}/usr/local/share/man/man3/* ${TMPDIR}-doc/usr/local/share/man/man3/
mv ${DESTDIR}/usr/local/lib/perl5/${PERLVER::-2}/core_perl/perllocal.pod ${TMPDIR}-doc/usr/local/share/doc/${PACKAGE}-doc/
mv ${DESTDIR}/usr/local/lib/perl5/site_perl/XML/SAX/*.pod ${TMPDIR}-doc/usr/local/lib/perl5/site_perl/XML/SAX/

###################################################
# Create info file                                #
###################################################
cd /tmp/submit/
sudo cat <<EOF> ${PACKAGE}-doc.tcz.info
Title:          ${PACKAGE}-doc.tcz
Description:    ${DESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   ${ME}
Tags:           ${TAGS}
Comments:       XML::SAX
                XML::SAX is a SAX parser access API for Perl.
                It includes classes and APIs required for implementing 
                SAX drivers, along with a factory class for returning
                any SAX parser installed on the user's system.
Change-log:     2018/01/11 First Version, 0.99
                2018/01/14 recompiled on tc-9.x, 1.00
                2018/04/20 recompiled on tc-9.x with .pod moved to -doc, 1.00
                2019/02/02 recompiled on tc-10.x, 1.00
                ${TODAY} recompiled with Perl-${PERLVER} on tc-13.x, ${VERSION}
Current:        ${TODAY} recompiled with Perl-${PERLVER} on tc-13.x, ${VERSION}
EOF

###################################################
# Create install script file                      #
###################################################
cat <<EOF> ${TMPDIR}-doc/usr/local/tce.installed/${PACKAGE}-doc
#!/bin/sh
cat /usr/local/share/doc/${PACKAGE}-doc/perllocal.pod >> /usr/local/lib/perl5/${PERLVER::-2}/core_perl/perllocal.pod
EOF

find $TMPDIR/ -type d | xargs chmod -v 755;

find $TMPDIR | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded

cd $TMPDIR
find $TMPDIR -perm 777 -exec chmod 755 {} \;
find $TMPDIR -perm 555 -exec chmod 755 {} \;
find $TMPDIR -perm 444 -exec chmod 644 {} \;
find $TMPDIR -perm 666 -exec chmod 644 {} \;
find $TMPDIR -perm 664 -exec chmod 644 {} \;
sudo chown -R root:root $TMPDIR

cd /tmp/submit/

mksquashfs $TMPDIR ${PACKAGE}.tcz

cd $TMPDIR
sudo sh -c "find usr -not -type d > ${PACKAGE}.tcz.list"
sudo mv ../${PACKAGE}.tcz .
sudo mv ../${PACKAGE}.tcz.dep .
sudo mv ../${PACKAGE}.tcz.info .

# Create md5 file

sudo sh -c "md5sum ${PACKAGE}.tcz > ${PACKAGE}.tcz.md5.txt"

# Cleanup temp directory

sudo rm -r -f usr

# create documentation extension

cd $TMPDIR-doc
find $TMPDIR-doc -perm 777 -exec chmod 755 {} \;
find $TMPDIR-doc -perm 555 -exec chmod 755 {} \;
find $TMPDIR-doc -perm 444 -exec chmod 644 {} \;
find $TMPDIR-doc -perm 666 -exec chmod 644 {} \;
find $TMPDIR-doc -perm 664 -exec chmod 644 {} \;
sudo chown -R root:root $TMPDIR-doc

cd /tmp/submit/

mksquashfs $TMPDIR-doc ${PACKAGE}-doc.tcz

cd $TMPDIR-doc
sudo sh -c "find usr -not -type d > ${PACKAGE}-doc.tcz.list"
sudo mv ../${PACKAGE}-doc.tcz .
sudo mv ../${PACKAGE}-doc.tcz.info .

# Create md5 file

sudo sh -c "md5sum ${PACKAGE}-doc.tcz > ${PACKAGE}-doc.tcz.md5.txt"

# Cleanup temp directory

sudo rm -r -f usr

