#!/bin/bash
# corn.SlackBuid: build script for the corn library
#
# Copyright 2013 by Jerry Nettrouer II
#
# This file is part of the corn package.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose and without fee is hereby granted, provided
# that the above copyright notice and this permission notice appear
# in all copies, and that the name of Jerry Nettrouer II not be used in
# advertising or publicity pertaining to distribution of the document
# or software without specific, written prior permission.
#
# Jerry Nettrouer II makes no representations about the suitability of the
# software described herein for any purpose.  It is provided "as is"
# without express or implied warranty.
#
# The author may be contacted via http://www.inpito.org/
# Jerry Nettrouer <j2[at]inpito.org>
#
# This package is written for my Slackware Systems!

PKGNAM="corn"
VERSION=${VERSION:-0.1.3}
BUILD=${BUILD:-inpito}

CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-${PKGNAM}
OUTPUT=${TMP}

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) # Determine Arm Architecture
        if [ "$(uname -m)" = "armv7l" ]; then
          ARCH=armv7l
        else
          ARCH=arm
        fi
        ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

# Set enviromental variables
if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
elif [ "$ARCH" = "arm" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "armv7l" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
fi

set -e

# Open sourcecode
cd $TMP
rm -rf $PKG
rm -rf ${PKGNAM}-${VERSION}
tar xvf $CWD/$PKGNAM-$VERSION.tar.?z* || exit 1
cd "${TMP}/${PKGNAM}-${VERSION}"

SOURCE_DIR=$( pwd )

# If you're compiling as root 
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

# Test for a 64-bit architecture
if [ "$ARCH" = "x86_64" ] || [ "$ARCH" = "aarch64" ]; then

  # Change directory into the source directory
  cd src

  # Change the 32-bit values into 64-bit values
  sed -i "s:#define ISIZE 5:#define ISIZE 9;:" corn.c
  sed -i "s:#define LNUMB 9:#define LNUMB 18:" corn.c
  sed -i "s:#define LIMIT 536870912:#define LIMIT 2305843009213693952:" corn.c

  # Return to the package root directory
  cd ..
fi

# Set the Compiler Flags
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --enable-shared=yes \
  --enable-static=no \
  --disable-debug \
  --build=$ARCH-slackware-linux

make
echo
echo "Package ${PKGNAM}-${VERSION} Made"
echo
make install-strip DESTDIR=$PKG

# Load the slack-desc
mkdir -p $PKG/install
cp -a $CWD/slack-desc $PKG/install/

mkdir -p $PKG/usr/share/doc/$PKGNAM-$VERSION
cp -av $CWD/README $PKG/usr/share/doc/$PKGNAM-$VERSION/

# Copy the source files to doc directory
cp -av $SOURCE_DIR/src/*.h $PKG/usr/share/doc/$PKGNAM-$VERSION
cp -av $SOURCE_DIR/src/*.c $PKG/usr/share/doc/$PKGNAM-$VERSION

# Time to build the package
cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PKGNAM-$VERSION-$ARCH-$BUILD.${PKGTYPE:-tgz}

