#!/bin/bash

####################################################################
# Script : qemu.SlackBuild
# Purpose: Build a qemu package for Slackware
# Author : Stuart Winter <mozes@slackware.com>
# Date   : 14-Oct-2006
####################################################################

PKGNAM=qemu
VERSION=2.5.0
BUILD=${BUILD:-1}

# Determine number of parallel jobs by the number of your cores+1
NUMJOBS="-j$(awk '/^processor[[:space:]]*:/{n=$3} END{print n+1}' /proc/cpuinfo)"

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

function auto_apply_patch () {
 patchfile=$1

 echo
 echo "***********************************************************"
 echo "** Working on Patch: $patchfile"
 echo "***********************************************************"
 echo

 # Decompress the patch if it's compressed with a known method:
 FTYPE=$( file $patchfile )
 case "$FTYPE" in
    *xz*compressed*)
        xz -dc $patchfile > $TMPBUILD/$(basename $patchfile).unpacked
        patchfile=$TMPBUILD/$(basename $patchfile).unpacked ;;
    *bzip2*compressed*)
        bzcat -f $patchfile > $TMPBUILD/$(basename $patchfile).unpacked
        patchfile=$TMPBUILD/$(basename $patchfile).unpacked ;;
    *gzip*compressed*)
        zcat -f $patchfile > $TMPBUILD/$(basename $patchfile).unpacked
        patchfile=$TMPBUILD/$(basename $patchfile).unpacked ;;
 esac

 # By now the patch is decompressed or wasn't compressed originally.
 #
 # Most patches should not require more levels than this:
 success=0
 for (( pl=0 ; pl<=5 ; pl++ )) ; do
   echo "Patch : $patchfile , trying patch level $pl"
     patch -N --fuzz=20 -t --dry-run -lp$pl < $patchfile > /dev/null 2>&1 && success=1 && break
   done
 if [ $success = 1 ]; then
    echo "Patch: $patchfile will apply at level $pl"
    patch -N --fuzz=20 --verbose -lp$pl < $patchfile
    return 0
  else
    echo "Patch: $patchfile failed to apply at levels 0-5"
    return 1
 fi
}

CWD=$PWD
TMP=/tmp/build-$PKGNAM
PKG=/tmp/package-$PKGNAM
rm -rf $TMP $PKG
mkdir -pm755 $TMP $PKG

# Determine which patch level to use & apply the patch:

# Extract source:
cd $TMP
tar xvvf $CWD/sources/$PKGNAM*.tar.*
cd $PKGNAM* || exit 1
chown -R root:root .
chmod -Rf a-s,a+rX,u+w,g-w,o-w .

# Apply Debian patches:
#grep -Ev '^#' debian/patches/series | while read patch ; do
#  auto_apply_patch debian/patches/$patch 
#done
#ls -1 $CWD/sources/[0-9]*.patch.xz | while read patch ; do
#  auto_apply_patch $patch
#done

# Configure:
# If you only want to emulate ARM, then leave this commented;
# but if you want to build emulators for all supported architectures,
# comment out the following line:
#TARGETS="--target-list=i386-softmmu,arm-softmmu,i386-linux-user,arm-linux-user,armeb-linux-user"
#
ADDITIONS="--audio-drv-list=oss,alsa,sdl" ## ,pa"
CFLAGS="-O2" \
./configure $ADDITIONS $TARGETS \
   --prefix=/usr \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --docdir=/usr/doc/qemu-$VERSION \
   --sysconfdir=/etc \
   --enable-system \
   --enable-vnc \
   --enable-sdl \
   --enable-kvm || exit 1
#   --enable-mixemu || exit 1
#   --disable-blobs \

# Flattened Device Tree:
# This requires the 'device-tree-compiler' package
# to be installed.
#   --enable-fdt \

# Build:
make $NUMJOBS || exit 1

# Install into package:
make install DESTDIR=$PKG || exit 1

# Move some stuff around:
mv $PKG/usr/share/{doc,man} $PKG/usr

# Install more docs:
cp -fa COPYING* Changelog LICENSE README* TODO VERSION \
       $PKG/usr/doc/$PKGNAM
mv -f $PKG/usr/doc/$PKGNAM $PKG/usr/doc/$PKGNAM-$VERSION

# Compress man pages:
( cd $PKG/usr/man
  ( find ./${man_dir} -type f -print0 | xargs -0 gzip -9   ) >/dev/null 2>&1
  ( find ./${man_dir} -type f -print0 | xargs -0 chmod 644 ) >/dev/null 2>&1 
  for i in $( find . -type d -maxdepth 1 -printf "%P\n" | grep -v "^$" ); do
    cd ${i} && ( find . -type l -printf "rm -f %P ; ln -s %l.gz %p.gz\n" ) | /bin/bash && cd ..
  done )

# Install package description: 
mkdir -pm755 $PKG/install
install -vpm644 $CWD/slack-desc $PKG/install

# Ensure package permissions:
cd $PKG
find . -type d -print0 | xargs -0 chmod 755
chown -R root:root .

# Build package:
VERSION="$( echo $VERSION | sed 's?-?_?g' )"
makepkg -l y -c n /tmp/$PKGNAM-$VERSION-$ARCH-$BUILD.txz

#EOF
