#!/bin/sh
# Copyright 2006, 2007, 2008  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# To build the helper module for a kernel other than the running kernel,
# run this script like this:
#   KERNEL=2.6.21.1 ./svgalib_helper.SlackBuild

CWD=$(pwd)
PKGNAM=svgalib_helper
TMP=${TMP:-/tmp}
PKG=$TMP/package-$PKGNAM
SRCNAM=svgalib
KERNEL=${KERNEL:-$(uname -r)}


VERSION=${VERSION:-1.9.25}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1_slack12.2}


if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

rm -rf $PKG
cd $TMP
rm -rf ${SRCNAM}-${VERSION}
tar xzvf $CWD/${SRCNAM}-$VERSION.tar.gz
cd ${SRCNAM}-$VERSION

# Make sure ownerships and permissions are sane:
chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -name "*.h" -exec chmod 644 {} \;

zcat $CWD/svgalib.prefix.diff.gz | patch -p1 --verbose || exit 1
zcat $CWD/svgalib_helper-linux_2.6.23.diff.gz | patch -p1 --verbose || exit 1
zcat $CWD/svgalib_helper.2.6.27.diff.gz | patch -p1 --verbose || exit 1

cd kernel/svgalib_helper
KERNELRELEASE=$KERNEL make || exit 1

mkdir -p $PKG/lib/modules/$KERNEL/kernel/misc
cat svgalib_helper.ko > $PKG/lib/modules/$KERNEL/kernel/misc/svgalib_helper.ko

# The docs for this are the same as the svgalib package
mkdir -p $PKG/usr/doc
( cd $PKG/usr/doc
  ln -sf $SRCNAM-$VERSION ${PKGNAM}-${VERSION}_$(echo ${KERNEL} | tr - _)
)

mkdir -p $PKG/install
cat $CWD/slack-desc.helper > $PKG/install/slack-desc
zcat $CWD/doinst.sh.helper.gz > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -c n -l y $TMP/${PKGNAM}-${VERSION}_$(echo ${KERNEL} | tr - _)-$ARCH-$BUILD.tgz