#!/bin/sh
set -e

LAB_DIRS='binary source udeb info changes'

non_empty_dir() {
    dir="$1"
    [ -d "$dir" ] && [ "$(ls -A "$dir")" ]
}

if [ "$1" = "configure" ]; then
    if [ -d /var/spool/lintian ]; then
        IN_USE=no
        HAS_LAB_FOLDER=no
        for DIR in $LAB_DIRS ; do
            if [ -d "/var/spool/lintian/$DIR" ] ; then
                HAS_LAB_FOLDER=yes
            fi
            if non_empty_dir "/var/spool/lintian/$DIR" ; then
                IN_USE=yes
                break
            fi
        done
        if [ "$IN_USE" = "no" ] && [ "$HAS_LAB_FOLDER" = "yes" ]; then
            echo "Removing empty lintian lab in /var/spool/lintian"
            for DIR in $LAB_DIRS ; do
                if [ -d "/var/spool/lintian/$DIR" ]; then
                    rmdir "/var/spool/lintian/$DIR"
                fi
            done
            rmdir --ignore-fail-on-non-empty /var/spool/lintian
        fi
    fi
fi

#DEBHELPER#
