#!/bin/bash
#
# Script : slackwarearm-current/source/x/x11/indibuild
# Purpose: Build modular X.org packages, handling build numbers
#          and X.org section types (app/driver/util) automatically.
# by Stuart Winter <mozes@slackware.com>
#
# Copyright 2008-2011  Stuart Winter, Surrey, England.
# 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.
#
# Building individual packages
# -----------------------------
# This script aids with building, unattended, the individual components.
# Before running this script, be sure to wipe the existing packages from the
# 'slackware/x' directory.  This helps determine if any of the builds failed/
# if there packages missing.
# It's a bit clunky, but we love it :)
#
# Build numbers
# -------------
# You can set specific build numbers by creating the
# package name inside build-nums dir:
# echo 2 > build-nums/xinit
#
# The way build numbers are handled in Slackware ARM is that
# when building individual components (which is always the way 
# unless you're starting a port from scratch), you:
#
# 1.  Check if there's a file with the package base name inside the 'build-nums' directory
#     and create one if there isn't.
#
# 2.  Adjust the build number -- either increasing it if it's a package rebuild,
#     or resetting it to '1' if it's a version update.
# 
# The reason for this is that Slackware build numbers often differ from Slackware ARM
# since it takes a while to catch up, and there may have already been several
# rebuilds in Slackware before we even start looking at rebuilding the package.
#
# When dealing with an Xorg update in Slackware, the easiest way to do it (without
# running the entire X11.SlackBuild), is to:
# - Filter only the x updates from the ChangeLog entries
# - Grep "Removed" note down which exist for ARM, and paste those .tgz file names into
#   Slackware ARM's changelog.
# - Grep "Rebuilt" and add those to this script
# - Grep "Added" and add those to the "Upgraded" (since the script handles them the same)
# - Grep "Upgraded", as below:
# cat /tmp/f | grep Upgraded | sed 's?+x/??g' | cut -d: -f1 | rev | cut -d- -f4- | rev | while read updpkg ; do echo "$updpkg \\" ; done
# The first time, run this script WITHOUT updating build numbers - just to get everything 
# upgraded and on the live filesystem.
# Run again, without updating version numbers to ensure everything links against each other
# For the Rebuilt packages, build those by supplying the "R" option to the "build" function
# For Upgraded packages, build those with the U option.
#######################################################################################
#
# pkg-config --libs --cflags glib-2.0
# 
# 

source /usr/share/slackdev/buildkit.sh
ORIGCWD=$PWD

mkdir -vpm755 $ORIGCWD/build-logs

##############################################################
#
# Syntax:
#    build <pkgname> [U|R]
# U=Package version upgrade, so the .tgz will be at build '1'
#   do this for NEW packages also
# R=Package rebuild, so the .tgz will be build+1
#
# If you exclude R or U then the build number is unmodified.
#
function build () {

 unset MODBUILDNOBUMP ## allow build number to be incremented.
 INDIPKG=$1

 echo "*********************************"
 echo "***** Working on modular package: $INDIPKG *****"
 echo "*********************************"

 # If it's an upgrade then we delete any build number, otherwise if it's a rebuild,
 # we'd want to update the build number but I'll leave that until later :)
 if [ "$2" = "U" ]; then
    echo "This is tagged as a package UPGRADE/NEW PACKAGE, set build: 1."
    echo "MODBUILD=1" > $ORIGCWD/build-nums/$INDIPKG
  elif [ "$2" = "R" ]; then
    # It's a rebuild & increment the build number:
    if [ -s $ORIGCWD/build-nums/$INDIPKG ]; then
       source $ORIGCWD/build-nums/$INDIPKG
       let MODBUILD++
       echo "Tagged as a rebuild - will be build: $MODBUILD"
       # This is handled in x11.SlackBuild now:
       # but ensure that MODBUILDNOBUMP is unset in arm/build:
       # (it should always be unless we're bootstrapping a new X)
#       echo "_PKGVER=${_PKGVER}" > $ORIGCWD/build-nums/$INDIPKG
#       echo "MODBUILD=$MODBUILD" >> $ORIGCWD/build-nums/$INDIPKG
      else
       # x11.SlackBuild assumes a build number of what is set in "arm/build" if there isn't a
       # build stamp file for a particular modular package.
       # but if we've not already got a build number, but got a package then we need
       # to assume that this is going to be the 2nd build.
       # This is because we only started creating build stamp files much later in the 
       # evolution of X11 packaging for Slackware ARM.
       # There's no package version included at this point but that does not
       # matter - it'll be dealt with in x11.SlackBuild.
       echo "MODBUILD=2" > $ORIGCWD/build-nums/$INDIPKG
       echo "Tagged as a rebuild but has no build stamp file - setting as build: 2"
    fi
  # If we don't specify either U or R, then we leave the build number alone
  # since we may be just making some test packages prior to a public tree push.
  # Build numbers are never updated when the build of a particular package version
  # hasn't ever been pushed publically.
  elif [ -z "$2" ]; then
     export MODBUILDNOBUMP=Yes
     echo "*** Package rebuild without build increment ***"
  fi

 # Determine the section of X where we'll find the source:
 ~/armedslack/dbuild $( basename $( find $SLACKSOURCE/x/x11/src -name "*$INDIPKG*" -printf "%h\n" )) $INDIPKG 2>&1 | tee $ORIGCWD/build-logs/$INDIPKG.log

}

