#!/bin/sh

# compile_xorriso.sh
# Copyright 2005 - 2010 Thomas Schmitt, scdbackup@gmx.net, GPL version 2
#
# Not intended for general use in production installations !
# Rather use:  ./bootstrap ; ./configure ; make
#
# This is a development tool which expects a special setup of directories
# on a Linux system (e.g. SuSE 10.2).
# It is to be executed in a common parent of the directories given with
#   $isofs $isoburn $burn $xorr

isofs=./nglibisofs-develop/libisofs/libisofs_libisofs_la-
isofs_filter=./nglibisofs-develop/libisofs/filters/libisofs_libisofs_la-
burn=./libburn-develop/libburn
isoburn=./libisoburn-develop/libisoburn
xorr=./libisoburn-develop/xorriso

debug_opts="-O2"
def_opts="-DXorriso_allow_external_filterS"
largefile_opts="-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE=1"
do_strip=0
static_opts=
warn_opts="-Wall"
nglibisofs=1
def_libreadline="-DXorriso_with_readlinE"
link_libreadline="-lreadline"
link_libcdio=

for i in "$@"
do
  if test "$i" = "-do_diet"
  then
    def_opts="$def_opts -DXorriso_no_helP"
    warn_opts=
  elif test "$i" = "-do_strip"
  then
    do_strip=1
  elif test "$i" = "-g"
  then
    debug_opts="-g -O0"
  elif test "$i" = "-no_libreadline"
  then
    def_libreadline=""
    link_libreadline=""
  elif test "$i" = "-dvd_obs_64k"
  then
    def_opts="$def_opts -DXorriso_dvd_obs_default_64K"
  elif test "$i" = "-use_libcdio"
  then
    link_libcdio="-lcdio"
  elif test "$i" = "-help" -o "$i" = "--help" -o "$i" = "-h"
  then
    echo \
        "$xorr/compile_xorriso.sh : to be executed above top level directories"
    echo "Options:"
    echo "  -dvd_obs_64k      64 KB default size for DVD/BD writing."
    echo "  -no_libreadline   do not compile for and link with libreadline."
    echo "  -use_libcdio      link with -lcdio because libburn uses it."
    echo "  -do_diet          produce capability reduced lean version."
    echo "  -do_strip         apply program strip to compiled programs."
    echo "  -g                produce debuggable programm."
    echo "  -static           compile with cc option -static."
    exit 0
  elif test "$i" = "-static"
  then
    static_opts="-static"
  fi
done

libisofs=
libisofs="$libisofs $isofs"buffer.o
libisofs="$libisofs $isofs"builder.o
libisofs="$libisofs $isofs"data_source.o
libisofs="$libisofs $isofs"ecma119.o
libisofs="$libisofs $isofs"ecma119_tree.o
libisofs="$libisofs $isofs"eltorito.o
libisofs="$libisofs $isofs"filesrc.o
libisofs="$libisofs $isofs"fs_image.o
libisofs="$libisofs $isofs"fs_local.o
libisofs="$libisofs $isofs"fsource.o
libisofs="$libisofs $isofs"image.o
libisofs="$libisofs $isofs"iso1999.o
libisofs="$libisofs $isofs"joliet.o
libisofs="$libisofs $isofs"libiso_msgs.o
libisofs="$libisofs $isofs"messages.o
libisofs="$libisofs $isofs"node.o
libisofs="$libisofs $isofs"rockridge.o
libisofs="$libisofs $isofs"rockridge_read.o
libisofs="$libisofs $isofs"stream.o
libisofs="$libisofs $isofs"tree.o
libisofs="$libisofs $isofs"util.o
libisofs="$libisofs $isofs"util_htable.o
libisofs="$libisofs $isofs"util_rbtree.o
libisofs="$libisofs $isofs"system_area.o
libisofs="$libisofs $isofs"make_isohybrid_mbr.o
libisofs="$libisofs $isofs"aaip_0_2.o
libisofs="$libisofs $isofs"filter.o
libisofs="$libisofs $isofs_filter"external.o
libisofs="$libisofs $isofs_filter"zisofs.o
libisofs="$libisofs $isofs_filter"gzip.o
libisofs="$libisofs $isofs"md5.o

echo "Version timestamp :  $(sed -e 's/#define Xorriso_timestamP "//' -e 's/"$//' "$xorr"/xorriso_timestamp.h)"

date -u '+#define Xorriso_build_timestamP "%Y.%m.%d.%H%M%S"' >"$xorr"/xorriso_buildstamp.h
echo "Build timestamp   :  $(sed -e 's/#define Xorriso_build_timestamP "//' -e 's/"$//' "$xorr"/xorriso_buildstamp.h)"

echo "compiling program $xorr/xorriso.c $static_opts $debug_opts $def_opts $link_libcdio"
cc -I. -DXorriso_with_maiN $def_libreadline \
  $warn_opts \
  $static_opts \
  $debug_opts \
  $def_opts \
  $largefile_opts \
  \
  -o "$xorr"/xorriso \
  \
  "$xorr"/xorriso_main.c \
  "$xorr"/sfile.c \
  "$xorr"/aux_objects.c \
  "$xorr"/findjob.c \
  "$xorr"/check_media.c \
  "$xorr"/misc_funct.c \
  "$xorr"/text_io.c \
  "$xorr"/match.c \
  "$xorr"/emulators.c \
  "$xorr"/disk_ops.c \
  "$xorr"/cmp_update.c \
  "$xorr"/parse_exec.c \
  "$xorr"/opts_a_c.c \
  "$xorr"/opts_d_h.c \
  "$xorr"/opts_i_o.c \
  "$xorr"/opts_p_z.c \
  \
  "$xorr"/base_obj.c \
  "$xorr"/lib_mgt.c \
  "$xorr"/sort_cmp.c \
  "$xorr"/drive_mgt.c \
  "$xorr"/iso_img.c \
  "$xorr"/iso_tree.c \
  "$xorr"/iso_manip.c \
  "$xorr"/write_run.c \
  "$xorr"/read_run.c \
  "$xorr"/filters.c \
  \
  "$isoburn"/isoburn.c \
  "$isoburn"/burn_wrap.c \
  "$isoburn"/data_source.c \
  "$isoburn"/isofs_wrap.c \
  \
  "$burn"/async.o \
  "$burn"/debug.o \
  "$burn"/drive.o \
  "$burn"/file.o \
  "$burn"/init.o \
  "$burn"/options.o \
  "$burn"/source.o \
  "$burn"/structure.o \
  \
  "$burn"/sg.o \
  "$burn"/write.o \
  "$burn"/read.o \
  "$burn"/libdax_audioxtr.o \
  "$burn"/libdax_msgs.o \
  "$burn"/cleanup.o \
  \
  "$burn"/mmc.o \
  "$burn"/sbc.o \
  "$burn"/spc.o \
  "$burn"/util.o \
  \
  "$burn"/sector.o \
  "$burn"/toc.o \
  \
  "$burn"/crc.o \
  "$burn"/ecma130ab.o \
  \
  $libisofs \
  \
  $link_libreadline \
  $link_libcdio \
  \
  -lpthread \
  -lacl \
  -lz

  ret=$?
  if test "$ret" = 0
  then
    dummy=dummy
  else
    echo >&2
    echo "+++ FATAL : Compilation of xorriso failed" >&2
    echo >&2
    exit 1
  fi


if test "$do_strip" = 1
then
  echo "stripping result $xorr/xorriso"
  strip "$xorr"/xorriso
fi

echo 'done.'