#!/bin/sh

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

export CFLAGS="-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

MIRROR="http://downloads.sourceforge.net/project/netpbm/super_stable/10.26.63/"
PACKAGE="netpbm"
SEP="-"
VERSION="10.26.63"
EXT="tczl"
DESCRIPTION="graphics tools and converters     (TESTING)"
AUTHORS="many authors. package maintained by Bryan Henderson"
HOMEPAGE="http://netpbm.sourceforge.net/"
LICENSE="GPL2 MIT BSD"
ME="Kingdomcome"
DEPS="graphics-libs-1.tczl"
BUILDDEPS="perl_xml.tcz graphics-libs-1-devs.tcz"
FLAGS="--prefix=/usr/local"
DOCS="usr/local/README usr/local/VERSION usr/local/man usr/local/misc
      usr/local/pkginfo usr/local/bin/manweb usr/local/bin/doc.url"

TCUSER=`cat /etc/sysconfig/tcuser`
SRC="${PACKAGE}${SEP}${VERSION}.tgz"
THISDIR=`pwd`
PKGDIR="${THISDIR}/${PACKAGE}"
SRCDIR="${PKGDIR}/${PACKAGE}${SEP}${VERSION}"

environment(){
   for each in compiletc.tcz squashfs-tools-4.0.tcz ${DEPS} ${BUILDDEPS}; do
      sudo -u ${TCUSER} tce-load -w -i ${each}
   done
   [ -d "${PKGDIR}" ] && rm -rf ${PKGDIR}
   mkdir -p ${PKGDIR}/tmp/usr
   cd ${PKGDIR}
   wget ${MIRROR}${SRC} || exit 1
   tar xzf ${SRC} || exit 1
}

buildit(){
   cd ${SRCDIR}
   cp Makefile.config.in Makefile.config
cat <<EOF>> Makefile.config

DEFAULT_TARGET = nonmerge
NETPBMLIBTYPE=unixshared
NETPBMLIBSUFFIX=so
STATICLIB_TOO=y
CFLAGS = -O3 -ffast-math  -pedantic -Wall -Wno-uninitialized -Wmissing-declarations -Wimplicit -Wwrite-strings -Wmissing-prototypes
CFLAGS_MERGE = -Wno-missing-declarations -Wno-missing-prototypes
LDRELOC = ld --reloc
LINKER_CAN_DO_EXPLICIT_LIBRARY=Y
LINKERISCOMPILER = Y
TIFFLIB = libtiff.so
JPEGLIB = libjpeg.so
PNGLIB = libpng.so
ZLIB = libz.so
NETPBM_DOCURL = http://netpbm.sourceforge.net/doc/
EOF
   make || exit 1
   make package pkgdir=${PKGDIR}/tmp/usr/local
}

workit(){
   cd ${PKGDIR}/tmp
   rm -rf ${DOCS}

   mkdir -p usr/local/share/doc/License
   cp ${SRCDIR}/doc/copyright_summary usr/local/share/doc/License/${PACKAGE}.txt

   find usr/ | xargs file | grep "executable" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
   find usr/ | xargs file | grep "shared object" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip -g 2> /dev/null

#mkdir -p usr/local/tce.installed
#cat <<EOF> usr/local/tce.install/${PACKAGE}
#!/bin/sh

#EOF
#chmod 755 usr/local/tce.installed/${PACKAGE}

#mkdir -p usr/local/tce.menu
#cat <<EOF> usr/local/tce.menu/${PACKAGE}
#<JWM>
#  <Program label=" "></Program>
#</JWM>
#EOF

#mkdir -p usr/local/tce.icons
#cat <<EOF> usr/local/tce.icons/${PACKAGE}
#i:
#t:
#c:
#EOF

#package specific stuff
   mv usr/local/link/* usr/local/lib/
   rm -rf usr/local/link
### licenses not found for these binaraies
   nolicense="pamchannel pamtopnm pbmto4425 pbmtoln03
              pbmtolps pbmtopk pktopbm ppmtopjxl spottopgm"
   for file in ${nolicense}; do
      rm -f usr/local/bin/${file}
   done
}

packageit(){
   cd ${PKGDIR}/tmp
   for dir in `ls`; do
      find ${dir} -not -type d | sort >> ../${PACKAGE}.${EXT}.list
   done
   mksquashfs . ../${PACKAGE}.${EXT}
   cd ${PKGDIR}
   md5sum ${PACKAGE}.${EXT} > ${PACKAGE}.${EXT}.md5.txt
   [ -f "${PACKAGE}.${EXT}.dep" ] && rm -f ${PACKAGE}.${EXT}.dep
   for each in ${DEPS}; do echo ${each} >> ${PACKAGE}.${EXT}.dep; done
size=`du -h ${PACKAGE}.${EXT} | cut -f 1`
today=`date +%Y/%m/%d`
cat <<EOF> ${PACKAGE}.${EXT}.info
Title:		${PACKAGE}.${EXT}
Description:	${DESCRIPTION}
Version:	${VERSION}
Author:		${AUTHORS}
Original-site:	${HOMEPAGE}
Copying-policy:	${LICENSE}
Size:		${size}
Extension_by:	${ME}
Comments:	Netpbm is a toolkit for manipulation of graphic images.
		Capable of converting to and from many formats;
		resizing, cropping, and mirroring images;
		and creating a sequence of images that fade from one
		image to another image.
		User manual available at http://netpbm.sourceforge.net/doc
		
		This extension is PPI compatible
Change-log:	----
Current:	${today} First Version
EOF
}

#here we go

environment
buildit
workit
packageit
