#!/bin/bash

# pcmcia-cs.SlackBuild
# by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 08-Jun-2004

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     ARCH_CFLAGS="-march=armv3 -mtune=xscale" ;;
   powerpc) ARCH_CFLAGS="" ;;
   sparc)   ARCH_CFLAGS="" ;;
   *)       ARCH_CFLAGS="" ;;
esac

# Extract source:
tar jxvvf $CWD/$PACKAGE-$VERSION.tar.bz2
cd $PACKAGE-$VERSION
slackhousekeeping

# Apply patches:
zcat $CWD/pcmcia-cs.dhcpcd.diff.gz | patch -p1 --verbose || failpatch

# Install docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a BUGS COPYING LICENSE MAINTAINERS doc/PCMCIA-HOWTO \
      doc/PCMCIA-PROG README* SUPPORTED.CARDS \
      $PKG/usr/doc/$PACKAGE-$VERSION

# Configure:
make clean
./Configure \
   --noapm \
   --nopnp \
   --trust \
   --kernel=/usr/src/linux \
   --noprompt \
   --current \
   --bsd || failconfig

# Build:
make all || failmake
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

# 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
chmod 644 $PKG/etc/pcmcia/*.opts

# Move config files to *.new:
( cd $PKG/etc/pcmcia
  for file in *.opts ; do
    mv $file ${file}.new
  done )

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
