#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-sparcutils

VERSION=1.9
ARCH=sparc
BUILD=0
TUPLE=$ARCH-splack-linux

# Package versions
ETHTOOLVERSION=3
DEBSPARCUTILS=1.9.orig




if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi

rm -rf $PKG
mkdir -p $PKG # place for the package to be built

# Make the directory structure
mkdir -p $PKG/usr/bin $PKG/usr/doc $PKG/usr/sbin
mkdir -p $PKG/usr/share/man/man1 $PKG/usr/share/man/man8

# Set up a little build area for us
rm -rf $TMP/sparcutils-build
mkdir -p $TMP/sparcutils-build
TMP=$TMP/sparcutils-build

# build ethtool
echo
echo "+============+"
echo "| ethtool-$ETHTOOLVERSION |"
echo "+============+"
cd $TMP
tar xvzf $CWD/ethtool-$ETHTOOLVERSION.tar.gz
cd ethtool-$ETHTOOLVERSION
./configure \
  --prefix=/usr \
  --host=$TUPLE \
  --build=$TUPLE \
  --target=$TUPLE
make 
cat ethtool > $PKG/usr/sbin/ethtool
gzip -9c ethtool.8 > $PKG/usr/share/man/man8/ethtool.8.gz


# build openprom
# Does anyone have a newer-than-'96 version of this?
echo
echo "+==========+"
echo "| openprom |"
echo "+==========+"
cd $TMP
zcat $CWD/openprom.c.gz > openprom.c
cc -O2 -o openprom openprom.c 
cat openprom > $PKG/usr/bin/openprom


# build uname-sunos
# Alright, who can tell me the original author
# and license for this?
echo
echo "+=============+"
echo "| uname-sunos |"
echo "+=============+"
cd $TMP
zcat $CWD/uname-sunos.c.gz > uname-sunos.c
cc -O2 -o uname-sunos uname-sunos.c 
cat uname-sunos > $PKG/usr/bin/uname-sunos



###############
# The following are from the Debian sparc-utils package
# The 'homepage' and sources are at
#   http://packages.debian.org/stable/source/sparc-utils

# Unpack the Debian sources
echo
echo "(Debian sources)"
cd $TMP
tar zxvf $CWD/sparc-utils_$DEBSPARCUTILS.tar.gz
# add the latest patches (and get more programs!)
zcat $CWD/patches/sparc-utils_1.9-2.3.diff.gz | \
  patch -b -p0 --verbose

# build elftoaout
echo
echo "+===========+"
echo "| elftoaout |"
echo "+===========+"
cd $TMP/sparc-utils-$DEBSPARCUTILS/elftoaout-2.3
make 
cat elftoaout > $PKG/usr/bin/elftoaout

# build piggyback 1.4 /piggyback64 1.2
echo
echo "+================+"
echo "| piggyback{,64} |"
echo "+================+"
cd $TMP/sparc-utils-$DEBSPARCUTILS/src
gcc -O2 -o piggyback piggyback.c 
gcc -O2 -o piggyback64 piggyback64.c 
cat piggyback   > $PKG/usr/bin/piggyback
cat piggyback64 > $PKG/usr/bin/piggyback64
gzip -9c ../debian/piggyback.1 > $PKG/usr/share/man/man1/piggyback.1
gzip -9c ../debian/piggyback64.1 > $PKG/usr/share/man/man1/piggyback64.1
# Pre-piggyback64 manual redirect:
#echo '.\" -*- nroff -*-' > $PKG/usr/share/man/man1/piggyback64.1
#echo '.\' >> $PKG/usr/share/man/man1/piggyback64.1
#echo '.so man1/piggyback.1' >> $PKG/usr/share/man/man1/piggyback64.1
#gzip -9 $PKG/usr/share/man/man1/piggyback*.1

# build prtconf
echo
echo "+=========+"
echo "| prtconf |"
echo "+=========+"
cd $TMP/sparc-utils-$DEBSPARCUTILS/prtconf-1.3
make 
cat eeprom  > $PKG/usr/sbin/eeprom
cat prtconf > $PKG/usr/sbin/prtconf
gzip -9c eeprom.8  > $PKG/usr/share/man/man8/eeprom.8.gz
gzip -9c prtconf.8 > $PKG/usr/share/man/man8/prtconf.8.gz

# build sparc32
echo
echo "+=========+"
echo "| sparc32 |"
echo "+=========+"
cd $TMP/sparc-utils-$DEBSPARCUTILS/sparc32-1.1
# Applying this patch will break the Makefile for this if
# it was previously patched.  Be sure to clean up after each build.
zcat $CWD/patches/sparc32-1.1-include.patch.gz | patch -p0 -b --verbose
make 
cat sparc32 > $PKG/usr/bin/sparc32
ln -sf sparc32 $PKG/usr/bin/sparc64
gzip -9c sparc32.8 > $PKG/usr/share/man/man8/sparc32.8.gz
gzip -9c sparc64.8 > $PKG/usr/share/man/man8/sparc64.8.gz

# build audioctl
echo
echo "+==========+"
echo "| audioctl |"
echo "+==========+"
cd $TMP/sparc-utils-$DEBSPARCUTILS/audioctl-1.3/
make 
cat audioctl > $PKG/usr/bin/audioctl
gzip -9c audioctl.1 > $PKG/usr/share/man/man1/audioctl.1.gz

# End Debian sparc-utils
#####


# Set all the permissions
chown -R root:root $PKG
chmod 755 $PKG/usr/bin/* $PKG/usr/sbin/*
chmod 644 $PKG/usr/share/man/man*/*.gz

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/../sparcutils-$VERSION-$ARCH-$BUILD.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP
  rm -rf $PKG
fi
