#!/bin/sh
CWD=$(pwd)
TMP=${TMP:-/tmp}
if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
PKG=$TMP/package-mc

VERSION=4.6.1_20070623p14
ARCH=${ARCH:-i486}
BUILD=${BUILD:-2}

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_20070623_utf8_mls_r2008-08-28.diff.gz | patch -p1 --verbose || exit 1
zcat $CWD/filegui.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 )
#
# Removed due to bug reports.  Does anyone use this feature?
# Will it be missed?  complain to: volkerdi@slackware.com
#	    --with-samba
#
# To enable samba, I believe these are the options to use:
# --with-samba \
# --with-configdir=/etc/samba \
# --with-codepagedir=/etc/codepages \
#
# The samba options were added back due to requests, so libsmbclient is
# again a dependency of mc...
#
# 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-codepagedir=/etc/codepages \
  --build=$ARCH-slackware-linux

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

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