#!/bin/sh
# (C) Per Øyvind Karlsen 2013
# Ugly shell script, but does at least replace the original one written in
# perl, thus freeing ourself from another perl dependency..

dir='/usr/share/ldetect-lst'
files='pcitable usbtable isatable pcmciatable dmitable'

for f in $files; do
    rm -f "$dir/$f" "$dir/$f.gz"
done

if [ "$1" == "--clean" ]; then
    exit 0
fi

for f in $files; do
    d="$dir/$f.d"
    [ -d "$d" ] || continue
    num=$(find $d -name \*.lst -o -name \*.lst.gz|wc -l)
    matches="$(find $d -name \*.lst -o -name \*.lst.gz|sort -u)"
    if [ $num -eq 1 ] && echo "$matches" | grep -q -e '\.gz$'; then
	ln -f "$matches" "$dir/$f.gz"
    else
	for m in $matches ; do
	    if echo "$m" | grep -q -e '\.gz$'; then
		zcat "$m" >> "$dir/$f"
	    else
		cat "$m" >> "$dir/$f"
	    fi
	done
	gzip -9 "$dir/$f"
    fi
done
