Ignore template output
[moggers87-useful.git] / clean-cache.sh
blob8c94e365948c8525dc7cc84990c7ad45e59f85ad
1 #!/bin/bash
3 set -euo pipefail
5 if [[ -z ${XDG_CACHE_HOME:-} ]]; then
6 cache_dir=$HOME/.cache
7 else
8 cache_dir=$XDG_CACHE_HOME
9 fi
11 if [[ ! -e $cache_dir ]]; then
12 echo "Can't find $cache_dir, exiting..."
13 exit 1
16 if [[ -z ${CLEAN_CACHE_CONFIG:-} ]]; then
17 rc_path="~/.config/moggers87/clean_cacherc"
18 else
19 rc_path=$CLEAN_CACHE_CONFIG
22 expanded_path=${rc_path/#\~/$HOME}
24 if [ -e $expanded_path ]; then
25 . $expanded_path
28 if [[ -z ${days:-} ]]; then
29 days=360
32 progname=`basename $0`
34 sub_help() {
35 echo "$progname [command]"
36 echo ""
37 echo "Commands:"
38 echo ""
39 echo " clean"
40 echo " Cleans $cache_dir of files that haven't been accessed in the last $days days. Deletes empty directories. Expects environment variables set in $rc_path. Set days to control what is considered old." | fmt
41 echo ""
42 echo " find"
43 echo " Same as clean, except it returns the list of files that would be removed rather than actually removing them" | fmt
44 echo ""
45 echo " example"
46 echo " Add an example config file to $rc_path if that path does not already exist" | fmt
47 echo ""
50 sub_example() {
51 if [ -e $expanded_path ]; then
52 echo "$rc_path already exists, refusing to overwrite!"
53 return 1
55 mkdir -p `dirname $expanded_path`
56 cat <<EOF > $expanded_path
57 # how many days old should something be before it's removed from the cache?
58 export days=365
59 EOF
62 sub_clean() {
63 find $cache_dir -type f -atime +$days -delete
64 find $cache_dir -type d -empty -delete
67 sub_find() {
68 find $cache_dir -type f -atime +$days
71 subcommand=${1:-}
72 case $subcommand in
73 "" | "-h" | "--help")
74 sub_help
77 shift
78 if [ -n "$(type -t sub_${subcommand})" ]; then
79 sub_${subcommand} $@
80 else
81 echo "Error: '$subcommand' is not a valid command" >&2
82 exit 1
85 esac