#!/bin/sh

. /etc/init.d/tc-functions

checkroot

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

TODAY=`date +%Y/%m/%d`
PACKAGE="usb-modeswitch"
MIRROR="http://www.draisberghof.de/usb_modeswitch/"
SEP="-"
VERSION="1.1.7"
DESCRIPTION="Activating Switchable USB Devices"
AUTHORS="Josua Dietze"
HOMEPAGE="http://www.draisberghof.de/${PACKAGE}/"
LICENSE="GPLv2"
ME="jls_legalize <unsenepopiu at tin dot it>"
DEPS="tcl_tk.tcz \
usb-serial-2.6.33.3-tinycore.tcz
usb-utils.tcz"
BUILDDEPS="libusb-dev.tcz"
FLAGS=""
DOCS="usr/share"
TCUSER=`cat /etc/sysconfig/tcuser`
SRC="${PACKAGE}${SEP}${VERSION}.tar.bz2"
MD5=${SRC}.md5
THISDIR=`pwd`
PKGDIR="${THISDIR}/${PACKAGE}"
SRCDIR="${PKGDIR}/${PACKAGE}${SEP}${VERSION}"
TCEDIR="$(cat /opt/.tce_dir)"
COPYING="COPYING"

clean(){
	rm -rf ${THISDIR}/${PACKAGE}
}
environment(){
	for each in compiletc.tcz squashfs-tools-4.x.tcz ${DEPS} ${BUILDDEPS}; do
		APPNAME=$(getbasefile "$each" 1)
		if [ ! -f /usr/local/tce.installed/"${APPNAME}" ]; then
			if [ ! -e ${TCEDIR}/optional/${each} ]; then
				sudo -u ${TCUSER} tce-load -w ${each}
			fi
			sudo -u ${TCUSER} tce-load -i ${TCEDIR}/optional/${each}
		fi
	done
	[ -d "${PKGDIR}" ] && rm -rf ${PKGDIR}
#	echo "downloading:  ${MIRROR}${SRC}"
	mkdir -p ${PKGDIR}/tmp
#	mkdir -p ${PKGDIR}/tmp/etc
#	mkdir -p ${PKGDIR}/tmp/usr/share/man/man1
	cd ${PKGDIR} && wget ${MIRROR}${SRC}
	tar xjf ${SRC}

}

buildit(){
   cd ${SRCDIR}
   #./configure ${FLAGS} > ../config-res
   #make
   make DESTDIR=${PKGDIR}/tmp install
}

workit(){
	cd ${PKGDIR}/tmp
	rm -rf ${DOCS}
	
	mkdir -p usr/local/share/doc/License
	cp ${SRCDIR}/COPYING 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 -pm 775 usr/local/tce.installed
	cat <<EOF> usr/local/tce.installed/${PACKAGE}
#!/bin/sh
ln -s /usr/local/bin/tclsh /usr/bin/
EOF

	chmod 775 usr/local/tce.installed/${PACKAGE}
}

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
   sed -i "s/2.6.33.3-tinycore/KERNEL/" ${PACKAGE}.tcz.dep

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

cat <<EOF> ${PACKAGE}.tcz.info
Title:		${PACKAGE}.tcz (TESTING)
Description:	${DESCRIPTION}
Version:	${VERSION}
Author:		${AUTHORS}
Original-site:	${HOMEPAGE}
Copying-policy:	${LICENSE}
Size:		${size}
Extension_by:	${ME}
Comments:	Reboot is needed.

		This extension is NOT PPI compatible
Change-log:     2010/06/11 First Version 1.1.2
		2011/01/21 updated to 1.1.6
Current:	${TODAY} updated to ${VERSION}
EOF
}
tarit(){	
	cd ${PKGDIR}
	rm ${PACKAGE}.tar.bz2
	cp ../${PACKAGE}.tcbuild .
	tar	cjvf ../${PACKAGE}$SEP$VERSION.tar.bz2 \
		${PACKAGE}.tcz* \
		${PACKAGE}.tcbuild \
		${SRC}
}

checkit(){
	sudo chown ${TCUSER}:staff ${THISDIR}
	cd ${PKGDIR}
	if [ ! -f /usr/local/tce.installed/submitqc ]; then
		if [ ! -e ${TCEDIR}/optional/submitqc ]; then
			sudo -u ${TCUSER} tce-load -w submitqc
		fi
		su ${TCUSER} -c "tce-load -is submitqc"
	fi
	submitqc
}

#here we go
clean
environment
echo -n "Press Enter key."
read gagme
buildit
echo -n "Press Enter key."
read gagme
workit
echo -n "Press Enter key."
read gagme
packageit
echo -n "Press Enter key."
read gagme
checkit
tarit

