#!/bin/sh
# Script by Zeqadious

# PACKAGE: libdrm
# DEPENDENCIES: -
# HOMEPAGE: http://dri.freedesktop.org
# COMPILE EXTRA REQUIREMENTS:

# ------ Verify Script
if [ "$(id -u)" != "0" ]; then
	echo "This script must be run as root!"
	exit
fi

# ------ Help
if [ "${1}" = "--help" ]; then
	echo \
	"VL ${0} script:
			--nocleanup	Do NOT Cleanup of Post build and Temporary Repo
			--download	Download the sources
			--help		Displays this help"
	exit
fi

# ------ Credentials
NAME=libdrm
VERSION=2.3.0
ARCH=${ARCH:-"i586"}
BUILD=${BUILD:-1}
VEC=${VEC:-"vl59"}
EXT=${EXT:-"tlz"}
REPO=${REPO:-"/home/PACKAGES"}
CFLAGS=${CFLAGS:-"-O2 -march=i586 -mtune=i686"}
export LDFLAGS="-Wl,--hash-style=gnu"
SOURCE=http://dri.freedesktop.org/libdrm/${NAME}-${VERSION}.tar.bz2
# ------ Setup
CWD=$(pwd)
TMP=${CWD}/build
PKG=${TMP}/package-${NAME}
# command line options
if [ "${1}" = "--nocleanup" -o "${2}" = "--nocleanup" ]; then
	CLEANUP="0"
else
	CLEANUP="1"
fi
if [ "${1}" = "--download" -o "${2}" = "--download" ]; then
	DOWNLOAD="1"
else
	DOWNLOAD="0"
fi
# build location
if [ ! -d ${TMP} ]; then
	install -d -m700 ${TMP}
fi
# repo location
if [ ! -d ${REPO} ]; then
	install -v -d -m755 ${REPO}
fi

# ------ Slack Desc
install -d -m755 ${PKG}/install
cat > ${PKG}/install/slack-desc << EoF
#      |-----handy-ruler------------------------------------------------------|
${NAME}: DRM Interface Library
${NAME}:
${NAME}: The DRM is a kernel module that gives direct hardware access to DRI
${NAME}: clients
${NAME}:
EoF

# ------ Retrieve & Decompress
X=0
while [ "${SOURCE[X]}" != "" ]; do
	[ "${DOWNLOAD}" = "1" ] && \
		( $(which wget) -c ${SOURCE[X]} || exit 1 )
	X=$((X + 1))
done
for file in *.tar.bz2; do
	tar jxvf ${file} -C ${TMP}
done
for file in *.tar.gz; do
	tar zxvf ${file} -C ${TMP}
done

# ------ Build Package
cd ${TMP}/${NAME}-${VERSION}

CFLAGS=${CFLAGS} CXXFLAGS=${CFLAGS} \
./configure \
	--program-prefix="" \
	--program-suffix="" \
	--prefix="/usr" \
	--sysconfdir="/etc" \
	--localstatedir="/var" \
	--mandir="/usr/man" \
	--infodir="/usr/info" \
	--enable-static="no" \
	--enable-shared="yes" \
	${ARCH}-slackware-linux || exit
	
make || exit
make install DESTDIR="${PKG}" || exit


# buildscript
install -d -m755 ${PKG}/usr/src/${NAME}-${VERSION}
install -m755 ${CWD}/${0} ${PKG}/usr/src/${NAME}-${VERSION}/build-${NAME}.sh

# ------ Finalize
# strip
find ${PKG} | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
find ${PKG} | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

# gzip man pages
find ${PKG}/usr/man -type f -exec gzip -9f {} \;

# gzip info pages
find ${PKG}/usr/info -type f -exec gzip -9f {} \;

# documentation
install -d -m755 ${PKG}/usr/doc/${NAME}-${VERSION}
cp -a \
  AUTHORS* COPYING* INSTALL* NEWS* README* ChangeLog* LICENSE* \
  ${PKG}/usr/doc/${NAME}-${VERSION}
# move incorrect doc installs
if [ -d ${PKG}/usr/share/doc ]; then
	if [ -d ${PKG}/usr/share/doc/${NAME}-${VERSION} ]; then
		( cd ${PKG}/usr/share/doc/${NAME}-${VERSION} ; mv -v * ${PKG}/usr/doc/${NAME}-${VERSION} )
	else
		( cd ${PKG}/usr/share/doc ; mv -v * ${PKG}/usr/doc/${NAME}-${VERSION} )
	fi
	rm -rf ${PKG}/usr/share/doc
fi

# set permissions
chown -R root:root ${PKG}
find ${PKG} -perm 664 -exec chmod 644 {} \;
find ${PKG} -perm 600 -exec chmod 644 {} \;
find ${PKG} -perm 444 -exec chmod 644 {} \;
find ${PKG} -perm 400 -exec chmod 644 {} \;
find ${PKG} -perm 440 -exec chmod 644 {} \;
find ${PKG} -perm 777 -exec chmod 755 {} \;
find ${PKG} -perm 775 -exec chmod 755 {} \;
find ${PKG} -perm 511 -exec chmod 755 {} \;
find ${PKG} -perm 711 -exec chmod 755 {} \;
find ${PKG} -perm 555 -exec chmod 755 {} \;

# ------ Create
cd ${PKG}

# build package
#makepkg -l y -c n -p ${REPO}/${NAME}-${VERSION}-${ARCH}-${VEC}.${BUILD}.${EXT}
makeslapt --tlz ${REPO}/${NAME}-${VERSION}-${ARCH}-${BUILD}${VEC}.${EXT}
# build md5 checksum
( cd ${REPO} ; md5sum ${NAME}-${VERSION}-${ARCH}-${BUILD}${VEC}.${EXT} \
	> ${NAME}-${VERSION}-${ARCH}-${BUILD}${VEC}.md5 )
# build dep file
cp install/slack-required ${NAME}-${VERSION}-${ARCH}-${BUILD}${VEC}.dep

# ------ Cleanup
if [ "${CLEANUP}" = "1" ]; then
	rm -rf ${TMP}
fi
