#!/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 (or cdrskin) development.
#

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

# libburn source used:  http://libburnia.pykix.org
# Downloaded by:
#  $ svn co http://libburnia-svn.pykix.org/libburn/tags/... $intermediate
# packed up in a tarball just to save it from inadverted changes by
#  $ tar czf libburn_svn.tgz $intermediate
original="./libburn_svn_release.tgz"
# Historic moments:
# original="./libburn_svn_A60815.tgz"
# original="./libburn_cdrskin_A60819.tgz"


# My changes are in $changes , mainly in $changes/cdrskin
changes="./libburn-release"

skin_release="1.5.4"
patch_level=""
# patch_level=".pl00"
skin_rev="$skin_release""$patch_level"

# The result directory and the name of the result tarballs
target="./cdrskin-${skin_release}"
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"

man_to_html_cmd="./cdrskin/convert_man_to_html.sh"
man_page_html="cdrskin/man_1_cdrskin.html"

# bintarget_dynamic="cdrskin_${skin_rev}-x86-suse9_0"
# bintarget_dynamic="cdrskin_${skin_rev}-amd64-suse10_2"
bintarget_dynamic="cdrskin_${skin_rev}-amd64-debian8_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 vim.swp and binaries
rm "$cdrskin_target"/.*.swp
rm "$cdrskin_target"/.*.swo
rm "$cdrskin_target"/*.o
rm "$cdrskin_target"/cdrfifo
rm "$cdrskin_target"/cdrskin
rm "$cdrskin_target"/cleanup
for i in std new make old
do
  if test -e "$cdrskin_target"/cdrskin_"$i"
  then
    rm "$cdrskin_target"/cdrskin_"$i"
  fi
done

# Remove eventual SVN stuff from cdrskin directory
for i in .deps .dirstamp .libs
do
  if test -e "$cdrskin_target"/"$i"
  then
    rm -rf "$cdrskin_target"/"$i"
  fi
done

# Remove GIFs of cdrskin_eng.html
rm "$cdrskin_target"/doener_*.gif "$cdrskin_target"/doener_*.png

# Remove automatically generated HTML man page
rm "$cdrskin_target"/man_1_cdrskin.html

# Remove all add_ts_changes_to_libburn besides this one
for i in "$cdrskin_target"/add_ts_changes_to_libburn*
do
  if test $(basename "$0") = $(basename "$i")
  then
    dummy=dummy
  else
    rm $i
  fi
done

# Remove libcevap
rm -rf "$target"/libcevap


# 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


# Make SVN state tarball for the libburn team
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 )

# Remove unwanted stuff after bootstrap
for i in "$target"/autom4te.cache
do
  if echo "$i" | grep '\*' >/dev/null
  then
    dummy=dummy
  else
    if test -e "$i"
    then
      rm -rf "$i"
    fi
  fi
done

# Repair non-portable shell code output of ./bootstrap
(
 cd "$compile_dir" || exit 1
 sed -e 's/^for ac_header in$/test -z 1 \&\& for ac_header in dummy/' \
     < ./configure > ./configure-repaired
 if test "$?" = 0
 then
   echo "$0: Empty 'for ac_header in' found in configure." >&2
 fi
 mv ./configure-repaired ./configure
 chmod a+rx,go-w,u+w ./configure
)

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

# Produce a static and a dynamic binary, and a HTML man page
(
 cd "$compile_dir" || exit 1
 ./configure
 make
 "$compile_cmd" -O2 -do_strip
 cp "$compile_result" "../$bintarget_dynamic"
 if test -n "$compile_static_opts"
 then
   "$compile_cmd" $compile_static_opts -O2 -do_strip
   cp "$compile_result" "../$bintarget_static"
 fi
 ( cd cdrskin ; cc -g -Wall -o unite_html_b_line unite_html_b_line.c )
 "$man_to_html_cmd"
 mv "$man_page_html" ..
)

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

# Show the result
./"$bintarget_dynamic" -version
./"$bintarget_static" -version
ls -l "$cdrskin_tarball"
ls -l "$bintarget_dynamic"
ls -l "$bintarget_static"
ls -l $(basename "$man_page_html")

