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

######################################################
# Prepare extension creation                         #
######################################################
#download and install dependencies
tce-load -wi bash
#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="go"
# Not the latest version 1.22.2 because of following issue:
# https://groups.google.com/g/golang-nuts/c/sJuUZouTZCo
# Version 1.22.2 should still compile fine, only the test is failing.
VERSION="1.22.1"
DESCRIPTION="The Go Programming Language"
DOCDESCRIPTION="Documentation part of go"
DEVDESCRIPTION="Development files part of go"
AUTHORS="see homepage"
HOMEPAGE="https://go.dev/"
LICENSE="https://github.com/golang/go?tab=BSD-3-Clause-1-ov-file"
ME="rhermsen"
TAGS="go programming"
DOCTAGS="man pages go programming"
DEVTAGS="development go programming"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}
BOOTSTRAPDIR=/tmp/BS/${PACKAGE}
ARCH=`uname -m`
# With 32-bit the tmp dir isn't big enough. Compile from disk.
if [ $ARCH == "i686" ]
then
SRCDIR=/mnt/vda1/tce/${PACKAGE}
elif [ $ARCH == "x86_64" ]
then
SRCDIR=/tmp/${PACKAGE}
fi

# Needed for internal linking
if [ $ARCH == "x86_64" ]
then
sudo ln -s /lib /lib64
fi

# Workdir
sudo rm -r ${SRCDIR} 2>/dev/null
mkdir ${SRCDIR}

# Bootstrapdir
sudo rm -r ${BOOTSTRAPDIR} 2>/dev/null
mkdir -p ${BOOTSTRAPDIR}

# Bootstrap build
cd ${BOOTSTRAPDIR}
if [ $ARCH == "i686" ]
then
wget https://go.dev/dl/go${VERSION}.linux-386.tar.gz
tar xzf go${VERSION}.linux-386.tar.gz
elif [ $ARCH == "x86_64" ]
then
wget https://go.dev/dl/go${VERSION}.linux-amd64.tar.gz
tar xzf go${VERSION}.linux-amd64.tar.gz
fi

# Source
cd ${SRCDIR}
wget https://go.dev/dl/go${VERSION}.src.tar.gz
tar xzf go${VERSION}.src.tar.gz

cd ${SRCDIR}/${PACKAGE}


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

# Compile
cd /${SRCDIR}/${PACKAGE}/src
# sudo GOROOT_BOOTSTRAP=/tmp/BS/go/go ./all.bash
GOROOT_BOOTSTRAP=/tmp/BS/go/go ./all.bash


# 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/${PACKAGE}
mkdir -p  ${TMPDIR}/usr/local/bin
mkdir -p  ${TMPDIR}/usr/local/share/doc/${PACKAGE}

cp ${SRCDIR}/${PACKAGE}/LICENSE ${TMPDIR}/usr/local/share/doc/${PACKAGE}/
mv ${SRCDIR}/${PACKAGE}/bin ${TMPDIR}/usr/local/${PACKAGE}
mv ${SRCDIR}/${PACKAGE}/lib ${TMPDIR}/usr/local/${PACKAGE}
mv ${SRCDIR}/${PACKAGE}/misc ${TMPDIR}/usr/local/${PACKAGE}
mv ${SRCDIR}/${PACKAGE}/pkg ${TMPDIR}/usr/local/${PACKAGE}
mv ${SRCDIR}/${PACKAGE}/src ${TMPDIR}/usr/local/${PACKAGE}
mv ${SRCDIR}/${PACKAGE}/test ${TMPDIR}/usr/local/${PACKAGE}
mv ${SRCDIR}/${PACKAGE}/go.env ${TMPDIR}/usr/local/${PACKAGE}
mv ${SRCDIR}/${PACKAGE}/VERSION ${TMPDIR}/usr/local/${PACKAGE}
mv ${SRCDIR}/${PACKAGE}/codereview.cfg ${TMPDIR}/usr/local/${PACKAGE}

# Add symlinks
cd $TMPDIR/usr/local/bin
ln -s ../go/bin/go
ln -s ../go/bin/gofmt


###################################################
# Create info file                                #
###################################################
cd /tmp/submit/
if [ $ARCH == "i686" ]
then
cat <<EOF> ${PACKAGE}.tcz.info
Title:          ${PACKAGE}.tcz
Description:    ${DESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   bmarkus, (${ME})
Tags:           ${TAGS}
Comments:       Go is an open source programming language that
                makes it easy to build simple, reliable, and efficient software.

                (consider using disk in case /tmp runs out of space)
                (e.g. export TMPDIR=/mnt/<disk>/<tmp_dir>)

Change-log:     2015/01/26 First version, 1.4.1 (bmarkus)
                2015/05/15 Updated to 1.4.2 (bmarkus)
                ${TODAY} updated version, ${VERSION} (${ME})
Current:        ${TODAY} updated version, ${VERSION} (${ME})
EOF
elif [ $ARCH == "x86_64" ]
then
cat <<EOF> ${PACKAGE}.tcz.info
Title:          ${PACKAGE}.tcz
Description:    ${DESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   David Maiorino, (${ME})
Tags:           ${TAGS}
Comments:       Go is an open source programming language that
                makes it easy to build simple, reliable, and efficient software.


Change-log:     2020/09/23 First version, 1.15.2 (David Maiorino)
                ${TODAY} Updated version, ${VERSION} (${ME})
Current:        ${TODAY} Updated version, ${VERSION} (${ME})
EOF
fi

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

mv ${SRCDIR}/${PACKAGE}/doc ${TMPDIR}-doc/usr/local/share/${PACKAGE}/
mv ${SRCDIR}/${PACKAGE}/api ${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:       Go is an open source programming language that
                makes it easy to build simple, reliable, and efficient software.


Change-log:     ${TODAY} first version, ${VERSION} (${ME})
Current:        ${TODAY} first version, ${VERSION} (${ME})
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

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

cd $TMPDIR-doc
sudo sh -c "find usr -not -type d > ${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

