#!/bin/sh

FREQUENCY=864000 # 10 days

cache_dir="$HOME/.cache"

if [ -n "$XDG_CACHE_HOME" ]; then
  cache_dir="$XDG_CACHE_HOME"
fi

stamp_file="$cache_dir/zeitgeist-vacuum.stamp"
timestamp=0

if [ -f "$stamp_file" ]; then
  timestamp="$(cat $stamp_file)"
fi

case $timestamp in
  ''|*[!0-9]*) timestamp=0 ;;
  *) ;;
esac

if [ $(($(date +%s) - $timestamp)) -lt $FREQUENCY ]; then
  exit 0;
fi

if (/usr/bin/zeitgeist-daemon --vacuum); then
  mkdir -p "$cache_dir"
  date +%s > "$stamp_file"
else
  exit $?
fi
