#!/bin/bash

set -eu
set -o pipefail

function find_free_nbd_device () {
    # Return first free nbd device
    return_device=''
    for chk_device in /sys/class/block/nbd*; do
        # find free nbd device by checking size field
        size_chk=`cat $chk_device/size`
        if [ "$size_chk" = "0" ]; then
            # device looks free. lets use it.
            return_device=$chk_device
            break
        fi
    done
    echo $return_device
}
[ -n "$ARCH" ]
[ -n "$TARGET_ROOT" ]

if [ 'amd64' = "$ARCH" ] ; then
    ARCH="x86_64"
fi

if [ 'i386' = "$ARCH" ] ; then
    ARCH="i586"
fi

DIB_CLOUD_IMAGES=${DIB_CLOUD_IMAGES:-http://download.opensuse.org/repositories/Cloud:/Images/images/}
DIB_RELEASE=${DIB_RELEASE:-13.1}
# FIXME: Hard coded build numbers, versions, etc.
# NOTE: Actual file name seems unstable, and has changed several times.
BASE_IMAGE_NAME=${BASE_IMAGE_NAME:-openSUSE-$DIB_RELEASE-OS-guest.$ARCH-0.0.2-Build3.1}

if [ -z "$DIB_OFFLINE" ] ; then
    # XXX: Try to extract the filename of the current built from the index
    # page. As there is one built available in the repo at a given time, this
    # should work. This is a tempoary workaround until the fix for
    # https://bugzilla.novell.com/show_bug.cgi?id=853882 is deployed
    echo "Looking up current built of Base Image ($BASE_IMAGE_NAME):"
    BASE_IMAGE_FILE=${BASE_IMAGE_FILE:-$(curl $DIB_CLOUD_IMAGES | \
        sed -n "s/^.*\<a\ href\=\"\($BASE_IMAGE_NAME.*\.tar\.bz2\)\".*$/\1/p")}
    if [ -n "$BASE_IMAGE_FILE" ]; then
        echo "Using base image: $DIB_CLOUD_IMAGES/$BASE_IMAGE_FILE"
    else
        echo -e "Failed to extract image file name from $DIB_CLOUD_IMAGES" \
            "\nPlease set BASE_IMAGE_FILE manually and retry."
        exit 1
    fi
else
    BASE_IMAGE_FILE=${BASE_IMAGE_FILE:-$BASE_IMAGE_NAME-raw.tar.bz2}
fi

# FIXME: either check the checksums into git or verify the signature
# on the checksums. We should not be trusting checksums we download
# over HTTP
SHA256SUMS=${SHA256SUMS:-$DIB_CLOUD_IMAGES/$BASE_IMAGE_FILE.sha256}
CACHED_FILE=$DIB_IMAGE_CACHE/$BASE_IMAGE_FILE

if [ -n "$DIB_OFFLINE" -a -f "$CACHED_FILE" ] ; then
    echo "Not checking freshness of cached $CACHED_FILE."
else
    echo "Fetching Base Image"
    $TMP_HOOKS_PATH/bin/cache-url $SHA256SUMS $DIB_IMAGE_CACHE/SHA256SUMS.openSUSE.$DIB_RELEASE.$ARCH
    $TMP_HOOKS_PATH/bin/cache-url $DIB_CLOUD_IMAGES/$BASE_IMAGE_FILE $CACHED_FILE
    pushd $DIB_IMAGE_CACHE
    grep "$BASE_IMAGE_FILE" SHA256SUMS.openSUSE.$DIB_RELEASE.$ARCH | sha256sum --check -
    popd
fi

# Extract the qcow2 image and qcow2.sha256 files.
# to make life easier create a temp directory to unzip the files.
# TODO(NobodyCam): validate qcow2 with sha256 file
UNTAR_TO_LOCATION=$TMP_MOUNT_PATH/openSUSE/Untar
mkdir -p $UNTAR_TO_LOCATION
sudo tar -C $UNTAR_TO_LOCATION -xjf $DIB_IMAGE_CACHE/$BASE_IMAGE_FILE
# mount qcow
nbd_device=$(find_free_nbd_device)
mount_device=/dev/`basename $nbd_device`
sudo qemu-nbd -c $mount_device $UNTAR_TO_LOCATION/$BASE_IMAGE_NAME.qcow2
# Ensure that $mount_device is a block device.
if [ -b $mount_device ]; then
    org_dir=`pwd`
    # make a mount point for the image
    mkdir $UNTAR_TO_LOCATION/mnt
    # mount the images root partition
    sudo mount ${mount_device}p1 $UNTAR_TO_LOCATION/mnt
    # copy contents to the build area
    cd $UNTAR_TO_LOCATION/mnt
    sudo find . -print | sudo cpio -padmu $TARGET_ROOT
    # ensure we are not in the directory about to be unmounted
    cd $org_dir
    echo "Copied $UNTAR_TO_LOCATION/mnt to $TARGET_ROOT"
    # unmount and remove the device used for the copy
    sudo umount $UNTAR_TO_LOCATION/mnt
    sudo qemu-nbd -d $mount_device
else
    echo "Unable to extract openSUSE Image."
    exit 1
fi
# remove the temp directory
sudo rm -rf $UNTAR_TO_LOCATION
