debpartial_conf_old(){
    # generates debpartial-mirror.conf 
    # compatible with versions 0.2.8 - 0.2.11
    
    # TODO check if variables used in conf file generation are set.
    
    echo [mirror] > $debpartial_conf
    echo server=$debian_mirror >> $debpartial_conf
    
    echo local_directory=$MIRROR >> $debpartial_conf
    
    echo files=$mirror_files >> $debpartial_conf
    
    echo get_suggests=false >> $debpartial_conf
    
    # NOTE spelling mistake is intentional- debpartial-mirror uses it.
    echo get_recomends=false >> $debpartial_conf
    
    echo get_provides=false >> $debpartial_conf
    
    echo archs=$ARCH >> $debpartial_conf
    
    echo [$CODENAME] >> $debpartial_conf
    
    # use '' to prevent shell expansion
    echo filter=$debpartial_filter >> $debpartial_conf
    
    echo include-task=$TASK >> $debpartial_conf
}

debpartial_conf_old_security(){
    # generates debpartial-mirror.conf 
    # compatible with versions 0.2.8 - 0.2.11
    
    # TODO check if variables used in conf file generation are set.
    
    echo [mirror] > $debpartial_conf_security
    echo server=$security_mirror >> $debpartial_conf_security
    
    echo local_directory=$SECURITY >> $debpartial_conf_security
    
    echo get_suggests=false >> $debpartial_conf_security
    
    # NOTE spelling mistake is intentional- debpartial-mirror uses it.
    echo get_recomends=false >> $debpartial_conf_security
    
    echo get_provides=false >> $debpartial_conf_security
    
    echo archs=$ARCH >> $debpartial_conf_security
    
    echo [$CODENAME/updates] >> $debpartial_conf_security
    
    echo filter=$debpartial_filter_security >> $debpartial_conf_security
    
    echo include-task=$TASK >> $debpartial_conf_security
    }
    
    debpartial_conf_old_extra(){
    # generates debpartial-mirror.conf 
    # compatible with versions 0.2.8 - 0.2.11
    
    # TODO check if variables used in conf file generation are set.
    
    echo [mirror] > $debpartial_conf_extra
    echo server=$debian_mirror_extra >> $debpartial_conf_extra
    
    echo local_directory=$MIRROR/extra-mirror >> $debpartial_conf_extra
    
    echo get_suggests=false >> $debpartial_conf_extra
    
    # NOTE spelling mistake is intentional- debpartial-mirror uses it.
    echo get_recomends=false >> $debpartial_conf_extra
    
    echo get_provides=false >> $debpartial_conf_extra
    
    echo archs=$ARCH >> $debpartial_conf_extra
    
    echo [$CODENAME] >> $debpartial_conf_extra
    
    echo filter=$debpartial_filter_extra >> $debpartial_conf_extra
    
    echo include-task=$TASK >> $debpartial_conf_extra
}

debpartial_conf_new(){
    # generate debpartial-mirror.conf for versions 0.2.90+
    
    echo [GLOBAL] > $debpartial_conf
    
    echo mirror_dir=$MIRROR >> $debpartial_conf
    
    echo get_suggests=false >> $debpartial_conf
    
    echo get_recommends=false >> $debpartial_conf
    
    echo get_provides=false >> $debpartial_conf
    
    echo get_sources=false >> $debpartial_conf
    
    echo get_packages=true >> $debpartial_conf
    
    echo architectures=$ARCH >> $debpartial_conf
    
    echo distributions=$CODENAME >> $debpartial_conf
    
    echo components=$debpartial_components >> $debpartial_conf
    
    echo [$CODENAME] >> $debpartial_conf
    
    echo server=$debian_mirror >> $debpartial_conf
    
    echo files=$mirror_files >> $debpartial_conf
    
    echo filter = $debpartial_filter_new include-from:$TASK >> $debpartial_conf
    
    if [ -n "$security_mirror" ]; then
        # TODO: export only if the DIR exists, after doing a debpartial-mirror run?
        # tell debian-cd to get security updates 
        export SECURITY="$MIRROR/$CODENAME-security"
        # configure debpartial-mirror to grab security updates
        echo [$CODENAME-security] >> $debpartial_conf
        echo distributions=$CODENAME/updates >> $debpartial_conf
        test -z "$debpartial_components_security" && debpartial_components_security="main"
        echo components=$debpartial_components_security >> $debpartial_conf
        echo server=$security_mirror >> $debpartial_conf
        echo filter = $debpartial_filter_new include-from:$TASK >> $debpartial_conf
        echo resolve_deps_using = $CODENAME >> $debpartial_conf
    fi
    if [ -n "$debian_mirror_extra" ]; then
        echo [extra-mirror] >> $debpartial_conf
        echo server=$debian_mirror_extra >> $debpartial_conf
        test -z "$debpartial_components_extra" && debpartial_components_extra="main"
        echo components=$debpartial_components_extra >> $debpartial_conf
        echo filter = $debpartial_filter_new include-from:$TASK >> $debpartial_conf
        echo resolve_deps_using = $CODENAME >> $debpartial_conf
        echo [merged-mirror] >> $debpartial_conf
        echo backends = $CODENAME extra-mirror >> $debpartial_conf
        export MIRROR="$MIRROR/merged-mirror"
    fi
}

test -z "$debpartial_conf" && debpartial_conf=debpartial-mirror.conf
test -r "$debpartial_conf" && cp -f $debpartial_conf $debpartial_conf.bak

debpartial_version="$(debpartial-mirror -v | head -n 1 | awk '{print $2}')"
# TODO compare versions with something more common than dpkg ?
if dpkg --compare-versions "$debpartial_version" lt 0.2.90 ; then
    debpartial_conf_old
    debpartial-mirror -c $debpartial_conf
    if [ -n "$security_mirror" ]; then
        debpartial_conf_security="$debpartial_conf.security"
        test -r "$debpartial_conf_security" && cp -f $debpartial_conf_security $debpartial_conf_security.bak
        export SECURITY="$MIRROR/$CODENAME-security/"
        debpartial_conf_old_security
        debpartial-mirror -c $debpartial_conf_security
    fi
    if [ -n "$debian_mirror_extra" ]; then
        debpartial_conf_extra="$debpartial_conf.extra"
        test -r "$debpartial_conf_extra" && cp -f $debpartial_conf_extra $debpartial_con_extra.bak
        debpartial_conf_old_extra
        debpartial-mirror -c $debpartial_conf_extra
        # FIXME: somehow, get debian-cd to include this archive
    fi  
else
    debpartial_conf_new
    for opt in update upgrade ; do
        debpartial-mirror -c $debpartial_conf $opt
    done
    # make symlinks for pool and dists directories
    if [ -d "$MIRROR/$CODENAME" ]; then
        MIRROR="$MIRROR/$CODENAME"
        echo "re-setting mirror for new debpartial-mirror dir: $MIRROR"
    else
        echo "ERROR: could not find mirror: $MIRROR"
        exit 1
    fi
fi
