#! /bin/sh

set -e

. /usr/share/debconf/confmodule

# Partman assumes 1MB = 1000*1000 bytes, so convert
# from MiB to partman's MB. Poor man's way of doing
# this is just to add 5% (1/(1000*1000)*(1024*1024)=1.048576)
mib_to_mb() {
    SIZE=$1
    PERCENT=5
    
    SIZE=$(expr ${SIZE} + ${SIZE} \* ${PERCENT} / 100 + 1)
    echo ${SIZE}
}

db_get cdrom-detect/try-hdd || true

if [ "${RET}" != "true" ]
then
    BLOCKS=`/usr/sbin/sfdisk -s /dev/sdb1`
    RECOVERY_MIB=$(expr ${BLOCKS} / 1024 + 1)
    RECOVERY=$(mib_to_mb ${RECOVERY_MIB})
    MAXRECOVERY=$(expr ${RECOVERY} + 10)

    db_set partman-auto/expert_recipe string          \
        recovery-recipe ::                            \
            ${RECOVERY} 100000 ${MAXRECOVERY} fat32   \
                \$primary{ }                          \
                \$bootable{ }                         \
                method{ format }                      \
                format{ }                             \
                use_filesystem{ }                     \
                filesystem{ fat32 }                   \
            .                                         \
            500 10000 1000000 ext3                    \
                \$primary{ }                          \
                \$bootable{ }                         \
                method{ format }                      \
                format{ }                             \
                use_filesystem{ }                     \
                filesystem{ ext3 }                    \
                mountpoint{ / }                       \
            .                                         \
            105% 10000 107% linux-swap                \
                method{ swap }                        \
                format{ }                             \
            .
else
    db_set partman-auto/expert_recipe string          \
        recovery-recipe ::                            \
            500 10000 1000000 ext3                    \
                \$primary{ }                          \
                \$bootable{ }                         \
                method{ format }                      \
                format{ }                             \
                use_filesystem{ }                     \
                filesystem{ ext3 }                    \
                mountpoint{ / }                       \
            .                                         \
            105% 10000 107% linux-swap                \
                method{ swap }                        \
                format{ }                             \
            .
fi
