#!/bin/sh
# vim:ts=8:et:fdm=marker
#
# dwww-refresh-cache -- refreshes dwww cache directory
#
# Part of the dwww package.  Written by Robert Luberda.

set -e

. /usr/share/dwww/functions.sh && dwww_initialize  || exit 1

export PATH=.:$PATH
echo -n "checking cache directory: $dwww_cache_dir..."
dwww_setup_cache_dir
echo done



# always remove temporary files, generated mostly by index++
echo -n "removing temporary files in cache dir..."
find "$dwww_cache_dir/" -maxdepth 1 \
        -atime +2 \( -name '*.tmp.*' \
        -o -name '[0-9]*.[0-9]*' \) \
        -type f -print0 | xargs -0 rm -f
echo done

# remove temporary files, generated by dwww-build and dwww-build-menu
echo -n "removing temporary files in temp dir..."
find "$DWWW_TMPDIR/" -maxdepth 1 \
        -ctime +1 \( -name 'dwww-build.*' \
        -o -name 'dwww-build-menu.*' \) \
        -type d -print0 | xargs -0 rm -rf
echo done

# remove accessed documents cache
if [ -n "$DWWW_KEEPDAYS" ] ; then
        echo -n "removing stale cache entries..."
        find "$dwww_cache_dir/" -maxdepth 3 \
                 -atime +"$DWWW_KEEPDAYS" -name '[^.]*' \
                ! -name 'dwww.swish++.index' \
                ! -name '*.dat' \
                -type f -print0 | xargs -0 rm -f
        echo done

        su -s /bin/sh "$DWWW_CGIUSER" -c '/usr/sbin/dwww-cache --clean' < /dev/null || true

fi

pkglist="$dwww_cache_dir/pkglist.tmp.$$"

if [ -n "$DWWW_QUICKFIND_DB" ] || [ -n "$DWWW_DOCBASE2PKG_DB" ] ; then
        echo -n "generating installed packages and programs list..."
        LC_ALL=C dpkg-query --search '*bin/*' '/usr/games/*' '/usr/share/doc-base/*' > "$pkglist" 2>/dev/null || true
        echo "done"
fi

if [ -n "$DWWW_QUICKFIND_DB" ] || [ -n "$DWWW_DOCBASE2PKG_DB" ] ; then
        echo -n "generating source and virtual package list..."
        LC_ALL=C dpkg-query --show --showformat='${status}, ${package}, ${source}, ${provides}\n' | 
                perl -ne '
                                chomp();
                                my ($status, $package, $source, @provides) = split(/, /);
                                next unless $status =~ /(^install)|(installed$)/;
                                next unless $package;
                                $source =~ s/ \(.*\).*// if $source;
                                print "$package: PKG-$package\n" unless $source;
                                print "$source: PKG-$package\n" if $source;
                                grep { print "$package: VRT-$_\n" } @provides if @provides;
                        ' >> "$pkglist"
        echo "done"
fi

if [ -n "$DWWW_DOCBASE2PKG_DB" ] ; then
        echo -n "creating doc-base files to packages mapping file..."
        grep '/usr/share/doc-base/' "$pkglist" | perl -ne '
                chomp();
                my ($pkg, $file) = split(/\s*:\s*/, $_, 2);
                open (F, "<", $file) or next;
                $_ = <F>;
                chomp();
                close (F);
                s/^Document\s*:\s*(\S+).*/$1/;
                next unless -f "/var/lib/doc-base/documents/$_";
                print "$_\01$pkg\n";
        ' > "$DWWW_DOCBASE2PKG_DB.tmp.$$"
        mv -f "$DWWW_DOCBASE2PKG_DB.tmp.$$" "$DWWW_DOCBASE2PKG_DB"
        chmod 644 "$DWWW_DOCBASE2PKG_DB"
        echo done
fi

if [ -n "$DWWW_QUICKFIND_DB" ] ; then
        echo -n "creating installed packages & programs cache..."
        dwww-quickfind --build "$DWWW_QUICKFIND_DB.tmp.$$" < "$pkglist"
        mv -f "$DWWW_QUICKFIND_DB.tmp.$$" "$DWWW_QUICKFIND_DB"
        chmod 644 "$DWWW_QUICKFIND_DB"
        echo done
fi

if [ -e "$pkglist" ] ; then
        rm -f "$pkglist"
fi
