#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
PKG=$TMP/package-gutenprint
rm -rf $PKG
mkdir -p $PKG

VERSION=5.0.1
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}

cd $TMP
rm -rf gutenprint-${VERSION}
tar xjvf $CWD/gutenprint-${VERSION}.tar.bz2 || exit 1
cd gutenprint-${VERSION}

chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 777 -exec chmod 755 {} \;

./configure \
  --prefix=/usr \
  --infodir=/usr/info \
  --mandir=/usr/man \
  --with-cups \
  --with-gimp2 \
  --with-gimp2-as-gutenprint \
  --enable-escputil \
  --disable-static \
  --build=$ARCH-slackware-linux
make -j5 || exit 1
make install DESTDIR=$PKG || exit 1

( 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
)

# Compress and if needed symlink the man pages:
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1) ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.?
      )
    done
  )
fi

mkdir -p $PKG/usr/doc/gutenprint-${VERSION}
# If you want 1MB of ChangeLog, it's in the source...
cp -a \
  ABOUT-NLS AUTHORS COPYING INSTALL NEWS README \
  $PKG/usr/doc/gutenprint-${VERSION}
  ( cd $PKG/usr/doc/gutenprint-${VERSION}
    ln -sf /usr/share/gutenprint/doc .
    ln -sf /usr/share/gutenprint/samples .
  )

# Big...  again, it's in the source if you want it.
rm -f $PKG/usr/share/gutenprint/doc/*.pdf

mkdir -p $PKG/install
cat << EOF > $PKG/install/doinst.sh
#!/bin/sh
# Since this is in /testing, an absolute path will do for now.
# If it gets moved to the main tree replacing gimp-print we'll
# probably have to use a chroot to run this.  Also, anyone
# using ROOT= with installpkg may have to chroot to the
# mount to run this command:
if [ -x /usr/sbin/cups-genppd.5.0 ]; then
  echo "Generating PPD files for CUPS:"
  /usr/sbin/cups-genppd.5.0
fi
EOF

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

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