#!/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="-L/usr/local/lib"
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

BUILDSTARTMARK="/tmp/start"
BUILDENDMARK="/tmp/end"
TODAY=`date +%Y/%m/%d`
MIRROR=""
PACKAGE="musescore"
SEP="-"
VERSION="1.1"
EXT="tcz"
DESCRIPTION="WYSIWYG music notation program"
AUTHORS="Werner Schweer and others"
HOMEPAGE="musescore.org"
LICENSE="GNU GPL"
ME="Marco Caminati"
DEPS="Xlibs.tcz
expat2.tcz
flac.tcz
fontconfig.tcz
glib2.tcz
libasound.tcz
libogg.tcz
libsndfile.tcz
libvorbis.tcz
qt-4.x-base.tcz
qt-4.x-extended.tcz
qt-4.x-script.tcz
qt-4.x-webkit.tcz
qt-4.x-xml.tcz"
# Note these are non-recursive deps! i.e.: each of them can be tce-loaded ignoring the respective .dep file, and the present extension should still work
BUILDDEPS=""
NONRECUDEPS="Xlibs.tcz 
alsa-dev.tcz
base-dev.tcz
binutils.tcz
cloog.tcz
cmake.tcz
expat2.tcz
flac.tcz
flac-dev.tcz
fontconfig.tcz
gcc.tcz
gcc_libs.tcz
glib2-dev.tcz
glib2.tcz
gmp.tcz
gperf.tcz
jack-dev.tcz
libasound.tcz
libiconv.tcz
libidn.tcz
libogg-dev.tcz
libogg.tcz
libsndfile-dev.tcz
libsndfile.tcz
libvorbis-dev.tcz
libvorbis.tcz
make.tcz
mpfr.tcz
openssl-0.9.8.tcz
ppl.tcz
pkg-config.tcz
qt-4.x-base.tcz
qt-4.x-dev.tcz
qt-4.x-extended.tcz
qt-4.x-script.tcz
qt-4.x-webkit.tcz
qt-4.x-xml.tcz
"
DOCS="usr/local/share"
# NB a RELATIVE path
REPO=${REPO:="`cat /etc/sysconfig/tce.mount || echo "/tmp"`/tce/optional"}
TCUSER=`cat /etc/sysconfig/tcuser`
SRC="${PACKAGE}${SEP}${VERSION}.tar.gz"
THISDIR=`pwd`
PKGDIR="${THISDIR}/${PACKAGE}"
SRCDIR="${PKGDIR}/mscore${SEP}${VERSION}"

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 -i "${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}"
   touch "$BUILDSTARTMARK"
   make
   touch "$BUILDENDMARK"
   make DESTDIR=${PKGDIR}/tmp/${PACKAGE} install
}

workit(){
   if [ "`ls -A ${PKGDIR}/tmp`" ]; then
      rm "$BUILDSTARTMARK" "$BUILDENDMARK"
   else
      echo "trying a naive install, press enter to go on"
      read
      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
   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
   echo "X-FullPathIcon=/usr/local/share/pixmaps/mscore.png" >> usr/local/share/applications/mscore.desktop
   ( cd usr/local/share/applications/ && ln -s mscore.desktop "${PACKAGE}.desktop" && cd ../pixmaps && ln -s mscore.png "${PACKAGE}.png" && ln -s mscore.xpm "${PACKAGE}.xpm" )
	 
#dont clobber tc setup
#   chown -R root:staff usr/local/tce.*

#package specific stuff

#SEPARATION
mkdir ../${PACKAGE}-data/usr/local/share ../${PACKAGE}-doc/usr/local/share/mscore${SEP}${VERSION} ../${PACKAGE}-locale/usr/local/share/mscore${SEP}${VERSION} -p
mv usr/local/share/mscore${SEP}${VERSION} "../${PACKAGE}-data/usr/local/share"
mv "../${PACKAGE}-data/usr/local/share/mscore${SEP}${VERSION}/locale" "../${PACKAGE}-locale/usr/local/share/mscore${SEP}${VERSION}"
mv "../${PACKAGE}-data/usr/local/share/mscore${SEP}${VERSION}/man" "../${PACKAGE}-doc/usr/local/share/mscore${SEP}${VERSION}"
(cd "../${PACKAGE}-doc/usr/local/share/mscore${SEP}${VERSION}/man/" && rm ` ls | egrep -v -- "-en"`)
}

packageit(){
   cd ${PKGDIR}/tmp/$1
   for dir in `ls -A`; do
      find ${dir} -not -type d | sort >> ../../${1}.${EXT}.list
   done

   mksquashfs . ../../${1}.${EXT}

   cd ${PKGDIR}
   md5sum ${1}.${EXT} > ${1}.${EXT}.md5.txt

   [ -f "${1}.${EXT}.dep" ] && 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:	Import and export of MusicXML and MIDI.
		Integrated sequencer.
		Output as a PDF, SVG or PNG document.
		Built from 68b43af92093a16f7f074c0eb560a867 mscore-1.1.tar.bz2
		available on Original-site
		Runtime dependencies are non-recursive, i.e.: each of them can be tce-loaded 
		ignoring the respective .dep file, and the present extension should still work.
Change-log:	----
Current:	${TODAY} First Version
EOF
}

#here we go

environment "$1"
buildit
workit
packageit "${PACKAGE}"
packageit "${PACKAGE}-data"
packageit "${PACKAGE}-locale"
packageit "${PACKAGE}-doc"

