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

VERSION=1.9
ARCH=sparc
BUILD=1

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

# 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

# build elftoaout
cd $TMP
tar xvzf $CWD/elftoaout-2.3.tar.gz
cd elftoaout-2.3
make 
cat elftoaout > $PKG/usr/bin/elftoaout

# build piggyback 1.4 /piggyback64 1.2
cd $TMP
tar xvjf $CWD/piggyback.tar.bz2
cd piggyback
gcc -O2 -o piggyback piggyback.c 
gcc -O2 -o piggyback64 piggyback64.c 
cat piggyback   > $PKG/usr/bin/piggyback
cat piggyback64 > $PKG/usr/bin/piggyback64
cp $CWD/piggyback.1 $PKG/usr/share/man/man1/piggyback.1
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 ethtool
cd $TMP
tar xvzf $CWD/ethtool.tar.gz
cd ethtool
make 
cat ethtool > $PKG/usr/sbin/ethtool
gzip -9c ethtool.8 > $PKG/usr/share/man/man8/ethtool.8.gz

# build prtconf
cd $TMP
tar xvzf $CWD/prtconf.tar.gz
cd prtconf
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
cd $TMP
tar xvzf $CWD/sparc32-1.1.tar.gz
cd sparc32-1.1
zcat $CWD/sparc32-1.1.diff.gz | patch -p1
zcat $CWD/sparc32-1.1-include.patch.gz | patch -p1
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
cd $TMP
tar xvzf $CWD/audioctl-1.3.tar.gz
cd audioctl
make 
cat audioctl > $PKG/usr/bin/audioctl
gzip -9c audioctl.1 > $PKG/usr/share/man/man1/audioctl.1.gz

# build openprom
cd $TMP
cc -O2 -o openprom $CWD/openprom.c 
cat openprom > $PKG/usr/bin/openprom

# build uname-sunos
cd $TMP
cc -O2 -o uname-sunos $CWD/uname-sunos.c 
cat uname-sunos > $PKG/usr/bin/uname-sunos

# 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/sparc-utils-$VERSION-$ARCH-$BUILD.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/elftoaout-2.3
  rm -rf $TMP/piggyback
  rm -rf $TMP/ethtool
  rm -rf $TMP/prtconf
  rm -rf $TMP/sparc32-1.1
  rm -rf $TMP/audioctl
  rm -rf $PKG
fi
