#!/bin/bash

# Copyright 2004, ..., 2017  Jean-Philippe Guillemin <jp.guillemin@free.fr>
# All rights reserved.
#
#   Permission to use, copy, modify, and distribute this software for
#   any purpose with or without fee is hereby granted, provided that
#   the above copyright notice and this permission notice appear in all
#   copies.
#
#   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
#   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#   IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
#   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
#   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.

export CWD=$(pwd)

export NAME="$1"
if [ "$NAME" = "" ]; then
	echo "usage $0 NAME"
	exit
fi
export ME=$(which $0)



SRCPKG=$(ls ${NAME}*tar* 2>/dev/null || ls ${NAME}*zip 2>/dev/null)
export VERSION=${SRCPKG%.*};VERSION=${VERSION%.*}; export VERSION=${VERSION##*-}

BUILD="$(date '+%y%m%d')"

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
	case "$( uname -m )" in
		i?86) export ARCH=i486 ;;
		arm*) export ARCH=arm ;;
		# Unless $ARCH is already set, use uname -m for all other archs:
		   *) export ARCH=$( uname -m ) ;;
	esac
fi

if [ "$ARCH" = "i486" ]; then
	SLKCFLAGS="-O2 -march=i486 -mtune=i686"
	export LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
	SLKCFLAGS="-O2 -march=i686 -mtune=i686"
	export LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
	SLKCFLAGS="-O2 -fPIC"
	export LIBDIRSUFFIX="64"
else
	SLKCFLAGS="-O2"
	export LIBDIRSUFFIX=""
fi



export PKG="/tmp/$NAME-$VERSION-$ARCH-$BUILD"
rm -rf $PKG
rm -f $PKG.txz
mkdir -p $PKG

export TMP="$CWD/tmp"

cd $CWD
rm -rf $TMP
mkdir -p $TMP

(
cd $TMP
tar xvf $CWD/$SRCPKG || unzip $CWD/$SRCPKG
)


export SRC=$(ls -d $TMP/*/)

  
cd $SRC
chown -R root.root .
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;

mkdir -p $PKG/usr/doc/$NAME-$VERSION

export ICEAUTH='/usr/bin/iceauth'
export EXTRACONFIG="--enable-maintainer-mode"

export CFLAGS="$SLKCFLAGS" 
export CXXFLAGS="$SLKCFLAGS" 

# some custom pre build instructions
if [ -e $CWD/prebuild.sh ]; then
	/bin/bash $CWD/prebuild.sh 
	cat $CWD/prebuild.sh > $PKG/usr/doc/$NAME-$VERSION/prebuild.sh
fi

# Let's try to be automagic
export PREFIX=/usr 
export LIBINSTALLDIR=/lib${LIBDIRSUFFIX} 

if [ -e $SRC/setup.py ]; then
	python setup.py install --root=$PKG
elif [ -e $SRC/Makefile.PL ]; then
	perl Makefile.PL
	make
	make install DESTDIR=$PKG
else

	if [ -e $CWD/configure.sh ]; then
		/bin/bash $CWD/configure.sh 
		cat $CWD/configure.sh > $PKG/usr/doc/$NAME-$VERSION/configure.sh
	else

		[ ! -e $SRC/configure ] && ./autogen.sh || autoreconf -fiv


		./configure \
		  --prefix=/usr \
		  --mandir=/usr/man \
		  --docdir=/usr/doc/$NAME-$VERSION \
		  --libdir=/usr/lib${LIBDIRSUFFIX} \
		  --localstatedir=/var \
		  --localedir=/usr/share/locale \
		  --sysconfdir=/etc \
		  --enable-static=no \
		  --disable-static \
		  --enable-shared \
		  --enable-compositor \
		  --enable-xinerama \
		  --enable-final \
		  --enable-gio \
		  --disable-debug \
		  --disable-schemas-compile \
		  --enable-pluggable-dialogs \
		  --enable-sound-settings \
		  --with-sound=pulseaudio \
		  --enable-gtk-doc=no \
		  --enable-gtk-doc-html=no \
		  --enable-gtk3 \
		  --enable-notifications \
		  --disable-schemas-compile \
		  --disable-libsystemd-login \
		  --build=$ARCH-zenwalk-linux \
		  $EXTRACONFIG
	fi		

	if [ -e $CWD/build.sh ]; then
		/bin/bash $CWD/build.sh 
		cat $CWD/build.sh > $PKG/usr/doc/$NAME-$VERSION/build.sh
	else
		sed -i -e 's|share/man|man|g' Makefile
		make -j6 || exit 1
		make install DESTDIR=$PKG LIBINSTALLDIR=/usr/lib${LIBDIRSUFFIX} PREFIX=/usr
	fi
fi
# docs
cp -f AUTHORS INSTALL README TODO NEWS ChangeLog NOTICE documentation/* $PKG/usr/doc/$NAME-$VERSION
cat $ME > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild


# no more .la  
rm -f $PKG/usr/lib${LIBDIRSUFFIX}/*.la

# Striptease
( 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
	find . -name *.la | xargs rm -f 
	for mandir in $(find ./usr/share/man -name man* -type d) ; do
		mkdir -p ./usr/man/$(basename $mandir)
		mv $mandir/*.? ./usr/man/$(basename $mandir)
	done	  
	rm -rf ./usr/share/man
	for mandir in $(find . -name man -type d) ; do
		if [ -d $mandir ]; then
			( cd $mandir
				find . -type f -name "*.?" -exec gzip -9 {} \;
			)
		fi
	done
)

# redundant , if any
rm -rf $PKG/usr/share/doc


cd $CWD

# some custom post build instructions
if [ -e $CWD/postbuild.sh ]; then
	/bin/bash $CWD/postbuild.sh 
	cat $CWD/postbuild.sh > $PKG/usr/doc/$NAME-$VERSION/postbuild.sh
fi

# slack-desc, doinst.sh
mkdir -p $PKG/install

if [ -e $CWD/slack-desc ]; then
	cat $CWD/slack-desc > $PKG/install/slack-desc
else
	cat > $PKG/install/slack-desc << END
$NAME: Zenwalk package for $NAME 
$NAME:
$NAME: This $NAME package was created for Zenwalk GNU Linux.  
$NAME: It can be installed on any Slackware Linux system.
$NAME: 
$NAME: For more information, see:  http://www.zenwalk.org
$NAME:
$NAME:
$NAME:
$NAME:
$NAME:
END

fi

if [ -e $CWD/doinst.sh ]; then
	cat $CWD/doinst.sh > $PKG/install/doinst.sh
fi

cd $PKG
makepkg -l y -c n $PKG.txz

# Cleanup
cd $CWD
rm -rf $PKG
rm -rf $TMP
