#! /bin/sh

# gets ARCH, THEMES, DESTDIR, INSTSYS_PARTS, BOOT_PARTS, WITH_FLOPPY via env

set -e
shopt -s extglob

# create directory layout
mkdir -p $DESTDIR/CD1/boot/$ARCH/loader
for theme in $THEMES ; do
  mkdir -p $DESTDIR/branding/$theme/CD1/boot/$ARCH
done

# copy branding independent parts
for i in $INSTSYS_PARTS $BOOT_PARTS ; do
  [ -e images/$i ] && cp -r images/$i $DESTDIR/CD1/boot/$ARCH
done

# xen kernel
for i in images/vmlinuz-+([^.]) ; do
  [ -d "$i" ] && cp "$i/linux" $DESTDIR/CD1/boot/$ARCH/`basename $i`
done

# biostest goes to 'loader' dir
if [ -e $DESTDIR/CD1/boot/$ARCH/biostest ] ; then
  mv $DESTDIR/CD1/boot/$ARCH/biostest $DESTDIR/CD1/boot/$ARCH/loader/biostest
fi

# copy branding dependent parts
for theme in $THEMES ; do
  for i in $INSTSYS_PARTS $BOOT_PARTS ; do
    for ii in images/$theme/$i ; do
      [ -e $ii ] && cp -r $ii $DESTDIR/branding/$theme/CD1/boot/$ARCH
    done
  done

  if [ -d images/$theme/EFI ] ; then
    cp -r images/$theme/EFI/EFI $DESTDIR/branding/$theme/CD1
  fi

  # xen initrd
  for i in images/$theme/initrd-+([^.]) ; do
    [ -f "$i" ] && cp $i $DESTDIR/branding/$theme/CD1/boot/$ARCH
  done

  # might be missing if theme does not really exist
  mkdir -p $DESTDIR/branding/$theme/CD1/boot/$ARCH/loader

  # initrd goes to 'loader' dir
  dir=$DESTDIR/branding/$theme/CD1/boot/$ARCH
  if [ -e $dir/initrd ] ; then
    mv $dir/initrd $dir/loader/initrd
    # check for syslinux 6.x setup
    if [ ! -e $dir/loader/gfxboot.cfg ] ; then
      tmp/base/usr/sbin/gfxboot -a $dir/loader/bootlogo --change-config install::initrd.size=$(stat -c "%s" $dir/loader/initrd)
    fi
  fi

  # cp etc/README $DESTDIR/branding/$theme/CD1/boot/$ARCH
  # recode utf8..ibmpc <$DESTDIR/branding/$theme/CD1/boot/$ARCH/README >$DESTDIR/branding/$theme/CD1/boot/$ARCH/README.DOS
  # cp etc/LIESMICH $DESTDIR/branding/$theme/CD1/boot/$ARCH
  # recode utf8..ibmpc <$DESTDIR/branding/$theme/CD1/boot/$ARCH/LIESMICH >$DESTDIR/branding/$theme/CD1/boot/$ARCH/LIESMICH.DOS

  # create boot floppies and put them in ftp tree
  if [ "$WITH_FLOPPY" = 1 ] ; then
    mkdir -p $DESTDIR/branding/$theme/FTP/boot/$ARCH/floppy
    rm -rf tmp/floppy-$theme
    mkdir -p tmp/floppy-$theme
    cp -a $DESTDIR/CD1/boot/$ARCH/loader/* $DESTDIR/branding/openSUSE/CD1/boot/$ARCH/loader/* tmp/floppy-$theme
    $DESTDIR/branding/$theme/CD1/boot/$ARCH/mkbootdisk --syslinux=tmp/base/usr/bin/syslinux --out=$DESTDIR/branding/$theme/FTP/boot/$ARCH/floppy/bootdisk tmp/floppy-$theme
  fi

done

mkdir -p $DESTDIR/usr/share
cp images/mini-iso-rmlist $DESTDIR/usr/share

if [ -f images/rescue-server ] ; then
  dst=$DESTDIR/usr/share/rescue-server/boot/x86_64
  mkdir -p $dst
  cp images/rescue-server $dst
  echo "rescue: rescue-server" >$dst/config
  cp -a $DESTDIR/CD1/boot/$ARCH/loader $DESTDIR/branding/$THEMES/CD1/boot/$ARCH/loader $dst
  cp data/boot/syslinux-rescue.cfg $dst/loader/isolinux.cfg
fi

if [ -f images/zenroot ] ; then
  dst=$DESTDIR/branding/Zen/CD1/boot/x86_64
  mkdir -p $dst
  cp images/zenroot $dst/root
  echo "root: root" >$dst/config
  cp -a $DESTDIR/CD1/boot/$ARCH/loader $DESTDIR/branding/$THEMES/CD1/boot/$ARCH/* $dst
  [ -d "$DESTDIR/branding/$THEMES/CD1/EFI" ] && cp -a $DESTDIR/branding/$THEMES/CD1/EFI $DESTDIR/branding/Zen/CD1
  cp data/boot/syslinux-zen.cfg $dst/loader/isolinux.cfg
  cp data/boot/message-zen $dst/loader/message
fi

if [ -d images/tftp ] ; then
  dst=$DESTDIR/usr/share/tftpboot-installation
  mkdir -p $dst
  cp -a images/tftp/* $dst
  cp -a $DESTDIR/CD1/* $dst/*
  cp -a $DESTDIR/branding/$THEMES/CD1/* $dst/*
fi

