#!/bin/bash
# 
# We cannot force the partition table cylinder size,
# due to the need to preserve existing OEM partitions.
#
# We cannot position the SR at 8GB, as an OEM partition
# may occupy that location.
#
# This script assumes that any existing partitions are
# contiguous at the head of the disk.
#
# Copyright (c) Citrix Systems 2008
# All rights reserved.

SYSTEM_PARTITION_SIZE="4096M"
STATE_PARTITION_SIZE="1024M"

DISK=$1
[ -b ${DISK} ] || ( echo >&2 "Disk is not a block device: ${DISK}" ; exit 1 )

# How many existing primary partitions are there?
# -----------------------------------------------
if fdisk -l ${DISK} 2>&1 | grep -q "doesn't contain a valid" ; then
    # No partition table present
    PREV_PRI_COUNT=0
else
    PREV_PRI_COUNT=$(fdisk -l ${DISK} | sed -ne 's,^'${DISK}'p\?[1-4] ,,p' | wc -l)
fi

if [ \( ${PREV_PRI_COUNT} -gt 2 \) -o \( ${PREV_PRI_COUNT} -eq 0 \)  ] ; then
    # Cannot coexist with these partitions, so remove them.
    if ! echo -e "o\nw\n" | fdisk ${DISK} >/dev/null 2>&1 ; then
        echo >&2 "Failed to create empty partition table on ${DISK}"
        exit 1
    fi
    PREV_PRI_COUNT=0
else
    # turn off the active flags on any existing partitions
    for PNUM in 1 2 3 4; do
        ACTIVE=$(fdisk -l ${DISK} | sed -ne 's,^'${DISK}'p\?'${PNUM}'  *\*,,p')
        if [ -n "${ACTIVE}" ] ; then
            if ! echo -e "a\n${PNUM}\nw\n" | fdisk ${DISK} >/dev/null 2>&1 ; then
                echo >&2 "Failed to clear active flag on partition ${PNUM} on ${DISK}"
                exit 1
            fi
        fi
    done
fi

# Is there an existing extended partition?
# ----------------------------------------
EXTENDED=$(fdisk -l ${DISK} | sed -ne 's,^\('${DISK}'p\?[1-4]\).*Extended$,\1,p')
if [ "x${EXTENDED}" != "x" ] ; then
    # Cannot coexist with an existing extended partition.
    # Create an empty partition table.
    if ! echo -e "o\nw\n" | fdisk ${DISK} >/dev/null 2>&1 ; then
        echo >&2 "Failed to create empty partition table on ${DISK}"
        exit 1
    fi
    PREV_PRI_COUNT=0
fi


get_available_primary_partition_number() {
    for PNUM in 1 2 3 4; do
        TEST=$(fdisk -l ${DISK} | sed -ne 's,'^${DISK}'p\?'${PNUM}' ,,p')
        [ "x${TEST}" != "x" ] || return ${PNUM}
    done
    return 0
}

# Create the FAT partition
# ------------------------
if [ "x${PREV_PRI_COUNT}" == "x0" ] ; then
    FAT_PNUM=1
    SPECIFY=""
else
    get_available_primary_partition_number
    FAT_PNUM=$?
    SPECIFY="${FAT_PNUM}\n"
fi
if ! echo -e "n\np\n${FAT_PNUM}\n\n+1\nt\n${SPECIFY}6\na\n${FAT_PNUM}\nw\n" | \
     fdisk ${DISK} >/dev/null 2>&1
then
    echo >&2 "Failed to create FAT partition on ${DISK}"
    exit 1
fi

# Create the Extended partition
# -----------------------------
get_available_primary_partition_number
EXTENDED_PNUM=$?
SPECIFY=""
[ "x${PREV_PRI_COUNT}" == "x2" ] || SPECIFY="${EXTENDED_PNUM}\n"
if ! echo -e "n\ne\n${SPECIFY}\n\nw\n" | fdisk ${DISK} >/dev/null 2>&1
then
    echo >&2 "Failed to create Extended partition on ${DISK}"
    exit 1
fi

# Create the logical partitions
# -----------------------------
SPECIFY=""
[ "x${PREV_PRI_COUNT}" == "x2" ] || SPECIFY="l\n"

#       1 logical partition (system image)
#       1 logical partition (system image)
#       1 logical partition (state)
#       1 logical partition (SR)

if ! echo -e "n\n${SPECIFY}\n+${SYSTEM_PARTITION_SIZE}\nn\n${SPECIFY}\n+${SYSTEM_PARTITION_SIZE}\nn\n${SPECIFY}\n+${STATE_PARTITION_SIZE}\nn\n${SPECIFY}\n\nt\n8\n8e\nw\n"\
     | fdisk ${DISK} >/dev/null 2>&1
then
    echo >&2 "Failed to create logical partitions on ${DISK}"
    exit 1
fi

exit 0
