#!/bin/sh

# The following source parts of xine-lib may not be distributed for legal
# reasons.
# Thus we have to cripple the source tree in a way that it still builds.

#
# Helpers
#

# $1: files $2: entries $3: prefix $4: postfix
do_nukeentry() {
for d in $1 ; do
  perl -i -e 'undef $/; $_=<>; for $e (qw|'"$2"'|) { s|(?<=[^-a-zA-Z0-9_./])'"$3"'$e'"$4"'(?=[^-a-zA-Z0-9_./])||g }; print' $d
done
}

# $1: files $2: entries $3: prefix $4: postfix
do_nukeline() {
for d in $1 ; do
  perl -i -e 'undef $/; $_=<>; for $e (qw|'"$2"'|) { s|^.*(?<=[^-a-zA-Z0-9_./])'"$3"'$e'"$4"'(?=[^-a-zA-Z0-9_./]).*$||mg }; print' $d
done
}

# $1: dir $2: files/dirs
do_remove() {
pushd "$1" >/dev/null || exit 1
rm -rf $2
popd >/dev/null
}

#
# Main
#

# Find source tarball + unpack

tmp="/`mktemp -d`"
trap 'rm -rf $tmp; echo 1>&2 "$0 failed"' EXIT
test -d "$tmp" -a -w "$tmp" || exit
tarsource="`echo xine-lib-*[0-9].tar.bz2`"
if [ ! -s "$tarsource" ] ; then
  echo "Error: cannot find source tarball"
  exit 1
fi
tarbase="${tarsource%.tar.bz2}"
tardest="$tarbase-crippled.tar.bz2"

rm -rf xine-lib-*-crippled.tar.bz2
echo 1>&2 "Unpacking..."
tar -C "$tmp" -xjf $tarsource || exit 1

pushd $tmp/$tarbase >/dev/null || exit 1

#
# Cripple source
#

echo 1>&2 "Crippling..."

c_subdirs="dxr3 liba52 libdts libfaad libffmpeg libmad libmpeg2 libspucc libspudec libspudvb libw32dll input/vcd libxineadec/gsm610 libxineadec/nosefart combined/ffmpeg"
c_demuxers="xineplug_dmx_asf.la asfheader.h asfheader.c demux_asf.c   xineplug_dmx_mpeg.la demux_mpeg.c   xineplug_dmx_mpeg_block.la demux_mpeg_block.c   xineplug_dmx_mpeg_ts.la demux_ts.c   xineplug_dmx_mpeg_elem.la demux_elem.c   xineplug_dmx_mpeg_pes.la demux_mpeg_pes.c   xineplug_dmx_yuv4mpeg2.la demux_yuv4mpeg2.c"
c_input="xineplug_inp_mms.la input_mms.c mms.c mmsh.c ../demuxers/asfheader.c mms.h mmsh.h   xineplug_inp_vcdo.la input_vcd.c   vcd"
c_libxineadec="xineplug_decode_gsm610.la xineplug_decode_nsf.la gsm610.c nsf.c  gsm610 nosefart"

cp src/libffmpeg/libavcodec/libpostproc/mangle.h src/post/deinterlace/plugins/

do_nukeentry src/Makefile.am             "$c_subdirs"
do_nukeentry configure.ac                "$c_subdirs" "src/" "/[a-zA-Z0-9_./]*Makefile"
do_remove    src                         "$c_subdirs"
do_nukeentry src/demuxers/Makefile.am    "$c_demuxers"
do_remove    src/demuxers                "$c_demuxers"
do_nukeentry src/input/Makefile.am       "$c_input"
do_remove    src/input                   "$c_input"
do_nukeentry src/libxineadec/Makefile.am "$c_libxineadec"
do_remove    src/libxineadec             "$c_libxineadec"
do_nukeentry src/combined/Makefile.am    "ffmpeg"
do_remove    src/combined                "ffmpeg"
do_nukeentry src/post/planar/Makefile.am "pp\.c \S*/libpostproc/libpostprocess\.la"
do_nukeline  src/post/planar/planar.c    "pp_init_plugin pp_special_info"
do_remove    src/post/planar             "pp.c"

echo 1>&2 "Autotools..."
autoreconf -fi || exit 1

#
# Create new (crippled) source tarball
#

popd >/dev/null

tar  --owner=root --group=root -C $tmp -cjf $tardest $tarbase || exit 1
rm -rf $tmp

echo 1>&2 ""
echo 1>&2 "Successfully crippled tarball. :-("
echo 1>&2 ""

rm -rf $tmp
trap - EXIT
exit 0
#EOF
