#!/bin/sh

set -e

is_26() {
    echo $1 | grep -q -E '^2\.6\.'
}

is_diverted() {
    dpkg-divert --list | egrep -q "diversion of $1 to $1-orig by loop-aes-_KVERS_" 
}
    
# Remove diversion of loop.ko for older 2.6 modules
if [ "$1" = configure ] && [ "$2" != "" ]; then
    if is_26 _KVERS_; then
        oldversion=${2%%+*}
        if dpkg --compare-versions "$oldversion" lt "3.1c-2"; then
            module=/lib/modules/_KVERS_/kernel/drivers/block/loop.ko

            if is_diverted $module; then
                dpkg-divert --remove --rename --package loop-aes-_KVERS_ --divert ${module}-orig ${module}
            fi
        fi
    fi
fi
    
depmod -a -F /boot/System.map-_KVERS_ _KVERS_ || true

