#
#	Build a uClinux/distribution source package from git tree.
#
#	(C) Copyright 1999-2013, Greg Ungerer <greg.ungerer@accelecon.com>
#
#	Takes a git tree a ssource and builds a uClinux-dist package.
#

NAME=uClinux-dist
SOURCE=${1:-uclinux-dist.working}
PKGDIR=images

#
#	KEEP_BOOT=true
#	KEEP_IXP_MODULES=true
#	KEEP_OCF_MODULES=true
#	KEEP_PROP=true
#	KEEP_MODULE_CONFIGS=true
#

# If we keep crypto or ixp modules, we need to keep the module configs too.
[ "$KEEP_CRYPTO" = "true" -o "$KEEP_IXP_MODULES" = "true" ] && KEEP_MODULE_CONFIGS=true


DATE=`date +%Y%m%d`
PACKAGE=${NAME}-${DATE}

DIRS="README SOURCE COPYING Documentation Makefile config linux-2.4.x linux-3.x freeswan openswan lib glibc uClibc user include tools vendors bin"

[ "$KEEP_PROP" = "true" ] && DIRS="$DIRS prop"
[ "$KEEP_BOOT" = "true" ] && DIRS="$DIRS boot"

CURPWD=`pwd`
SRCDIR=${CURPWD}/${SOURCE}
DSTDIR=${CURPWD}/${NAME}
SLASHPKGDIR=${CURPWD}/${PKGDIR}/.


#
#	Check if target directory exists, bail if it does.
#
if [ -d "${DSTDIR}" ]
then
	echo "ERROR: directory ${DSTDIR} exists, stopping now!"
	exit 1
fi


#
#	Remove any old current images hanging around...
#
[ -d ${SLASHPKGDIR} ] || mkdir -p ${SLASHPKGDIR}
rm -f ${SLASHPKGDIR}/${PACKAGE}*


#
#	Start creating new directory
#
echo "Creating ${DSTDIR} directory..."
mkdir ${DSTDIR}
cd ${SRCDIR}
echo "Copying ${SRCDIR} to ${DSTDIR}..."
find ${DIRS} -print | grep -v CVS | cpio -pmd ${DSTDIR}

if [ "$KEEP_MODULE_CONFIGS" = "true" ]; then
	echo "Copying module dir configs..."
	mkdir -p ${DSTDIR}/modules
	head -9 ${SRCDIR}/modules/Kconfig > ${DSTDIR}/modules/Kconfig
	sed -e ':b /\\$/{ ; N ; s/\\\n// ; bb ; }' ${SRCDIR}/modules/Makefile |\
		sed -e "s/^subdir-m.*$/subdir-m := /" \
		> ${DSTDIR}/modules/Makefile
fi

if [ "$KEEP_OCF_MODULES" = "true" ]; then
	echo "Copying modules/ocf..."
	find modules/ocf -print | grep -v CVS | cpio -pd ${DSTDIR}
	find ${DSTDIR}/modules/ocf -name "*.o" | xargs rm -f
	echo "source ocf/Kconfig" >> ${DSTDIR}/modules/Kconfig
	cp ${DSTDIR}/modules/Makefile ${DSTDIR}/modules/Makefile.bak
	sed -e "s/^subdir-m := /subdir-m := ocf /" \
		${DSTDIR}/modules/Makefile.bak \
		> ${DSTDIR}/modules/Makefile
	rm ${DSTDIR}/modules/Makefile.bak
	cp ${SRCDIR}/prop/include/octeon-asm.h ${DSTDIR}/modules/ocf/cryptocteon/
else
	cd ${DSTDIR}
	find vendors -name "config.linux*" |
	while read FILE
	do
		cp ${FILE} ${FILE}.org
		sed -e "s/CONFIG_KLIPS_OCF=y/# CONFIG_KLIPS_OCF is not set/g" \
		    ${FILE}.org > ${FILE}
		rm -f ${FILE}.org
	done
	sed -e 's/-I$(ROOTDIR)\/modules\/ocf//' \
		${SRCDIR}/linux-2.4.x/net/ipsec/Makefile \
		> ${DSTDIR}/linux-2.4.x/net/ipsec/Makefile
	sed -e "s/bool '   IPsec: OCF HW Acceleration support' CONFIG_KLIPS_OCF//" \
		${SRCDIR}/openswan/linux/net/ipsec/Config.in.os2_4 \
		> ${DSTDIR}/openswan/linux/net/ipsec/Config.in.os2_4
	cd ${SRCDIR}
fi

if [ "$KEEP_IXP_MODULES" = "true" ]; then
	echo "Copying modules/ixp425..."
	find modules/ixp425 -print | grep -v CVS | cpio -pd ${DSTDIR}
	find ${DSTDIR}/modules/ixp425 -name "*.o" | grep -v dsr.o | xargs rm -f
	echo "source ixp425/Kconfig" >> ${DSTDIR}/modules/Kconfig
	cp ${DSTDIR}/modules/Makefile ${DSTDIR}/modules/Makefile.bak
	sed -e "s/^subdir-m := /subdir-m := ixp425 /" \
		${DSTDIR}/modules/Makefile.bak \
		> ${DSTDIR}/modules/Makefile
	rm ${DSTDIR}/modules/Makefile.bak
fi

if [ "$KEEP_PROP" != "true" ]; then
	cp ${DSTDIR}/config/Kconfig /tmp/
	grep -v prop /tmp/Kconfig > ${DSTDIR}/config/Kconfig
	cp ${DSTDIR}/config/Makefile /tmp/
	sed -e "s/\.\.\/prop\/Kconfig\.auto//g" < /tmp/Makefile |
		sed -e "s/\.\.\/prop\/Makefile\.auto//g" \
		> ${DSTDIR}/config/Makefile
