#!/bin/sh

set -e
set -u
set -x

[ `id -u` = 0 ] || { echo "must be root"; exit 1; }

# export CFLAGS="-march=i486 -Os -pipe"
export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
# export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
# export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe -fno-exceptions -fno-rtti"
# export CPPFLAGS="-I/usr/local/include"
export LDFLAGS="-Wl,-O1"
# export LDFLAGS="-L/usr/local/lib"
# export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

SRCID="$(cd "$(dirname "$1")";md5sum "$(basename "$1")")"
BUILDSTARTMARK="/tmp/start"
BUILDENDMARK="/tmp/end"
OSVERSION="`version`"
TODAY=`date +%Y/%m/%d`
MIRROR=""
PACKAGE="st-xft"
SEP="-"
VERSION="0.2-56d8f361918d"
EXT="tcz"
DESCRIPTION="Xft branch (i.e. with smooth rendering of outline fonts) of a simple terminal emulator for X"
AUTHORS="Anselm R. Garbe, Aurelien Aptel, Devin J. Pohly"
HOMEPAGE="http://st.suckless.org/"
LICENSE="GNU GPL"
ME="Marco Caminati"
DEPS="
fontconfig.tcz
ttf-berasansmono-font.tcz
"
RECUWARN=""
BUILDDEPS="
mpc.tcz
binutils.tcz
fontconfig.tcz
"
NONRECUDEPS="
gcc.tcz
make.tcz
eglibc_base-dev.tcz
linux-3.0.1_api_headers.tcz
Xorg-7.6-dev.tcz
freetype_base-dev.tcz
fontconfig-dev.tcz
libxft.tcz
Xlibs.tcz
ncurses-utils.tcz
ncurses.tcz
ttf-bitstream-vera.tcz
"
UNUSED="
"
DOCS="usr/local/share"
PREFIX=/usr/local 
# NB a RELATIVE path
REPO=${REPO:="/etc/sysconfig/tcedir/optional"}
TCUSER=`cat /etc/sysconfig/tcuser`
SRC="${PACKAGE}${SEP}${VERSION}.tar.gz"
THISDIR=`pwd`
PKGDIR="${THISDIR}/${PACKAGE}"
SRCDIR="${PKGDIR}/st-56d8f361918d"
NAIVE=""

environment(){
   for each in file.tcz binutils.tcz squashfs-tools-4.x.tcz ${BUILDDEPS}; do
# binutils.tcz provides strip
      sudo -u ${TCUSER} tce-load -i ${REPO}/${each}
   done
#   [ -d "${PKGDIR}" ] && rm -rf ${PKGDIR}
   mkdir -p ${PKGDIR}/tmp/${PACKAGE}
   for each in ${NONRECUDEPS}
   do
      yes n | cp -s "${REPO}/${each}" "${PKGDIR}"
      sudo -u ${TCUSER} tce-load -i "${PKGDIR}/${each}"
   done
   cd ${PKGDIR}
#   cp -a ${MIRROR}${SRC} .
   cd "${SRCDIR}" || tar -xf "$1"
   cd "${SRCDIR}"
#   make clean
# $1 is the source tar with full path
}

buildit(){
   cd "${SRCDIR}"
   sed -i -e "3 s/monospace-8/Bera Sans Mono-16/" config.def.h
   touch "$BUILDSTARTMARK"
   make
   touch "$BUILDENDMARK"
   mkdir "${PKGDIR}/tmp/${PACKAGE}/${PREFIX}/bin/" "${PKGDIR}/tmp/${PACKAGE}/${PREFIX}/share/terminfo/" -p
   cp -i ./st "${PKGDIR}/tmp/${PACKAGE}/${PREFIX}/bin/"
   tic -s -o"${PKGDIR}/tmp/${PACKAGE}/${PREFIX}/share/terminfo/" ./st.info
}

workit(){
   if [ "`ls -A ${PKGDIR}/tmp`" ]; then
      rm "$BUILDSTARTMARK" "$BUILDENDMARK"
   else
      cd /
      find usr/ -newer "$BUILDENDMARK" -not -type d > /tmp/${PACKAGE}.list
      tar czO -T /tmp/${PACKAGE}.list | tar xzf - -C ${PKGDIR}/tmp/${PACKAGE}
      rm /tmp/${PACKAGE}.list
      NAIVE="DESTDIR not supported: the files in this package were picked by timestamps."
   fi

   cd ${PKGDIR}/tmp/${PACKAGE}

   find usr/ | xargs file | grep "executable" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs -t strip --strip-unneeded || true 
   find usr/ | xargs file | grep "shared object" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs -t strip -g || true
	 
#dont clobber tc setup
#   chown -R root:staff usr/local/tce.*

#package specific stuff

#SEPARATION
}

packageit(){
   cd ${PKGDIR}/tmp/$1
   find -mindepth 1 -not -type d| busybox sed -e "s_^\./\(.*$\)_\1_" | sort > ../../${1}.${EXT}.list
   mksquashfs . ../../${1}.${EXT}
   cd ${PKGDIR}
   md5sum ${1}.${EXT} > ${1}.${EXT}.md5.txt
   rm -f ${1}.${EXT}.dep
   for each in ${DEPS}; do echo ${each} >> ${1}.${EXT}.dep; done
   size=`du -h ${1}.${EXT} | cut -f 1`

cat <<EOF> ${1}.${EXT}.info
Title:		${1}.${EXT}
Description:	${DESCRIPTION}
Version:	${VERSION}
Author:		${AUTHORS}
Original-site:	${HOMEPAGE}
Copying-policy:	${LICENSE}
Size:		${size}
Extension_by:	${ME}
Comments:	Built under Core version ${OSVERSION} from ${SRCID}
		available on Original-site
		${NAIVE}
		${RECUWARN}
		tce-size under 300k, and smooth fonts under magnification: 
		try st -f "Bera Sans Mono-40", or load and use your favourite monospace outline font.
		Bera Sans dependency is not strict: it could be any other monospace font.
		It acts as a convenient, small-footprint default.
Change-log:	----
Current:	${TODAY} First Version
EOF
}

#here we go

environment "$1"
buildit
workit
packageit "${PACKAGE}"

