#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
#
# Packager Chionchio Michelino [just added SMB protocol vfs support ;-) ]
# Thanks to Pat and www.slacky.it

CWD=$(pwd)
TMP=${TMP:-/tmp}
if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
PKG=$TMP/package-mc
CHOST=i486
VERSION=4.6.1_20070309
ARCH=${ARCH:-i486}
BUILD=2mch
NAME=mc

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

rm -rf $PKG
mkdir -p $PKG/usr
cd $TMP
rm -rf mc-$VERSION
tar xjvf $CWD/mc-$VERSION.tar.bz2 || exit 1
cd mc-$VERSION

zcat $CWD/mc-4.6.1.php.syntax.diff.gz | patch -p1 --verbose || exit 1

chown -R root:root .
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 {} \;

# This is obsolete:
# ( cd vfs/samba ; zcat $CWD/samba.codepages.diff.gz | patch -p0 )
# To enable samba, I believe these are the options to use:
# --with-samba \
# --with-configdir=/etc/samba \
# --with-codepagedir=/etc/codepages \
#
# Using --with-included-slang seems to be the magic incantation that builds
# a clean, small mc like days of yore
# --with-screen=mcslang \

CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --mandir=/usr/man \
  --localstatedir=/var \
  --enable-charset \
  --with-gpm-mouse \
  --with-ext2undel \
  --with-x=yes \
  --with-vfs \
  --with-samba \
  --with-configdir=/etc/samba \
  --with-screen=mcslang \
  --build=$CHOST-slackware-linux

# Removed due to bug reports.  Does anyone use this feature?
# Will it be missed?  complain to: volkerdi@slackware.com
#	    --with-samba

make -j3 || exit 1
make DESTDIR=$PKG install || 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
)

mkdir -p $PKG/etc/profile.d
cat $PKG/usr/share/mc/bin/mc.csh > $PKG/etc/profile.d/mc.csh
cat $PKG/usr/share/mc/bin/mc.sh > $PKG/etc/profile.d/mc.sh
chmod 755 $PKG/etc/profile.d/*

# 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/mc-$VERSION
cp -a \
  ABOUT-NLS AUTHORS COPYING FAQ HACKING INSTALL INSTALL.FAST MAINTAINERS NEWS README* TODO \
  $PKG/usr/doc/mc-$VERSION

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild
requiredbuilder -v -y -s $CWD $PKG

# Build package:
cd $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

