#!/bin/sh
# preinst script for libmtp

set -e

PACKAGE=libmtp8

# Taken from http://wiki.debian.org/DpkgConffileHandling
# Remove a no-longer used conffile
rm_conffile() {
    PKGNAME="$1"
    CONFFILE="$2"
    if [ -e "$CONFFILE" ]; then
        md5sum="`md5sum \"$CONFFILE\" | sed -e \"s/ .*//\"`"
        old_md5sum="`dpkg-query -W -f='${Conffiles}' $PKGNAME | sed -n -e \"\\\\' $CONFFILE'{s/ obsolete$//;s/.* //p}\"`"
        if [ "$md5sum" != "$old_md5sum" ]; then
            echo "Obsolete conffile $CONFFILE has been modified by you."
            echo "Saving as $CONFFILE.dpkg-bak ..."
            mv -f "$CONFFILE" "$CONFFILE".dpkg-bak
        else
            echo "Removing obsolete conffile $CONFFILE ..."
            rm -f "$CONFFILE"
        fi
    fi
}

# Prepare to move a conffile without triggering a dpkg question
prep_mv_conffile() {
    PKGNAME="$1"
    CONFFILE="$2"
    if [ -e "$CONFFILE" ]; then
        md5sum="`md5sum \"$CONFFILE\" | sed -e \"s/ .*//\"`"
        old_md5sum="`dpkg-query -W -f='${Conffiles}' $PKGNAME | sed -n -e \"\\\\' $CONFFILE '{s/ obsolete$//;s/.* //p}\"`"
        if [ "$md5sum" = "$old_md5sum" ]; then
            rm -f "$CONFFILE"
        fi
    fi
}

case "$1" in
install|upgrade)
    # PART A: Check if current version is 0.3.7-3 or less
    if dpkg --compare-versions "$2" le 0.3.7-3 ; then
        # PART A.2: Prepare to move conffile
        # PART A.3: Basically rm_conffile without marking the file obsolete (I think)
            then
            fi
    fi
    fi

        # Check if libmtp5 or early libmtp6 (<< 0.2.1-2) are installed (and not purged)
        removeit="1"
        # Get libmtp* package list in the format '${Package} ${Version}'
        listpkgs="`dpkg-query -W -f '${Package} ${Version}\n' 'libmtp*' | grep '^libmtp[0-9]\+\ '`"
        # Separate by new line
        IFS=$(printf "\n")
        for ipkg in $listpkgs; do
            name="`echo $ipkg | cut -d' ' -f1`"
            version="`echo $ipkg | cut -d' ' -f2`"
            # Exception: Do not remove if libmtp5 is installed
            if [ "$name" = "libmtp5" -a ! "$version" = "" ]; then
                removeit="0"
            fi
            # Exception: Do not remove if libmtp6 (<< 0.2.1-2) is installed
            if [ "$name" = "libmtp6" -a ! "$version" = ""]; then
                if dpkg --compare-versions "$version" lt-nl "0.2.1-2"; then
                    removeit="0"
                fi
            fi
        done
        unset IFS
        # If not installed, remove the old libmtp.rules
    fi

    # PART C:
    # Disabled until the package libmtp-common is made.
    
    # PART D:
	fi

esac



exit 0
