#!/bin/sh

set -x

# This script documents how this cdrskin version was derived from
# a vanilla libburn version. It is not intended nor needed for any
# use of cdrskin but included here only to show the technical 
# relationship between both projects - which are close friends
# and issue roughly the same software.
#
# Package maintainers are advised to cover rather libburn than
# cdrskin unless they put only emphasis on the cdrecord emulation
# provided by cdrskin. libburn contains cdrskin - cdrskin is an
# oscillating, friendly and coordinated fork of libburn.
# 
# Script results are a source tarball and two binaries
# one dynamic and one static in respect to system libs.
# Both binaries are static in respect to libburn.
#
# The script is to be run in the directory above the toplevel
# directory of libburn resp. cdrskin development.
#
# libburn version used:  http://libburn.pykix.org SVN of Aug 15 2006
# packed up in a tarball just to save it from inadverted changes.
# original="./libburn_svn_A60815.tgz"
original="./libburn_cdrskin_A60819.tgz"

# The top level directory in that snapshot is named
intermediate="./libburn_pykix"

# My changes are in libburn-0.2.1.ts.develop , mainly in ./cdrskin

changes="./libburn-0.2.1.ts.develop"
skin_rev="0.1.5"

# The result directory and the name of the result tarballs
target="./cdrskin-${skin_rev}"
cdrskin_tarball="./cdrskin-${skin_rev}.tar.gz"
cdrskin_tarball_svn="./cdrskin-${skin_rev}.svn.tar.gz"

# (This once earned me an embarrassingly blooping source tarball)
# compile_dir="$changes"

compile_dir="$target"
compile_cmd="./cdrskin/compile_cdrskin.sh"
compile_static_opts="-static"
compile_result="cdrskin/cdrskin"


# addresses relative to compile_dir :
bintarget_dynamic="../cdrskin_${skin_rev}-x86-suse9_0"
bintarget_static="$bintarget_dynamic"-static

if test -d "$changes"
then
  dummy=dummy
else
  echo "$0 : FATAL : no directory $changes" >&2
  exit 1
fi

for i in "$target" "$intermediate"
do
  if test -e "$i"
  then
    echo "$0 : FATAL : already existing $i" >&2
    exit 2
  fi
done

if test -f "$original"
then
 dummy=dummy
else
  echo "$0 : FATAL : no file $original" >&2
  exit 3
fi


# Unpack SVN snapshot.
tar xzf "$original"


# Rename the directory to the cdrskin name
mv "$intermediate" "$target"


# Copy the changes from the development tree
# 
cdrskin_dir="$changes"/cdrskin
libburn_dir="$changes"/libburn
cdrskin_target="$target"/cdrskin
libburn_target="$target"/libburn

# Create version timestamp
timestamp="$(date -u '+%Y.%m.%d.%H%M%S')"
echo "$timestamp"
echo '#define Cdrskin_timestamP "'"$timestamp"'"' >"$cdrskin_dir"/cdrskin_timestamp.h

# Add the cdrskin files
if test -e "$cdrskin_target"
then
  rm -rf "$cdrskin_target"
fi
cp -a "$cdrskin_dir" "$cdrskin_target"

# Remove copied binaries
rm "$cdrskin_target"/cdrfifo
rm "$cdrskin_target"/cdrskin
rm "$cdrskin_target"/cleanup

# Remove unwanted SVN stuff (TODO: avoid downloading it)
for i in "$target"/.svn "$target"/*/.svn
do
  if test "$i" = "$target"'/*/.svn'
  then
    dummy=dummy
  else
    if test -e "$i"
    then
      rm -rf "$i"
    fi
  fi
done

# For now: Add own libburn-README in toplevel
cp -a "$changes"/README "$target"

# Add modified Makefile.am
cp -a "$changes"/Makefile.am "$target"


# Make SVN state tarball for the libburn team
# TODO: will probably be obsoleted after sucessful merge
tar czf "$cdrskin_tarball_svn" "$target"


# Get over dependecy on autotools. Rely only on cc, make et. al.
# This is not the same as "make dist" but i can do it without
# having to evaluate the quality of said "make dist"
#
( cd "$target" ; ./bootstrap )


# Pack it up to the new libburn+cdrskin-tarball
tar czf "$cdrskin_tarball" "$target"

# Produce a static and a dynamic binary
(
 cd "$compile_dir" || exit 1
 ./configure
 make
 $compile_cmd -do_strip
 cp "$compile_result" "$bintarget_dynamic"
 if test -n "$compile_static_opts"
 then
   $compile_cmd $compile_static_opts -do_strip
   cp "$compile_result" "$bintarget_static"
 fi
)

# Disable this for debugging the merge process
rm -rf "$target"