fi


if [ "$KEEP_BOOT" != "true" ]; then
	cp ${DSTDIR}/config/Kconfig /tmp/
	grep -v boot /tmp/Kconfig > ${DSTDIR}/config/Kconfig
	cp ${DSTDIR}/config/Makefile /tmp/
	sed -e "s/\.\.\/boot\/Kconfig\.auto//g" < /tmp/Makefile |
		sed -e "s/\.\.\/boot\/Makefile\.auto//g" \
		> ${DSTDIR}/config/Makefile
fi

#
#       Remove all binaries, top level clean...
#
echo "Removing objects from ${DSTDIR}..."
cd ${DSTDIR}
make dep 2>&1 | cat > /tmp/dep
echo "making clean ..."
make distclean 2>&1 | cat > /tmp/clean
make -C linux-2.4.x distclean 2> /dev/null > /dev/null
make -C linux-3.x distclean 2> /dev/null > /dev/null
make -C config/scripts clean 2> /dev/null > /dev/null
make -C config/scripts/lxdialog clean 2> /dev/null > /dev/null
find linux-3.x -name ".*.cmd" | xargs rm -f
rm -f linux-3.x/scripts/kconfig/*.o
rm -f linux-3.x/scripts/kconfig/lex.zconf.c
rm -f linux-3.x/scripts/kconfig/zconf.hash.c
rm -f linux-3.x/scripts/kconfig/zconf.tab.c
rm -f linux-3.x/scripts/kconfig/conf
rm -f linux-3.x/scripts/basic/fixdep
rm -f linux-3.x/scripts/basic/docproc
rm -f linux-3.x/scripts/basic/hash
rm -rf linux-3.x/include/config
rm -f linux-2.4.x/include/asm-armnommu/arch
rm -f linux-2.4.x/include/asm-armnommu/proc
rm -f uClibc/extra/config/*.o
cd ..


#
#	Remove mention of some vendors from configs...
#
echo "removing vendors..."
rm -rf ${DSTDIR}/vendors/ATT


#
#	Remove other miscellaneous stuff...
#
echo "Cleaning out miscellaneous other stuff..."
cd ${DSTDIR}
find . -name errs -print | xargs rm -f
find . -name .depend -print | xargs rm -f
find . -name .depend.embed -print | xargs rm -f
find . -name .version -print | xargs rm -f
find . -name .svn -print | xargs rm -rf
find user/iptables -name "*.d" | xargs rm -f
rm -f linux-2.?.x/.config*
rm -f linux-2.?.x/include/linux/autoconf.h
rm -f linux-2.?.x/include/asm
rm -f linux-2.?.x/include/asm-arm/arch
rm -f linux-2.?.x/include/asm-arm/proc
rm -f .config config.arch
rm -f config/autoconf.h config/.config config/.config.old
rm -rf vendors/.config vendors/.config.old
rm -rf vendors/Hitachi/dsp
rm -f user/iptables/.makefirst
rm -f images/*
rm -rf romfs
rm -rf tools/c++ tools/gcc-* tools/misc/gcc-include
rm -f tools/misc/pivio*
rm -f tools/misc/nap5272.gdb
rm -f tools/misc/*.hdc
rm -rf tools/misc/mips/sg590.majic
rm -f lib/uClibc
rm -f uClibc/.config*
rm -f uClibc/extra/config/conf uClibc/extra/config/zconf.tab.[ch]
rm -f uClibc/extra/config/lex.zconf.c uClibc/extra/config/lkc_defs.h
rm -rf uClibc/include/bits
rm -f uClibc/include/fpu_control.h uClibc/include/net/bpf.h

if [ "$KEEP_MODULE_CONFIGS" != "true" ]; then
	find vendors -name config.modules | xargs rm -f
fi

if [ "$KEEP_IXP_MODULES" != "true" ]; then
	cd ${DSTDIR};
	find vendors -name "config.linux-2.4.x" |
	while read FILE
	do
		cp ${FILE} ${FILE}.org
		sed -e "s/CONFIG_IXP4XX_CRYPTO=y/# CONFIG_IXP4XX_CRYPTO is not set/g" \
		    ${FILE}.org > ${FILE}
		rm -f ${FILE}.org
	done
fi


#
#	Remove private vendor names from config files.
#
cd ${DSTDIR}
find vendors -name "config.device*" |
while read FILE
do
	grep -v "CONFIG_DEFAULTS_ADTRAN_" ${SRCDIR}/${FILE} | \
	    grep -v "CONFIG_DEFAULTS_ATT_" | \
	    grep -v "CONFIG_DEFAULTS_MARCONI_" | \
	    grep -v "CONFIG_DEFAULTS_GILBARCO_" \
	    > ${DSTDIR}/${FILE}
done


#
#	Change some permissions, so they look nice.
#
chmod 775 ${DSTDIR}/bin/*

#
#	Fix up some autogen files so they don't get rebuilt.
#
touch ${DSTDIR}/user/mawk/array.[ch]

echo "Packaging ${DSTDIR}..."
cd ${CURPWD}
tar cf ${PACKAGE}.tar ${NAME}
echo "gziping ${PACKAGE}.tar..."
bzip2 -9 -k ${PACKAGE}.tar
gzip -9 ${PACKAGE}.tar

mv ${PACKAGE}.tar.gz ${PACKAGE}.tar.bz2 ${PKGDIR}

exit 0
