#!/usr/bin/bash
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh

set -e

UNIT_DIR="${1:-/tmp}"

cmdline=( $(</proc/cmdline) )
cmdline_arg() {
    local name="$1" value="$2"
    for arg in "${cmdline[@]}"; do
        if [[ "${arg%%=*}" == "${name}" ]]; then
            value="${arg#*=}"
        fi
    done
    echo "${value}"
}

cmdline_bool() {
    local value=$(cmdline_arg "$@")
    case "$value" in
        ""|0|no|off) return 1;;
        *) return 0;;
    esac
}

add_requires() {
    local name="$1"; shift
    local target="${1:-initrd.target}"
    local requires_dir="${UNIT_DIR}/${target}.requires"
    mkdir -p "${requires_dir}"
    ln -sf "../${name}" "${requires_dir}/${name}"
}

# This can't be done with ConditionKernelCommandLine because that always
# starts the unit's dependencies. We want to start networkd only on first
# boot.
if $(cmdline_bool 'coreos.firstboot' 0); then
#   add_requires ignition-disks.service
    # we want this to run before initrd-switch-root.service, not initrd.target,
    # because it needs to run after ostree-prepare-root.service.
    add_requires ignition-files.service initrd-switch-root.service
    add_requires ignition-ask-var-mount.service initrd-switch-root.service
    #if [[ $(cmdline_arg coreos.oem.id) == "packet" ]]; then
    #    add_requires coreos-static-network.service
    #fi

    # On EC2, shut down systemd-networkd if ignition fails so that the instance
    # fails EC2 instance checks.
    #if [[ $(cmdline_arg coreos.oem.id) == "ec2" ]]; then
    #    mkdir -p ${UNIT_DIR}/systemd-networkd.service.d
    #    cat > ${UNIT_DIR}/systemd-networkd.service.d/10-conflict-emergency.conf <<EOF
#[Unit]
#Conflicts=emergency.target
#Conflicts=emergency.service
#Conflicts=dracut-emergency.service
#EOF
    #fi
fi

# Write ignition-setup.service customized for PXE/ISO or regular boot
#pxe=
#nopxe=
#usr=$(cmdline_arg mount.usr "$(cmdline_arg usr)")
#if [[ -z "${usr}" && -f /usr.squashfs ]]; then
#    # PXE-booted system, with or without persistent root
#    # (see 10diskless-generator)
#    pxe=1
#else
#    nopxe=1
#fi
#cat > ${UNIT_DIR}/ignition-setup.service <<EOF
#[Unit]
#Description=Ignition (setup)
#DefaultDependencies=false
#
#Requires=local-fs-pre.target
#Before=local-fs-pre.target
#
## pull in OEM device if it should exist
#${nopxe:+Requires=dev-disk-by\x2dlabel-OEM.device
#After=dev-disk-by\x2dlabel-OEM.device}
#
#[Service]
#Type=oneshot
#ExecStart=/usr/sbin/ignition-setup ${nopxe:+normal} ${pxe:+pxe}
#EOF

RANDOMIZE_DISK_GUID=$(cmdline_arg coreos.randomize_disk_guid)
if [ -n "$RANDOMIZE_DISK_GUID" ]; then
    escaped_guid=$(systemd-escape --path "/dev/disk/by-diskuuid/$RANDOMIZE_DISK_GUID")
    add_requires "disk-uuid@${escaped_guid}.service"
fi

if [[ $(cmdline_arg coreos.oem.id) == "digitalocean" ]]; then
    add_requires coreos-digitalocean-network.service
fi

oem_id=pxe
if [[ $(systemd-detect-virt || true) =~ ^(kvm|qemu)$ ]]; then
    oem_id=qemu
fi

echo "OEM_ID=$(cmdline_arg coreos.oem.id ${oem_id})" > /run/ignition.env
