#!/bin/sh

PXE_ROOT=/var/lib/tftpboot/X86PC/linux/pxelinux.cfg
MAC_TABLE=/var/lib/tftpboot/X86PC/conf_mac_profiles

log() {
    logger -t deployd $@
}

read MAC_ADDR
PXE_ADDR=01-${MAC_ADDR//:/-}
PXE_FILE=${PXE_ROOT}/${PXE_ADDR}

NEW_PROFILE=`awk --field-separator \| "!/^#/ { if (\\$1 == \"$MAC_ADDR\") print \\$3}" /var/lib/tftpboot/X86PC/conf_mac_profiles`

if [ -n "${NEW_PROFILE}" ]; then
    INST_LINK=`readlink ${PXE_FILE}`
    NEW_LINK=profiles/boot/${NEW_PROFILE}
    if [ ${INST_LINK} = ${NEW_LINK} ]; then
        log "${MAC_ADDR} was already installed"
    elif ln -sf ${NEW_LINK} ${PXE_FILE}; then
        killall -USR1 drakpelinux
        log "${MAC_ADDR} successfully installed"
    else
        log "unable to switch ${MAC_ADDR} to boot profile"
    fi
else
    log "failed to locate configuration for ${MAC_ADDR}"
fi
