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

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

######################################################
# Configure extension creation parameters            #
######################################################
# Variables
TODAY=`date +%Y/%m/%d`
PACKAGE="gmp"
VERSION="6.2.0"
DESCRIPTION="The GNU Multiple Precision Arithmetic Library"
DEVDESCRIPTION="Development files part of the GNU Multiple Precision Arithmetic Library"
AUTHORS="various (http://gmplib.org/manual/Contributors.html#Contributors)"
HOMEPAGE="https://gmplib.org/"
LICENSE="GNU LGPL v3 and GNU GPL v2"
ME="rhermsen"
TAGS="gmp arbitrary precision arithmetic"
DEVTAGS="development gmp arbitrary precision arithmetic"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}

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

# Source
mkdir /tmp/${PACKAGE}
cd /tmp/${PACKAGE}
wget https://ftp.gnu.org/gnu/${PACKAGE}/${PACKAGE}-${VERSION}.tar.xz
tar -xJf ${PACKAGE}-$VERSION.tar.xz
cd ${PACKAGE}-${VERSION}

######################################################
# Compile extension                                  #
######################################################
CFLAGS='-march=i486 -mtune=i686 -Os -pipe'
CXXFLAGS='-march=i486 -mtune=i686 -Os -pipe'
ABI=32

./configure \
--prefix=/usr/local \
--enable-cxx \
--host=i486-pc-linux-gnu







make
mkdir -p $DESTDIR
make DESTDIR=$DESTDIR install

sudo rm -r ${TMPDIR}* 2>/dev/null
mkdir -p  ${TMPDIR}/usr/local/lib
mkdir -p $TMPDIR/usr/local/share/doc/${PACKAGE}/

