#!/bin/bash
set -e

source $(dirname $0)/version
cd $(dirname $0)/..

ARTIFACTS=$(pwd)/dist/artifacts
BUILD=build
INITRD_DIR=${BUILD}/initrd

echo Create initrd layout in $INITRD_DIR

rm -rf ${INITRD_DIR}
mkdir -p ${INITRD_DIR}/usr/{etc,bin,share/ros,var/lib/cni/bin}

./scripts/template

cp -rf assets/selinux          ${INITRD_DIR}/usr/etc
cp -rf assets/docker           ${INITRD_DIR}/usr/etc
cp build/images.tar            ${INITRD_DIR}/usr/share/ros/
cp bin/ros                     ${INITRD_DIR}/usr/bin/
ln -s usr/bin/ros              ${INITRD_DIR}/init
ln -s bin                      ${INITRD_DIR}/usr/sbin
ln -s usr/sbin                 ${INITRD_DIR}/sbin
ln -s ros                      ${INITRD_DIR}/usr/bin/system-docker
ln -s ros                      ${INITRD_DIR}/usr/bin/docker-runc
ln -s ../../../../usr/bin/ros  ${INITRD_DIR}/usr/var/lib/cni/bin/bridge
ln -s ../../../../usr/bin/ros  ${INITRD_DIR}/usr/var/lib/cni/bin/host-local

cat <<HERE > ${INITRD_DIR}/usr/share/ros/os-release
NAME="RancherOS"
VERSION=${VERSION}
ID=rancheros
ID_LIKE=
VERSION_ID=${VERSION}
PRETTY_NAME="RancherOS ${VERSION}"
HOME_URL="http://rancher.com/rancher-os/"
SUPPORT_URL="https://forums.rancher.com/c/rancher-os"
BUG_REPORT_URL="https://github.com/rancher/os/issues"
BUILD_ID=
HERE

# Support upgrades from old persistent consoles that bind mount these
touch ${INITRD_DIR}/usr/bin/docker-containerd
touch ${INITRD_DIR}/usr/bin/docker-containerd-shim
touch ${INITRD_DIR}/usr/bin/docker

# Override using a local kernel build
if [ -e ${DAPPER_SOURCE}/assets/kernel.tar.gz ]; then
    echo "copying ${DAPPER_SOURCE}/assets/kernel.tar.gz ${DOWNLOADS}/kernel.tar.gz"
    cp ${DAPPER_SOURCE}/assets/kernel.tar.gz ${DOWNLOADS}/kernel.tar.gz
fi

if [ -e ${DOWNLOADS}/kernel.tar.gz ]; then
    mkdir -p ${BUILD}/kernel
    tar xf ${DOWNLOADS}/kernel.tar.gz -C ${BUILD}/kernel

    for i in vmlinuz vmlinux; do
        if [ -e ${BUILD}/kernel/boot/${i}-* ]; then
            mkdir -p ${ARTIFACTS}
            # frustratingly, the vmlinuz versioned filename != the tag name, so we need to do some guessing
            # for eg, 4.9-rc8-rancher2 is called vmlinuz-4.9.0-rc8-rancher
            echo "Copy ${BUILD}/kernel/boot/${i}-* to ${ARTIFACTS}/vmlinuz-${KERNEL_VERSION}"
            cp ${BUILD}/kernel/boot/${i}-* ${ARTIFACTS}/vmlinuz-${KERNEL_VERSION}
            # use an unversioned filename for `scripts/run`
            cp ${BUILD}/kernel/boot/${i}-* ${BUILD}/kernel/vmlinuz
            break
        fi
    done

    # TODO: move these into a separate tar.gz and add to the syslinux initrd line
    if [ -d ${BUILD}/kernel/lib ]; then
        rm -rf ${INITRD_DIR}/usr/lib
        cp -rf ${BUILD}/kernel/lib ${INITRD_DIR}/usr/
        depmod -b ${INITRD_DIR}/usr $(basename ${INITRD_DIR}/usr/lib/modules/*)

        #TODO:
        # new: put the kernel modules into their own initrd file
        #mkdir -p ${BUILD}/kernel-fs/usr/
        #pushd .
        #cp -rf ${BUILD}/kernel/lib ${BUILD}/kernel-fs/usr/
        #depmod -b ${BUILD}/kernel-fs/usr $(basename ${BUILD}/kernel-fs/usr/lib/modules/*)
        ## and then package it up cpio
        #cd ${BUILD}/kernel-fs/
        #echo Creating kernel ${ARTIFACTS}/linuxmods-${KERNEL_VERSION}
        
        #if [ "$COMPRESS" == "" ]; then
        #    COMPRESS="gzip -1"
        #fi
        #find | cpio -H newc -o | ${COMPRESS} > ${ARTIFACTS}/linuxmods-${KERNEL_VERSION}
        #popd
        #echo Done creating kernel ${ARTIFACTS}/linuxmods-${KERNEL_VERSION}
        ## use an unversioned filename for `scripts/run`
        #cp ${ARTIFACTS}/linuxmods-${KERNEL_VERSION} ${BUILD}/kernel/linuxmods
    fi
else
	if [ "$ARCH" == "amd64" ]; then
		echo "no ${DOWNLOADS}/kernel.tar.gz found"
		exit 1
	fi
fi

if [ "$ARCH" == "amd64" ]; then
	ls -lah ${ARTIFACTS}/vmlinuz-*
	if [ ! -e "${ARTIFACTS}/vmlinuz-${KERNEL_VERSION}" ]; then
		echo "Can't find ${ARTIFACTS}/vmlinuz-${KERNEL_VERSION}"
		exit -1
	fi
fi

if [ -e ${DOWNLOADS}/policy.29 ]; then
    mkdir -p ${INITRD_DIR}/usr/etc/selinux/ros/policy/
    cp ${DOWNLOADS}/policy.29 ${INITRD_DIR}/usr/etc/selinux/ros/policy/
fi

DFS_ARCH=$(docker create ${DFS_IMAGE}${SUFFIX})
trap "docker rm -fv ${DFS_ARCH} >/dev/null" EXIT

docker export ${DFS_ARCH} | tar xf - -C ${INITRD_DIR} --exclude=usr/bin/docker*      \
                                                      --exclude=usr/share/git-core   \
                                                      --exclude=usr/bin/git          \
                                                      --exclude=usr/bin/ssh          \
                                                      --exclude=usr/libexec/git-core \
                                                      usr

./scripts/hash-initrd
