#!/bin/bash
#
# (c) 2012 C Anthony Risinger <anthony@xtfx.me>
#
# MIT or BSD licensed ... do what you will.

# Find suitable python
! PYTHON=$(for p in python{2,2.{7,6,5}}; do type -p ${p} && break; done) &&
    echo "dude ... you need python2.x" &&
    exit 1

# Download test sources
for x in annotation.h annotation.c; do
    echo -n "Downloading (${x}) ... "
    wget --quiet -O ${x} "http://git.gnome.org/browse/gobject-introspection/plain/tests/scanner/${x}?id=GOBJECT_INTROSPECTION_1_30_0"
    echo "Done."
done

# Compile/link
echo -n "Compiling and linking (libannotation.so) ... "
gcc annotation.c $(pkg-config --cflags --libs gobject-2.0) -fPIC -shared -Wl,-soname,libannotation.so -o libannotation.so
echo "Done."

# Generate .gir
echo -n "Creating GIR file (g-ir-scanner) ... "
LD_RUN_PATH=$(pwd) g-ir-scanner --quiet --warn-all --nsversion=1.0 --include=GLib-2.0 --include=GObject-2.0 --library=annotation --namespace=Annotation --pkg-export=Annotation --output=Annotation-1.0.gir annotation.h annotation.c
echo "Done."

# Generate .typelib
echo -n "Creating TYPELIB file (g-ir-compiler) ... "
g-ir-compiler Annotation-1.0.gir > Annotation-1.0.typelib
echo "Done."

# SIGSEGV on read!
echo -e "\nShow me the SIGSEGV (on read)!!!"
LD_LIBRARY_PATH=$(pwd) GI_TYPELIB_PATH=$(pwd) "${PYTHON}" <<EOF
from gi.repository import Annotation as anno
hi = anno.Object()
print hi.props.function_property
EOF

# SIGABRT on write!
echo -e "Show me the SIGABRT (on write)!!!\n"
LD_LIBRARY_PATH=$(pwd) GI_TYPELIB_PATH=$(pwd) "${PYTHON}" <<EOF
from gi.repository import Annotation as anno
hi = anno.Object()
hi.props.function_property = lambda *x: x
EOF
