#! /bin/bash
# 
# $Id: build-static,v 1.7 2005/12/28 04:50:21 lace Exp $
# Relink already built package fully statically.
# Copyright (C) 2005 Jan Kratochvil <project-captive@jankratochvil.net>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; exactly version 2 of June 1991 is required
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


# It is not possible to just redefine Makefile variables as there are pushed
# various threading libraries etc. all the time.

# Designed+tested only for Fedora Core 4 (+updates). The list of libraries will
# definitely need to be changed for other distributions.

# Required precompiled static libraries .a and some .c stubs:
STATIC_PATH=/usr/local/staticbuild


set -e -o pipefail
if test "$1" != link;then
	set -x
	./autogen.pl --dist
	(set -x;CFLAGS="-O2" CPPFLAGS="-I$STATIC_PATH/include/gnome-vfs-module-2.0" \
			./configure --enable-maintainer-mode --disable-shared --enable-static --enable-install-pkg \
			--enable-standalone \
			--enable-standalone-fonts=/usr/local/share/captive/fonts \
			--disable-standalone-yum \
		) 2>&1|tee errs1
#			--enable-standalone-yum=/etc/yum.repos.d
	make clean
	make 2>&1|tee errs2
	set +x
	fi

targets="
		src/client/fuse/mount.captive
		src/client/fuse/captive-fusermount
		src/client/sandbox-server/captive-sandbox-server
		src/client/cmdline/captive-cmdline
		src/install/acquire/captive-install-acquire
		src/install/fstab/captive-install-fstab
		"
# FIXME: Make all the other relinks also properly conditional.
if grep -q 's,@ENABLE_BUG_REPLAY_TRUE@,,' config.status;then
	targets="$targets
			src/client/bug-replay/captive-bug-replay
			"
	fi
(for target in $targets;do
	dir=`dirname $target`
	base=`basename $target`
	rm -f $target
	# If you want to be really bad: s/-n// and s/_OBJ/OBJ/
	objects="$(make -s -C $dir -f Makefile -f - print_objects <<HERE

print_objects:
	echo '\$(addprefix $dir/,\$(`echo -n $base|tr -c a-zA-Z _`_OBJECTS))'

HERE
)"
#	set -x
	gcc -O2 -static -Wall -Wstrict-prototypes -Wsign-compare -o $target $objects `pkg-config --cflags gmodule-2.0 gthread-2.0` $STATIC_PATH/src/*.c -L$STATIC_PATH/lib/ -L./src/install/libcaptive-install/ -lcaptive-install -L./src/libcaptive/.libs/ -lcaptive -L/usr/X11R6/lib -L/usr/lib/gnome-vfs-2.0/modules -lgnomevfs-2 -lreadline -ltermcap -lcrypto -lntfs-gnomevfs -lntfs -lfuse -lgnomeui-2 -lSM -lICE -lbonoboui-2 -lgnomecanvas-2 -lgnome-2 -lpopt -lart_lgpl_2 -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lpangoxft-1.0 -lpangoft2-1.0 -lpangox-1.0 -lpango-1.0 -lgobject-2.0 -lxml2 -lbonobo-2 -lgconf-2 -lbonobo-activation -lORBitCosNaming-2 -lORBit-2 -lm -lgthread-2.0 -lXft -lfontconfig -lfreetype -lexpat -lXrandr -lXinerama -lXfixes -lXcursor -lXext -lXrender -lX11 -lpng -lz -lglib-2.0 -lc -lnss_files -lnss_dns -lresolv
	set +x
	done) 2>&1|tee errs2
echo OK
ls -l $targets
