#! /bin/bash

# What to build...
MACHINES=("zynq-generic" "zynqmp-generic" "versal-generic")
SOC_VARIANTS=("7zs 7z" "cg eg ev dr" "s80")

TARGET_IMAGES="petalinux-image-minimal petalinux-image-full"

TARGET_CREATEREPO="meta-createrepo"

# Figure out where we are run from, to find this layer
thisLayer=$(realpath $(dirname $(dirname $0)))

# Make sure this layer is available in the current project
echo "Adding $thisLayer to the project"
bitbake-layers add-layer $thisLayer

# We are going to pass SOC_VARIANT and TMPDIR via the environment
export BB_ENV_EXTRAWHITE="${BB_ENV_EXTRAWHITE} SOC_VARIANT TMPDIR"

echo Starting build...
for i in ${!MACHINES[@]}; do
    MACHINE=${MACHINES[$i]}
    SOCS=${SOC_VARIANTS[$i]}

    echo "Building for ${MACHINE} variants ${SOCS}"

    for SOC_VARIANT in $SOCS; do
        TMPDIR="$(pwd)/${MACHINE}${SOC_VARIANT}"
        mkdir -p ${TMPDIR}
        TMPDIR=$(realpath $TMPDIR)

        echo "Building ${MACHINE}_${SOC_VARIANT}"

        TMPDIR=${TMPDIR} SOC_VARIANT=${SOC_VARIANT} MACHINE=${MACHINE} DISTRO=petalinux-binary bitbake ${TARGET_CREATEREPO} ${TARGET_IMAGES}
        TMPDIR=${TMPDIR} SOC_VARIANT=${SOC_VARIANT} MACHINE=${MACHINE} DISTRO=petalinux-binary bitbake ${TARGET_IMAGES} -c populate_sdk_ext

        # We need to clear the cache before we can export the PR values
        eval $( TMPDIR=${TMPDIR} SOC_VARIANT=${SOC_VARIANT} MACHINE=${MACHINE} DISTRO=petalinux-binary bitbake -e | grep ^CACHE= )
        if [ "x${CACHE}" != "x" ]; then
            rm -rf ${CACHE}
        fi

        TMPDIR=${TMPDIR} SOC_VARIANT=${SOC_VARIANT} MACHINE=${MACHINE} DISTRO=petalinux-binary bitbake -R conf/prexport.conf -p

        eval $( TMPDIR=${TMPDIR} SOC_VARIANT=${SOC_VARIANT} MACHINE=${MACHINE} DISTRO=petalinux-binary bitbake -R conf/prexport.conf -e | grep ^PRSERV_DUMPFILE= )

        mkdir -p tmp/deploy/prserv
        cp ${PRSERV_DUMPFILE} tmp/deploy/prserv/${MACHINE}_${SOC_VARIANT}.conf


        # Now process the arches for the repository configuration
        mkdir -p tmp/deploy/rpm/repos

        cat > tmp/deploy/rpm/repos/${MACHINE}_${SOC_VARIANT}.repo < /dev/null

        ARCHES=$(TMPDIR=${TMPDIR} SOC_VARIANT=${SOC_VARIANT} MACHINE=${MACHINE} DISTRO=petalinux-binary bitbake -e | grep ^ALL_MULTILIB_PACKAGE_ARCHS= | sed 's,-,_,g')
        if [ -z "$ARCHES" ]; then
            echo "ERROR: No architectures defined in machine ${MACHINE} variant ${SOC_VARIANT}" >&2
            continue
        fi
        eval $ARCHES

        # This file should already exist on the target, but we update it just in case.
        # This may be useful if the user is moving from one system to another...
        echo $ALL_MULTILIB_PACKAGE_ARCHS | tr ' ' '\n' | grep -v ^any$ | grep -v ^all$ | grep -v ^noarch$ | tac | tr '\n' ':'  | sed 's,:$,,' > tmp/deploy/rpm/repos/${MACHINE}_${SOC_VARIANT}.arch

        for arch in ${ALL_MULTILIB_PACKAGE_ARCHS}; do
            echo "Looking for ${TMPDIR}/deploy/rpm/$arch"
            if [ ! -d ${TMPDIR}/deploy/rpm/$arch ]; then
                # Look for exceptions!
                if [ ${arch##zynq} = ${arch} -a ${arch##versal} = ${arch} ]; then
                    continue
                fi
            fi

            echo "Generating Repository configuration $arch"

            cat >> tmp/deploy/rpm/repos/${MACHINE}_${SOC_VARIANT}.repo << EOF
[$arch]
name=$arch Packages for PetaLinux
baseurl=http://xsjmhatle50.xilinx.com/petalinux/rpm/$arch/
enabled=1
gpgcheck=0

EOF

            echo "Copying RPMS $arch..."
            if [ -d ${TMPDIR}/deploy/rpm/$arch ]; then
                rsync -S --ignore-existing -i -I -r ${TMPDIR}/deploy/rpm/$arch tmp/deploy/rpm
            else
                mkdir -p tmp/deploy/rpm/$arch
            fi
        done

        echo "Copying wic images..."
	mkdir -p tmp/deploy/images/${MACHINE}_${SOC_VARIANT}

	rsync -S --ignore-existing -i -I -l -r ${TMPDIR}/deploy/images/*/*wic* tmp/deploy/images/${MACHINE}_${SOC_VARIANT}/.

    done
done

# Extract the createrepo
echo "Extracting the createrepo_c tool"

createrepo_dir=$(date +%Y%M%d%H%M%S).$$
${TMPDIR}/deploy/sdk/x86_64-createrepo-nativesdk-standalone-2020.1.sh -d ${createrepo_dir} -y

for arch in tmp/deploy/rpm/* ; do
    if [ $(basename $arch) = "repos" ]; then
        continue
    fi

    echo "Generating index for $(basename $arch)"
    (
        . ${createrepo_dir}/environment-setup-x86_64-petalinux-linux
        cd $(dirname $arch)
        createrepo_c $(basename $arch)
    )
done
