#!/bin/sh

test -z "$LIBTOOL" && LIBTOOL=libtool
test -z "$CC" && CC=gcc

instdir="`pwd`/inst"

# First, source for the dummy dependency library "libdep.la"

cat << EOF > dep-helper.c
/* non-static helper function that isn't exported */
const char *helper(void)
{
	return "module1";
}
EOF

cat << EOF > dep.c
const char *helper(void);
const char *dep(void)
{
	return helper();
}
EOF

cat << EOF > dep.expsym
dep
EOF

# Then, the source for module1.la depending on "libdep.la".

cat << EOF > module1.c
const char *dep(void);
const char *module1_LTX_entry_point(void)
{
	return dep();
}
EOF

cat << EOF > module1.expsym
module1_LTX_entry_point
EOF

# Source for the simplistic module2.la w/o dependencies.

cat << EOF > module2.c
const char *module2_LTX_entry_point(void)
{
	return "module2";
}
EOF

cat << EOF > module2.expsym
module2_LTX_entry_point
EOF

# Source for libfoo.la, the module consumer.

cat << EOF > foo.c
#include <stdio.h>

#ifndef PIC
typedef const char *(module_entry)(void);
const char *module1_LTX_entry_point(void);
const char *module2_LTX_entry_point(void);
extern module_entry *module1 = module1_LTX_entry_point;
extern module_entry *module2 = module2_LTX_entry_point;
#endif

void foo_init(void);
void foo_exit(void);
void foo_doit(void);

void foo_init(void)
{
	printf("foo_init\n");
}

void foo_exit(void)
{
	printf("foo_exit\n");
}

void foo_doit(void)
{
#ifdef PIC
	/* dlopen, LoadLibrary, use ltdl, whatever */
	printf("not interested in shared modules today\n");
#else
	printf("%s\n", module1());
	printf("%s\n", module2());
#endif
}
EOF

cat << EOF > foo.expsym
foo_init
foo_doit
foo_exit
EOF

# Source for a simple test program.

cat << EOF > bar.c
void foo_init(void);
void foo_exit(void);
void foo_doit(void);

int main(void)
{
	foo_init();
	foo_doit();
	foo_exit();
	return 0;
}
EOF



# Build dummy the dependency library libdep.la
$LIBTOOL --mode=compile --tag=CC $CC -c -o dep-helper.lo dep-helper.c
$LIBTOOL --mode=compile --tag=CC $CC -c -o dep.lo dep.c
$LIBTOOL --mode=link --tag=CC $CC -o libdep.la dep-helper.lo dep.lo \
 -rpath $instdir -export-symbols dep.expsym \
 -version-info 2:0:0 -no-undefined

# Install the dummy dependency library
mkdir $instdir
$LIBTOOL --mode=install --tag=CC cp libdep.la $instdir/libdep.la
# And remove some key files from the build, so that
# they are not found later.
rm -rf libdep.la .libs/libdep*


# Build module1.la
$LIBTOOL --mode=compile --tag=CC $CC -c -o module1.lo module1.c
$LIBTOOL --mode=link --tag=CC $CC -o module1.la module1.lo \
 -L$instdir -ldep \
 -rpath /usr/local/lib/example -export-symbols module1.expsym \
 -avoid-version -no-undefined -module

# Build module2.la
$LIBTOOL --mode=compile --tag=CC $CC -c -o module2.lo module2.c
$LIBTOOL --mode=link --tag=CC $CC -o module2.la module2.lo \
 -rpath /usr/local/lib/example -export-symbols module2.expsym \
 -avoid-version -no-undefined -module

# Build libfoo.la
$LIBTOOL --mode=compile --tag=CC $CC -c -o foo.lo foo.c
$LIBTOOL --mode=link --tag=CC $CC -o libfoo.la foo.lo \
 -rpath /usr/local/lib -export-symbols foo.expsym \
 -version-info 3:0:0 -no-undefined \
 -dlopen module1.la -dlopen module2.la

# Compile simple test program
$LIBTOOL --mode=compile --tag=CC $CC -c -o bar.lo bar.c

# Link a couple of different versions of the test program.
# First, a standard link, prefer shared libs across the board.
$LIBTOOL --mode=link --tag=CC $CC -o bar \
 bar.lo libfoo.la -dlopen self

# Now a static link, prefer static libs of the locally
# built libs (That's what -static means, right?)
# Linking with -static fails since symbols are extracted
# from the static version of libdep.la, but the link is
# later done against the shared version of libdep.la,
# which doesn't have the "helper" symbol. At least not on
# my platform (cygwin).
$LIBTOOL --mode=link --tag=CC $CC -o bar-static \
 bar.lo libfoo.la -dlopen self -static

# Only accept static libs.
$LIBTOOL --mode=link --tag=CC $CC -o bar-all-static \
 bar.lo libfoo.la -dlopen self -all-static

# Another variant of static only libs (in this case).
$LIBTOOL --mode=link --tag=CC $CC -o bar-static-libtool-libs \
 bar.lo libfoo.la -dlopen self -static-libtool-libs
