#!/bin/bash

DI_DIR=$1
MIRROR=$2

if [ ! -d $1/build/boot/ ] || [ ! -d $2 ] ; then
    echo "Usage: $0 DI_DIR MIRROR" > /dev/stderr
    exit 1
fi

# Configurable stuff
DI_DIST=sid
ARCH=arm64
# End of Configurable stuff

debmirror --keyring /usr/share/keyrings/debian-archive-keyring.gpg --rsync-extra=doc --verbose --progress --getcontents \
  --nosource --method=http --host=ftp.uk.debian.org --root=:debian --arch=$ARCH \
  --dist=$DI_DIST --section=main,contrib,non-free-firmware,main/debian-installer \
  --di-dist=dists --di-arch=arches --diff=use --exclude='cross|dbg' $MIRROR

cd $DI_DIR/build/

fakeroot make all_clean build_cdrom_grub build_cdrom_gtk

DEBIAN_CD=$(mktemp -d)

cd $DEBIAN_CD
apt-get source debian-cd
cd `find . -type d | awk 'NR==2'`

echo 'export COPYLINK=1' >> CONF.sh
echo "export MIRROR=${MIRROR}" >> CONF.sh
echo 'export TDIR=/tmp/debian-cd/tmp' >> CONF.sh
echo 'export OUT=/tmp/debian-cd/out' >> CONF.sh
echo 'export APTTMP=/tmp/debian-cd/apttmp' >> CONF.sh
echo "export DI_DIST=${DI_DIST}" >> CONF.sh
echo "export CODENAME=${DI_DIST}" >> CONF.sh
echo "export DI_CODENAME=${DI_DIST}" >> CONF.sh

# Replacement of easy-build.sh
echo "export MAXISOS=1" >> CONF.sh
echo "export MAXJIGDOS=0" >> CONF.sh
echo "export INSTALLER_CD=2" >> CONF.sh # NETINST

time ./build.sh $ARCH
