#!/bin/sh

# Import Cobbler profiles on first boot

url=http://download.fedora.redhat.com/pub/fedora/linux
ksdir=/var/www/cobbler/ks_mirror
rawhide=11

set -x
set -e
for dir in $ksdir/* ; do
    ova=$dir/.treeinfo.ova
    if [ ! -f $ova ]; then
        echo "Skipping $dir, no $ova file present"
        continue
    fi

    os=$(awk '{print $1}' $ova)
    ver=$(awk '{print $2}' $ova)
    arch=$(awk '{print $3}' $ova)

    cobbler import --name=$os-$ver --arch=$arch --path=$dir

    sed -e 's#^reboot.*#poweroff#' /var/lib/cobbler/kickstarts/sample_end.ks \
        > /var/lib/cobbler/kickstarts/sample-$os-$ver-$arch.ks

    if [[ "$ver" =~ "$rawhide" ]]; then
        cobbler repo add --name=f$ver-$arch --arch=$arch --mirror-locally=0 \
            --mirror=$url/development/$arch/os
        repos=""
    else
        cobbler repo add --name=f$ver-$arch --arch=$arch --mirror-locally=0 \
            --mirror=$url/releases/$ver/Everything/$arch/os
        cobbler repo add --name=f$ver-$arch-updates --arch=$arch --mirror-locally=0 \
            --mirror=$url/updates/$ver/$arch.newkey
        repos="f$ver-$arch f$ver-$arch-updates"
    fi

    cobbler profile edit --name=$os-$ver-$arch \
        --repos="$repos" \
        --kickstart=/var/lib/cobbler/kickstarts/sample-$os-$ver-$arch.ks
done

node_arch=$(rpm -q --qf "%{arch}" ovirt-node-image)
node_dir=/usr/share/ovirt-node-image

cobbler distro add --name="oVirt-Node-$node_arch" --arch=$node_arch \
    --initrd=$node_dir/tftpboot/initrd0.img --kernel=$node_dir/tftpboot/vmlinuz0 \
    --kopts="rootflags=loop root=/ovirt-node-image.iso rootfstype=iso9660 ro console=tty0 console=ttyS0,115200n8"

cobbler profile add --name=oVirt-Node-$node_arch --distro=oVirt-Node-$node_arch
cobbler system add --netboot-enabled=1 --profile=oVirt-Node-$node_arch \
    --name=node3 --mac=00:16:3e:12:34:57
cobbler system add --netboot-enabled=1 --profile=oVirt-Node-$node_arch \
    --name=node4 --mac=00:16:3e:12:34:58 --kopts="ovirt_init=scsi ovirt_local_boot"
cobbler system add --netboot-enabled=1 --profile=oVirt-Node-$node_arch \
    --name=node5 --mac=00:16:3e:12:34:59 --kopts="ovirt_init=scsi"

sed -i -e '/kernel /a \\tIPAPPEND 2' /etc/cobbler/pxe/pxesystem.template
sed -i -e "s/^ONTIMEOUT.*/ONTIMEOUT oVirt-Node-$node_arch/" \
    /etc/cobbler/pxe/pxedefault.template

service cobblerd restart
cobbler sync

