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

######################################################
# Prepare extension creation                         #
######################################################
#download and install dependencies
tce-load -wi protobuf-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="protobuf-c"
VERSION="1.5.0"
DESCRIPTION="Protobuf-c is a C implementation of the Google Protocol Buffers data serialization format"
DOCDESCRIPTION="Documentation part of protobuf-c"
DEVDESCRIPTION="Development files part of protobuf-c"
AUTHORS="Dave Benson (original author)"
HOMEPAGE="https://github.com/protobuf-c/protobuf-c"
LICENSE="https://github.com/protobuf-c/protobuf-c?tab=License-1-ov-file"
ME="rhermsen"
TAGS="protobuf-c libprotobuf-c protoc-c"
DOCTAGS="man pages protobuf-c libprotobuf-c protoc-c"
DEVTAGS="development protobuf-c libprotobuf-c protoc-c"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}

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

# Source
wget https://github.com/${PACKAGE}/${PACKAGE}/releases/download/v${VERSION}/${PACKAGE}-${VERSION}.tar.gz
tar -xzf ${PACKAGE}-${VERSION}.tar.gz
cd ${PACKAGE}-${VERSION}

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

# Export variables needed for compilation
ARCH=`uname -m`
if [ $ARCH == "i686" ]
then
echo
export CFLAGS="-Os -pipe -march=i486 -mtune=i686"
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

# Configure it
# https://www.gnu.org/prep/standards/html_node/Directory-Variables.html
./configure  --prefix=/usr/local \
--exec-prefix=/usr/local \
--sysconfdir=/usr/local/etc/${PACKAGE} \
--localstatedir=/var/run/${PACKAGE} \
--runstatedir=/var/run/${PACKAGE} \
--includedir=/usr/local/include

# Strange step needed to get make working. As if the include-dir didn't work, or is ignored.
cp -r /tmp/tcloop/protobuf-dev/usr/local/include/google cd /tmp/${PACKAGE}/${PACKAGE}-${VERSION}

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

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

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

cp /tmp/${PACKAGE}/${PACKAGE}-${VERSION}/LICENSE ${TMPDIR}/usr/local/share/doc/${PACKAGE}/
mv $DESTDIR/usr/local/bin/* $TMPDIR/usr/local/bin/
mv $DESTDIR/usr/local/lib/*.so* $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:       Protobuf-c is a C implementation of the Google Protocol
                Buffers data serialization format. It includes libprotobuf-c, a
                pure C library that implements protobuf encoding and decoding,
                and protoc-c, a code generator that converts Protocol Buffer
                .proto files to C descriptor code, based on the original protoc.

                protobuf-c formerly included an RPC implementation;
                that code has been split out into the protobuf-c-rpc project.

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

###################################################
# Create .dep file                                #
###################################################
cat <<EOF> ${PACKAGE}.tcz.dep
protobuf.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

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

######################################################
# Dev extension                                      #
######################################################

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/include $TMPDIR-dev/usr/local

###################################################
# 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:   ${ME}
Tags:           ${DEVTAGS}
Comments:       Protobuf-c is a C implementation of the Google Protocol
                Buffers data serialization format. It includes libprotobuf-c, a
                pure C library that implements protobuf encoding and decoding,
                and protoc-c, a code generator that converts Protocol Buffer
                .proto files to C descriptor code, based on the original protoc.

                protobuf-c formerly included an RPC implementation;
                that code has been split out into the protobuf-c-rpc project.

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 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

