#!/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 -fexceptions -frtti"
export CPPFLAGS="-I/usr/local/include"
export LDFLAGS="-L/usr/local/lib"
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

TODAY=`date +%Y/%m/%d`
MIRROR="http://github.com/danieldg/inspircd/tarball/v2.0.0b4"
PACKAGE="inspircd"
SEP="-"
VERSION="2.0.0b4"
DESCRIPTION="IRC server     (TESTING)"
AUTHORS="http://wiki.inspircd.org/Credits"
HOMEPAGE="http://www.inspircd.org/"
LICENSE="GPLv2"
ME="Kingdomcome"
DEPS="perl5.tcz"
BUILDDEPS="openssl-0.9.8m openssl-0.9.8m-dev sqlite3 sqlite3-dev
           gnutls gnutls-dev libgcrypt libgcrypt-dev"
FLAGS="--disable-interactive --prefix=/opt/inspircd --enable-openssl
       --enable-gnutls"
DOCS=""

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

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 -pm 777 ${PKGDIR} ${PKGDIR}/tmp
   cd ${PKGDIR} && wget -O ${SRC} ${MIRROR}
   sudo -u ${TCUSER} tar xf ${SRC} || exit 1
}

buildit(){
   cd ${SRCDIR}/src/modules
   ln -s extra/m_sqlite3.cpp
   ln -s extra/m_sqlv2.h
   cd ${SRCDIR}
   sudo -u ${TCUSER} ./configure ${FLAGS}
   sudo -u ${TCUSER} make
   sudo -u ${TCUSER} make install
}

workit(){
   cd /
   find opt/inspircd -not -type d > /tmp/${PACKAGE}.list
   tar czO -T /tmp/${PACKAGE}.list | tar xzf - -C ${PKGDIR}/tmp
   rm /tmp/${PACKAGE}.list

   cd ${PKGDIR}/tmp
   rm -rf ${DOCS}

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

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

   mkdir -pm 775 usr/local/tce.installed
cat <<EOF> usr/local/tce.installed/${PACKAGE}
#!/bin/sh
[ ! -d /opt/inspircd/data ] && mkdir -pm 775 /opt/inspircd/data && chown root:staff /opt/inspircd/data
[ ! -d /opt/inspircd/logs ] && mkdir -pm 775 /opt/inspircd/logs && chown root:staff /opt/inspircd/logs
EOF
   chmod 775 usr/local/tce.installed/${PACKAGE}

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

#   mkdir -pm 775 usr/local/tce.icons
#cat <<EOF> usr/local/tce.icons/${PACKAGE}
#i:
#t:
#c:
#EOF
#   chmod 644 usr/local/tce.icons/*

#dont clobber tc setup
   chown -R root:staff usr/local/tce.*

#package specific stuff
   chown -R root:staff opt
   chmod 2775 opt
   for each in `find opt -type d -not -name opt`; do
      chmod 775 $each
   done
   chmod 664 opt/inspircd/conf/*
}

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

   mksquashfs . ../${PACKAGE}.tcz

   cd ${PKGDIR}
   md5sum ${PACKAGE}.tcz > ${PACKAGE}.tcz.md5.txt

   [ -f "${PACKAGE}.tcz.dep" ] && rm -f ${PACKAGE}.tcz.dep
   for each in ${DEPS}; do echo ${each} >> ${PACKAGE}.tcz.dep; done

   size=`du -h ${PACKAGE}.tcz | cut -f 1`

cat <<EOF> ${PACKAGE}.tcz.info
Title:		${PACKAGE}.tcz
Description:	${DESCRIPTION}
Version:	${VERSION}
Author:		${AUTHORS}
Original-site:	${HOMEPAGE}
Copying-policy:	${LICENSE}
Size:		${size}
Extension_by:	${ME}
Comments:	This is a beta version of InspIRCd
		Everything is installed into /opt/inspircd
		To run, copy conf/*.example to conf/* and edit configs.
		Then from /opt/inspircd run ./inspircd start
		
		The wiki found at http://wiki.inspircd.org/ has good
		documentation on configuration and running.
		
		Depends on perl5.tcz and optionally on openssl or gnutls
Change-log:	----
Current:	${TODAY} First Version
EOF
}

#here we go

environment
buildit
workit
packageit
