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

######################################################
# Prepare extension creation                         #
######################################################
#download and install dependencies
PYVER=3.9
# download and install python
tce-load -wi python${PYVER}-dev.tcz
# tce-load -wi libtirpc-dev.tcz
#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="talloc"
VERSION="2.4.3"
DESCRIPTION="talloc is a hierarchical, reference counted memory allocator"
DOCDESCRIPTION="Documentation part of talloc"
DEVDESCRIPTION="Development files part of talloc"
AUTHORS="Andrew Tridgell"
HOMEPAGE="https://talloc.samba.org/talloc/doc/html/index.html"
LICENSE="LGPLv3"
ME="rhermsen"
TAGS="talloc memory allocator"
DOCTAGS="man pages talloc memory allocator"
DEVTAGS="development talloc memory allocator"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}
SRCDIR=/tmp/src/${PACKAGE}
ARCH=`uname -m`

# Workdir
# sudo rm -r ${SRCDIR} 2>/dev/null
[ ! -d ${SRCDIR} ] && mkdir -p ${SRCDIR}

# Source
cd ${SRCDIR}
[ -d ${PACKAGE}-${VERSION} ] && rm -r ${PACKAGE}-${VERSION}
[ ! -f "${PACKAGE}-${VERSION}.tar.gz" ] && wget https://www.samba.org/ftp/${PACKAGE}/${PACKAGE}-${VERSION}.tar.gz
tar xzf ${PACKAGE}-${VERSION}.tar.gz

cd ${SRCDIR}/${PACKAGE}-${VERSION}


######################################################
# Compile extension                                  #
######################################################

# Export variables needed for compilation
ARCH=`uname -m`
if [ $ARCH == "i686" ]
then
echo
export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
# no specific arch dependent parameters
elif [ $ARCH == "x86_64" ]
then
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe"
echo
# no specific arch dependent parameters
else
exit 1
fi
export LDFLAGS="-Wl,-O1"
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

# Compile
cd /${SRCDIR}/${PACKAGE}-${VERSION}

# configure
./configure


# variable expansion with sed
# https://askubuntu.com/questions/76808/how-do-i-use-variables-in-a-sed-command
# sed -i "s/^PREFIX=\/usr\/local/PREFIX=\/tmp\/dest\/tree\/usr\/local/g"  /${SRCDIR}/${PACKAGE}-${VERSION}/Makefile


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


# Below lines just for savekeeping in case make is used in a build script, and needs troubleshooting.
# https://stackoverflow.com/questions/5820303/how-do-i-force-make-gcc-to-show-me-the-commands
# make -n install
# make SHELL='sh -x' DESTDIR=$DESTDIR install 

######################################################
# Base extension                                     #
######################################################
sudo rm -r ${TMPDIR}* 2>/dev/null

mkdir -p  ${TMPDIR}/usr/local/lib/
mkdir -p  ${TMPDIR}/usr/local/share/doc/${PACKAGE}


cp ${SRCDIR}/${PACKAGE}-${VERSION}/LICENSE ${TMPDIR}/usr/local/share/doc/${PACKAGE}/
mv ${DESTDIR}/usr/local/lib/*.so* ${TMPDIR}/usr/local/lib
mv ${DESTDIR}/usr/local/lib/python* ${TMPDIR}/usr/local/lib

###################################################
# 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:       Talloc is a hierarchical, reference counted memory pool system with destructors.
                It is built atop the C standard library and it defines a set of utility functions that
                altogether simplifies allocation and deallocation of data, especially for complex
                structures that contain many dynamically allocated elements such as strings and
                arrays.

Change-log:     ${TODAY} First version, ${VERSION} (${ME})
Current:        ${TODAY} First version, ${VERSION} (${ME})
EOF

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


# Move files to doc extension
mkdir -p $TMPDIR-doc/usr/local/share/${PACKAGE}

# mv ${DESTDIR}/usr/local/man ${TMPDIR}-doc/usr/local/share/${PACKAGE}/

###################################################
# Create info file                                #
###################################################
cat <<EOF> ${PACKAGE}-doc.tcz.info
Title:          ${PACKAGE}-doc.tcz
Description:    ${DOCDESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   ${ME}
Tags:           ${DOCTAGS}
Comments:       Talloc is a hierarchical, reference counted memory pool system with destructors.
                It is built atop the C standard library and it defines a set of utility functions that
                altogether simplifies allocation and deallocation of data, especially for complex
                structures that contain many dynamically allocated elements such as strings and
                arrays.

Change-log:     ${TODAY} first version, ${VERSION} (${ME})
Current:        ${TODAY} first version, ${VERSION} (${ME})
EOF

# Move files to dev extension
mkdir -p $TMPDIR-dev/usr/local/include
mkdir -p $TMPDIR-dev/usr/local/lib

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

###################################################
# Create info file                                #
###################################################
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:   ${ME}
Tags:           ${DEVTAGS}
Comments:       Talloc is a hierarchical, reference counted memory pool system with destructors.
                It is built atop the C standard library and it defines a set of utility functions that
                altogether simplifies allocation and deallocation of data, especially for complex
                structures that contain many dynamically allocated elements such as strings and
                arrays.

Change-log:     ${TODAY} first version, ${VERSION} (${ME})
Current:        ${TODAY} first version, ${VERSION} (${ME})
EOF

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


###################################################
# Create base extension in temp dir               #
###################################################
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
mksquashfs $TMPDIR ${PACKAGE}.tcz

cd $TMPDIR
sudo sh -c "find usr -not -type d | sed 's/\(.*$\)/\/\1/g' > ${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 doc extension in temp dir                #
###################################################

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 ..
# mksquashfs $TMPDIR-doc ${PACKAGE}-doc.tcz
mksquashfs $TMPDIR-doc ${PACKAGE}-doc.tcz

cd $TMPDIR-doc
sudo sh -c "find usr -not -type d | sed 's/\(.*$\)/\/\1/g' > ${PACKAGE}-doc.tcz.list"
sudo mv ../${PACKAGE}-doc.tcz* .
#sudo mv /tmp/${PACKAGE}/${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

###################################################
# 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
mksquashfs $TMPDIR-dev ${PACKAGE}-dev.tcz

cd $TMPDIR-dev
sudo sh -c "find usr -not -type d | sed 's/\(.*$\)/\/\1/g' > ${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

