#!/bin/sh
CWD=`pwd`
PKG=/tmp/package-pcmcia-cs

VERSION=${VERSION:-3.2.7}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-3}

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mcpu=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

rm -rf $PKG
mkdir -p $PKG
cd /tmp
rm -rf pcmcia-cs-$VERSION
tar xzvf $CWD/pcmcia-cs-$VERSION.tar.gz
cd pcmcia-cs-$VERSION
chown -R root.root .
zcat $CWD/pcmcia-cs.dhcpcd.diff.gz | patch -p1 --verbose
mkdir -p $PKG/usr/doc/pcmcia-cs-$VERSION
cp -a \
  BUGS COPYING LICENSE MAINTAINERS doc/PCMCIA-HOWTO \
  doc/PCMCIA-PROG README* SUPPORTED.CARDS \
  $PKG/usr/doc/pcmcia-cs-$VERSION
chmod 644 $PKG/usr/doc/pcmcia-cs-$VERSION/*
make clean
./Configure --noapm --nopnp --trust --kernel=/usr/src/linux --noprompt --current --bsd
make all
make install PREFIX=$PKG
# I see no good reason to make these setuid root.
# If you want to be crazy (or have no "problem" users :-), change 'em yourself.
chmod 755 $PKG/sbin/cardctl $PKG/usr/X11R6/bin/xcardinfo
# This version of rc.pcmcia has been modified to probe for the proper PCIC.
mkdir -p $PKG/etc/rc.d
zcat $CWD/rc.pcmcia.gz > $PKG/etc/rc.d/rc.pcmcia
mv $PKG/etc/rc.d/rc.pcmcia $PKG/etc/rc.d/rc.pcmcia.new
# Patch the config.opts for the correct WebGear wireless defaults:
( cd $PKG ; zcat $CWD/config.opts.diff.gz | patch -p1 --backup --suffix=.orig )
# Remove original version (the changes are well commented within the file):
rm -f $PKG/etc/pcmcia/config.opts.orig
# Move config files to *.new:
( cd $PKG/etc/pcmcia
  for file in config.opts ftl.opts ide.opts ieee1394.opts memory.opts network.opts parport.opts scsi.opts serial.opts wireless.opts ; do
    mv $file ${file}.new
  done
)
# This seems like a dumb idea.
#mkdir -p /usr/include/pcmcia
#cp -a include/pcmcia/* /usr/include/pcmcia
#chmod 644 /usr/include/pcmcia/*
#chown root.root /usr/include/pcmcia/*
chown -R root.bin $PKG/sbin $PKG/usr/bin $PKG/usr/X11R6/bin
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)
gzip -9 $PKG/usr/man/man?/*.?
mkdir -p $PKG/install
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
makepkg -l y -c n ../pcmcia-cs-$VERSION-$ARCH-$BUILD.tgz

