#!/bin/sh
#
######################################################
# Build script for Core 10                           #
#                                                    #
# 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="libyaml"
VERSION="0.2.2"
DESCRIPTION="C library for parsing and emitting YAML"
DEVDESCRIPTION="Development files part of C library for parsing and emitting YAML"
AUTHORS="See homepage"
HOMEPAGE="https://pyyaml.org/wiki/LibYAML"
LICENSE="https://github.com/yaml/libyaml/blob/master/LICENSE"
ME="rhermsen"
TAGS="yaml ansible"
DEVTAGS="development yaml ansible"
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 http://pyyaml.org/download/$PACKAGE/yaml-$VERSION.tar.gz
tar -xzf yaml-$VERSION.tar.gz
cd yaml-${VERSION}

######################################################
# Compile extension                                  #
######################################################
./configure \
--prefix=/usr/local

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}/yaml-${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:       LibYAML is a YAML parser and emitter library
                LibYAML covers presenting and parsing processes.
                Thus LibYAML defines the following two processors:
                - Parser, which takes an input stream of bytes and
                produces a sequence of parsing events.
                - Emitter, which takes a sequence of events and
                produces a stream of bytes.
                
Change-log:     ${TODAY} compile later version, ${VERSION} (${ME})
Current:        ${TODAY} compile later 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:   ${ME}
Tags:           ${DEVTAGS}
Comments:       LibYAML is a YAML parser and emitter library
                LibYAML covers presenting and parsing processes.
                Thus LibYAML defines the following two processors:
                - Parser, which takes an input stream of bytes and
                produces a sequence of parsing events.
                - Emitter, which takes a sequence of events and
                produces a stream of bytes.
                
Change-log:     ${TODAY} compile later version, ${VERSION} (${ME})
Current:        ${TODAY} compile later version, ${VERSION} (${ME})
EOF

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

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

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

