#!/bin/bash

# slackkit.SlackBuild
# Build a package of the Slackware ARM's package build kit.
# Stuart Winter <mozes@slackware.com>
# 30-May-2004
#
# In ARMedslack this package belongs in the 'extra' directory
# for the time being, although it will be moved into another more
# appropriate directory later on.

# Package metadata:
PACKAGE=slackkit
VERSION=1.49
BUILD=1

# Automatically determine the architecture we're building on:
MARCH=$( uname -m )
if [ -z "$ARCH" ]; then
  case "$MARCH" in
    i?86)    export ARCH=i586
             export LIBDIRSUFFIX="" ;;
    arm*)    export ARCH=arm
             export LIBDIRSUFFIX="" ;;
    aarch64) export ARCH=aarch64
             export LIBDIRSUFFIX=64 ;;
    x86_64)  export ARCH=x86_64
             export LIBDIRSUFFIX=64 ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
    *)       export ARCH=$MARCH
             export LIBDIRSUFFIX="" ;;
  esac
fi

# Paths
CWD=$PWD
TMP=/tmp/build-slackkit
PKG=/tmp/package-slackkit
PKGSTORE=/tmp
rm -rf $TMP $PKG
mkdir -p $TMP $PKG

# Build package framework:
cd $PKG
mkdir -p usr/share/slackdev usr/lib${LIBDIRSUFFIX}

# Build fakeuname.  This shared object reports the architecture type
# as armv3l so that RPM (amongst others) will let us build on more
# recent ARM architectures.
( cd $TMP
#  gcc -mabi=aapcs-linux -mfloat-abi=soft -meabi=4 -O3 -o fakeuname.o -c -fPIC -Wall $CWD/fakeuname.c
#  gcc -mabi=aapcs-linux -mfloat-abi=soft -meabi=4 -O3 -o libfakeuname.so -shared -W1,export-dynamic fakeuname.o -ldl
  gcc -O3 -o fakeuname.o -c -fPIC -Wall $CWD/sources/fakeuname.c
  gcc -O3 -o libfakeuname.so -shared -Wl,--export-dynamic fakeuname.o -ldl
  strip --strip-unneeded libfakeuname.so
  install -vpm755 libfakeuname.so $PKG/usr/lib${LIBDIRSUFFIX} )

# Build ln wrapper:
gcc -O3 $CWD/sources/lndashf.c -o $PKG/usr/share/slackdev/ln-wrapper
strip --strip-unneeded $PKG/usr/share/slackdev/ln-wrapper

# Install the bits:
install -m755 $CWD/sources/buildkit.sh usr/share/slackdev
install -m755 $CWD/sources/v usr/share/slackdev
install -m755 $CWD/sources/{slackwipepkg,slackmakepkg} usr/share/slackdev
install -m644 $CWD/sources/slackdev.config usr/share/slackdev/slackdev.config.new

# Install the build script templates:
install -vpm755 $CWD/sources/{source.download,differ,trackbuild.d,d.SlackBuild} usr/share/slackdev/
cp -fav $CWD/sources/arm usr/share/slackdev/

# Switch the default architecture depending on what machine type this
# build script is being run:
if [ "$ARCH" = "aarch64" ]; then
   # Comment ARM:
   sed -i 's?^export ARCH=arm?#&?g' usr/share/slackdev/slackdev.config.new
   # Set aarch64:
   sed -i 's/^#\(export ARCH=aarch64*\)/\1/' usr/share/slackdev/slackdev.config.new
fi

# Install docs:
# when I write some.

# Package description & doinst script:
mkdir install
install -m644 $CWD/slack-desc.slackkit install/slack-desc
install -m644 $CWD/doinst.slackkit install/doinst.sh

# Slackware policy:
cd $PKG
chmod 755 .
chown -R root.root .

# Make the package
makepkg -l y -c n $PKGSTORE/$PACKAGE-$VERSION-$ARCH-$BUILD.txz