#########################################################################################
# Slackware ARM packages
# These packages aren't included in Slackware either because they aren't needed
# or are found not to work.
#
# They're needed on the ARM platform though.
#########################################################################################
#
# This is cheating - a dirty work around to avoid hacking x11.SlackBuild some more.
# fbdev is removed from Slackware since it causes failures when a machine has no xorg.conf, but
# we need it for ARMedslack - and it works with or without an xorg.conf.
#
###################################
# Package: driver/xf86-video-fbdev
###################################
#
# Remove it from the package black list, allowing it to build:
sed -i 's?xf86-video-fbdev??g' $SLACKSOURCE/x/x11/package-blacklist || exit 1
# Install our local copy into the Slackware tree because this is easier than modifying
# the x11.SlackBuild to find the source in our local tree.  It's a little hacky I agree but
# for one package, I adjust my standards ;-)
#cp -favv $ORIGCWD/src/driver/xf86-video-fbdev*  $SLACKSOURCE/x/x11/src/driver/ || exit 1
#build xf86-video-fbdev R
#rm -fv $SLACKSOURCE/x/x11/src/driver/xf86-video-fbdev*
#exit

#
###
## this stuff is deprecated in X.org 7.5 but is left here as reference for
## how to add any ARM specific stuff.
##
# Both liblbxutil & lbxproxy are needed for app/proxymngr to compile.
#
##########################
# Package: lib/liblbxutil
##########################
#cp -favv $ORIGCWD/src/lib/liblbxutil*  $SLACKSOURCE/x/x11/src/lib/
#build liblbxutil  U
#rm -fv $SLACKSOURCE/x/x11/src/lib/liblbxutil*
#
#########################
# Package: app/lbxproxy
#########################
#cp -favv $ORIGCWD/src/app/lbxproxy*  $SLACKSOURCE/x/x11/src/app/
#build lbxproxy U
#rm -fv $SLACKSOURCE/x/x11/src/app/lbxproxy*
##
# You'll probably want to build this next since it relies on both of the above "*lbx*"
# packages being installed.
#build proxymngr


########################################################################################
# The following packages are both in Slackware x86 & ARM
########################################################################################
#build xf86-video-ati
#build xf86-input-penmount

build xf86-video-intel U

#build pixman U

#build compiz U
#build libX11 R
#build xkeyboard-config U
exit

build libX11 U
build util-macros U
build xorg-sgml-doctools U
build xrdb

exit

# We have the same version _number_ as Slackware but it's from GIT and
# the content in the archive is different:
#rm -fv $SLACKSOURCE/x/x11/src/driver/xf86-video-xgi*
#cp -favv $ORIGCWD/src/driver/xf86-video-xgi*  $SLACKSOURCE/x/x11/src/driver/ || exit 1
#build xf86-video-xgi R
#exit

# Upgrade a bunch of stuff:
# When upgrading "xorg-server" packages, you only need to rebuild the
# base "xorg-server" - the others "xnest" etc will be built during that run.
# ** Remember to set R(ebuild) or U(pgrade) here!

# Don't build with DRI:
# No point. I thought it'd help with the KDE crash but it doesn't.
#sed -i 's?--with-dri-driver-path=.*?\\?g' $SLACKSOURCE/x/x11/configure/xorg-server
# 
for i in \
 libXi \
 xproto \
 ; do build $i U
#; do build $i R


#  build $i  # just now build it - will rebuild & update build # on the last pass.

done

exit

#build xdm R

#build imake U

#build xorg-server U

# build proto U

# EOF
