#! /bin/sh

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

set -e

# create directory layout
mkdir -p $DESTDIR/CD1/boot/$ARCH
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

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

  perl -e "print pack('N', -s '$DESTDIR/branding/$theme/CD1/boot/$ARCH/initrd')" >$DESTDIR/branding/$theme/CD1/boot/$ARCH/initrd.siz
  perl -e "print pack('N', 0x1000000)" >$DESTDIR/branding/$theme/CD1/boot/$ARCH/initrd.off

  install -m 644 /boot/image-* $DESTDIR/branding/$theme/CD1/boot/$ARCH/linux

  install -m 644 data/initrd/s390/suse.ins $DESTDIR/branding/$theme/CD1/suse.ins
  perl -pi -e "s#^([^*]+)#boot/$ARCH/\$1#" $DESTDIR/branding/$theme/CD1/suse.ins
  install -m 644 data/initrd/s390/susehmc.ins $DESTDIR/branding/$theme/CD1/susehmc.ins
  install -m 644 data/initrd/s390/suse.ins $DESTDIR/branding/$theme/CD1/boot/$ARCH/suse.ins

  install -m 644 data/initrd/s390/parmfile $DESTDIR/branding/$theme/CD1/boot/$ARCH/parmfile
  install -m 644 data/initrd/s390/parmfile.hmc $DESTDIR/branding/$theme/CD1/boot/$ARCH/parmfile.hmc
  install -m 644 data/initrd/s390/parmfile.cd $DESTDIR/branding/$theme/CD1/boot/$ARCH/parmfile.cd

  install -m 644 data/initrd/s390/sles.exec $DESTDIR/branding/$theme/CD1/boot/$ARCH/sles.exec
  install -m 644 tmp/base/usr/share/doc/packages/s390-tools/zpxe.rexx $DESTDIR/branding/$theme/CD1/boot/$ARCH/zpxe.rexx

  # create cd.ikr and remove no longer needed parmfile.cd
  bin/mk_cd_ikr $DESTDIR/branding/$theme/CD1/boot/$ARCH/suse.ins
  rm $DESTDIR/branding/$theme/CD1/boot/$ARCH/parmfile.cd
done

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

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
