#!/bin/sh

set -e

case "$1" in
    ""|-h|--help)
        echo "Usage: $0 directory [ ... ]"
        exit 1
        ;;
esac

for dir in "$@"; do
    if [ ! -d "$dir" ]; then
        echo "$dir is not a directory"
        exit 2
    fi
    if [ -f "$dir"/index.theme ]; then
        if [ -f "$dir"/icon-theme.cache ]; then
            # The cache already exists, regenerate it
            gtk-update-icon-cache --force --quiet "$dir"
        fi
    else
        # No more index.theme, remove the cache if it exists
        rm -f "$dir"/icon-theme.cache
        rmdir -p --ignore-fail-on-non-empty "$dir"
    fi
done