mv $DESTDIR/usr/local/lib/*.so* ${TMPDIR}/usr/local/lib/
cp /tmp/${PACKAGE}/${PACKAGE}-${VERSION}/COPYING $TMPDIR/usr/local/share/doc/${PACKAGE}/
cp /tmp/${PACKAGE}/${PACKAGE}-${VERSION}/COPYING.LESSERv3 $TMPDIR/usr/local/share/doc/${PACKAGE}/
cp /tmp/${PACKAGE}/${PACKAGE}-${VERSION}/COPYINGv2 $TMPDIR/usr/local/share/doc/${PACKAGE}/
cp /tmp/${PACKAGE}/${PACKAGE}-${VERSION}/COPYINGv3 $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:   robc/juanito
Tags:           ${TAGS}
Comments:       gnu mp is a library for arbitrary precision arithmetic, operating on
                signed integers, rational numbers, and floating point numbers
                This extension is PPI compatible.
                
Change-log:     2009/05/06 Original
                2009/06/03 Updated to 4.3.1, rebuilt for TC 2.X
                2009/06/03 re-compiled to add libgmpxx and used gcc-4.4.0 so that all gmpxx tests pass
                2009/07/24 recompiled for tc3 and updated -> 4.3.2
                2010/03/05 recompiled for tc4, split out devs and updated 4.3.2 -> 5.0.2
                2011/08/16 added libgmp.so.3 symlink for backwards compatibility
                2012/04/21 recompiled for i486 compatibility
                2013/03/23 updated 5.08 -> 5.1.1
                2014/09/18 updated 5.1.1 -> 6.0.0a
                2015/10/02 recompiled on tc-7.x
                2016/10/20 updated 6.0.0a -> 6.1.1
                2017/12/09 updated 6.1.1 -> 6.1.2
                2018/11/04 recompiled on tc-10.x
                2019/11/29 recompiled on tc-11.x
                ${TODAY} updated version, ${VERSION} (${ME})
Current:        ${TODAY} updated version, ${VERSION} (${ME})
EOF

# Delete compilation work directory
cd ..
#rm -r -f $WRKDIR

# Adjust directory access rigths
find $TMPDIR/ -type d | xargs chmod -v 755;

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

###################################################
# Create info file                                #
###################################################
cd /tmp/submit/
cat <<EOF> ${PACKAGE}-dev.tcz.info
Title:          ${PACKAGE}-dev.tcz
Description:    ${DEVDESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   juanito
Tags:           ${DEVTAGS}
Comments:       gnu mp is a library for arbitrary precision arithmetic, operating on
                signed integers, rational numbers, and floating point numbers
                This extension is PPI compatible.

                Dev files
                
Change-log:     2011/08/16 first version
                2012/04/21 recompiled for i486 compatibility
                2013/03/23 updated 5.08 -> 5.1.1
                2014/09/18 updated 5.1.1 -> 6.0.0a
                2015/10/02 recompiled on tc-7.x
                2016/10/20 updated 6.0.0a -> 6.1.1
                2017/12/09 updated 6.1.1 -> 6.1.2
                2018/11/04 recompiled on tc-10.x
                2019/11/29 recompiled on tc-11.x
                ${TODAY} updated version, ${VERSION} (${ME})
Current:        ${TODAY} updated version, ${VERSION} (${ME})
EOF

mkdir -p $TMPDIR-dev/usr/local/lib/pkgconfig

mv $DESTDIR/usr/local/lib/pkgconfig/*.pc ${TMPDIR}-dev/usr/local/lib/pkgconfig
mv $DESTDIR/usr/local/lib/*.la $TMPDIR-dev/usr/local/lib/
mv $DESTDIR/usr/local/lib/*.a $TMPDIR-dev/usr/local/lib/
mv $DESTDIR/usr/local/include $TMPDIR-dev/usr/local

###################################################                                                              
# Create .dep file                                #                                                              
###################################################                                                              
cat <<EOF> /tmp/submit/${PACKAGE}-dev.tcz.dep                                                                      
${PACKAGE}.tcz
EOF

###################################################
# Create base extension in temp dir               #
###################################################

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
#sudo chown -R tc:staff $TMPDIR/usr/local/etc/${PACKAGE}
#sudo chown -R root:staff $TMPDIR/usr/local/tce.installed
#sudo chmod -R 775 $TMPDIR/usr/local/tce.installed
#sudo chown -R tc:staff $TMPDIR/usr/local/tce.installed/*
#sudo chmod -R 755 $TMPDIR/usr/local/tce.installed/*

cd ..
mksquashfs $TMPDIR ${PACKAGE}.tcz
cd $TMPDIR
sudo sh -c "find usr -not -type d > ${PACKAGE}.tcz.list"
sudo mv ../${PACKAGE}.tcz* .
#sudo mv /tmp/${PACKAGE}/${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 dev extension in temp dir                #                                                              
###################################################                                                              
                                                                                                                 
cd $TMPDIR-dev                                                                                                   
find $TMPDIR-dev -perm 777 -exec chmod 755 {} \;                                                                 
find $TMPDIR-dev -perm 555 -exec chmod 755 {} \;                                                                 
find $TMPDIR-dev -perm 444 -exec chmod 644 {} \;                                                                 
find $TMPDIR-dev -perm 666 -exec chmod 644 {} \;                                                                 
find $TMPDIR-dev -perm 664 -exec chmod 644 {} \;                                                                 
sudo chown -R root:root $TMPDIR-dev                                                                              
cd ..                                                                                                            
mksquashfs $TMPDIR-dev ${PACKAGE}-dev.tcz                                                                           
cd $TMPDIR-dev                                                                                                   
sudo sh -c "find usr -not -type d > ${PACKAGE}-dev.tcz.list"                                                        
sudo mv ../${PACKAGE}-dev.tcz* .                                                                                     
#sudo mv /tmp/${PACKAGE}/${PACKAGE}-dev.tcz.info .                                                                  
                                                                                                                 
# Create md5 file                                                                                                
sudo sh -c "md5sum ${PACKAGE}-dev.tcz > ${PACKAGE}-dev.tcz.md5.txt"                                                    
                                                                                                                 
# Cleanup temp directory                                                                                         
sudo rm -r -f usr

