#!/bin/bash

# arm/build
# Check package dependencies, set metadata and launch
# package build script.
# by Stuart Winter <mozes@slackware.com>
#
source /usr/share/slackdev/buildkit.sh

# Package metadata:
export PKGNAM=ruby
export VERSION=${VERSION:-1.9.3-p484} # hyphens taken care of in the build script
export PKGARCH=${PKGARCH:-arm}
#export BUILD=${BUILD:-1}
export BUILD=${BUILD:-1_slack13.37}
export PKGSERIES=${PKGSERIES:-d}
export SLACKPACKAGE=$PKGNAM-$VERSION-$PKGARCH-$BUILD.tgz

## ******************************************************************* ##
# For test packages - best to store in another location rather than
# overwriting the working copy in the main tree:
 export PKGSTORE=/tmp/
 mkdir -vpm755 $PKGSTORE/$PKGSERIES
## ******************************************************************* ##

# Reset the ARM file system: restore it to the 'factory settings'
# as it were (this has no effect when building natively):
sboxsysrestore

# Ensure base ARM packages are installed first:
slackbasedeps

# Ensure $PKGNAM isn't already installed:
slackfailpkgdeps $PKGNAM || removepkg $PKGNAM

# You may wonder how I know which version numbers I'm dealing with.
# The answer is that at present present I do not need to worry.
# For different versions of ARMedslack I chroot into that version 
# and build using only the packages built for that version.
#
# Ensure specific build dependencies (mainly libraries):
{ slackcheckpkgdeps db44       || installpkg $PKGSTORE/l/db44-[0-9]*.t?z || exit 99 ; }
{ slackcheckpkgdeps gdbm       || installpkg $PKGSTORE/l/gdbm-[0-9]*.t?z || exit 99 ; }
{ slackcheckpkgdeps libtermcap || installpkg $PKGSTORE/l/libtermcap-[0-9]*.t?z || exit 99 ; }
{ slackcheckpkgdeps ncurses    || installpkg $PKGSTORE/l/ncurses-[0-9]*.t?z || exit 99 ; }
{ slackcheckpkgdeps readline   || installpkg $PKGSTORE/l/readline-[0-9]*.t?z || exit 99 ; }
{ slackcheckpkgdeps openssl    || installpkg $PKGSTORE/n/openssl-[0-9]*.t?z || exit 99 ; }
{ slackcheckpkgdeps tcl        || installpkg $PKGSTORE/tcl/tcl-[0-9]*.t?z || exit 99 ; }
{ slackcheckpkgdeps tk         || installpkg $PKGSTORE/tcl/tk-[0-9]*.t?z || exit 99 ; }
{ slackcheckpkgdeps libX11     || installpkg $PKGSTORE/x/libX11-[0-9]*.t?z || exit 99 ; }
{ slackcheckpkgdeps libyaml    || ( echo "Needs to be installed from /patches" ; exit 99 ); }

# Launch the package build script:
BUILDLOG=$( basename $SLACKPACKAGE .tgz ).build.log
( ./$PKGNAM.SlackBuild ) >& /dev/stdout | tee $BUILDLOG

# Compress the build log:
bzip2 -9fvz $BUILDLOG

