#!/bin/sh

. ./config.sh
LOKIDIR=$OUTDIR/loki

function xmlfiles
{
  BASE=$1
  DIR=$2
  cd $BASE/$DIR
  for a in `find -type f`; do
    a=`echo $a | sed -e 's/^.\///'`
    echo "      $DIR/$a" >> $SETUPXML
  done
  cd -
}

echo -n "Creating loki installer..."
mkdir -p $LOKIDIR
mkdir -p $LOKIDIR/setup.data
SETUPXML=$LOKIDIR/setup.data/setup.xml

cat > $SETUPXML << __END__
<?xml version="1.0" standalone="yes"?>
<install product="$PACKAGENAME" desc="$PACKAGEDESC" version="$PACKAGEVER" uninstall="uninstall.sh" path="/opt/planeshift" binarypath="/usr/bin" promptbinaries="yes">
  <eula>
    share/doc/planeshift/license.txt
  </eula>
__END__

for module in $MODULES; do
  # copy over module data
  cp -rP $OUTDIR/$module/* $LOKIDIR

  # read module settings
  . $module.list
  
  BASE=$OUTDIR/$module
  echo "  <option install=\"true\">" >> $SETUPXML
  echo "    $MODULENAME" >> $SETUPXML
  if test "$BINARIES"; then
    echo "    <binary>" >> $SETUPXML
    xmlfiles $BASE $BINDIR
    echo "    </binary>" >> $SETUPXML
    rm -rf $LOKIDIR/bin
    mkdir -p $LOKIDIR/bin/Linux/x86
    cp -rP $BASE/$BINDIR $LOKIDIR/bin/Linux/x86
  fi
  echo "    <files>" >> $SETUPXML
  xmlfiles $BASE $CONFIGDIR
  xmlfiles $BASE $PLUGINDIR
  xmlfiles $BASE $DOCDIR
  xmlfiles $BASE $DATADIR
  echo "    </files>" >> $SETUPXML
  echo "  </option>" >> $SETUPXML
done
echo "</install>" >> $SETUPXML

cp -p setup.data/* $LOKIDIR/setup.data >& /dev/null

mkdir -p $LOKIDIR/setup.data/bin/Linux/x86
LOKIEXES="uninstall setup.gtk setup"
for l in $LOKIEXES; do
  cp -p $LOKIFILES/$l $LOKIDIR/setup.data/bin/Linux/x86
done
cp -p $LOKIFILES/image/setup.sh $LOKIDIR

echo "ok"
