summaryrefslogtreecommitdiff
path: root/publish-purge
blob: cc17338f94aaa50187caec142147f6053eba4a36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash

# A cleanup script for files published using publish(1).

# Copyright (c) 2012 Stefan Huber
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.


set -e
set -u

test_if_ancient()
{
    local d="$1"
    local ttl="$2"
    d=$(readlink -f -- "$d")

    if [ -e "$d/.publish.ttl" ]; then
        mtime="$(stat --format "%Y" "$d/.publish.ttl")"
    else
        mtime="$(stat --format "%Y" "$d")"
    fi
    tod="$(($mtime + 3600*24*$ttl))"
    now="$(date +%s)"

    [ "$optVerbose" -ge "2" ] && echo "Considering $d: ttl=$ttl, mod time=$mtime, time of death=$tod, now=$now"

    [ "$tod" -lt "$now" ] && return 0
    return 1
}


cleanit()
{
    local d="$1"
    local ttl="$2"
    d=$(readlink -f -- "$d")

    if [ -e "$d/.publish.ttl" ]; then
        local newttl
        newttl="$(cat "$d/.publish.ttl")"
        if [ "$newttl" -eq "$newttl" ] 2>/dev/null; then
            # if it is a number
            ttl="$newttl"
        else
            [ "$optVerbose" -ge "1" ] && echo "Ignoring invalid TTL setting '$newttl' in $d"
        fi
    fi

    if [ "$ttl" -gt 0 ] && test_if_ancient "$d" "$ttl"; then
        [ "$optVerbose" -ge "1" ] && echo "- Purging: $d"
        [ "$optDonot" = "1" ]  || rm -rf "$d"
    fi
    return 0
}

usage()
{
    cat << EOF
Usage: $0 [OPTIONS]

OPTIONS:
   -h     Show this message.
   -n     Do not actually purge directories, but show what would be done.
   -v     Give verbose output. Be more verbose with -vv.
EOF
}

optVerbose=0
optDonot=0

while getopts "hnv" OPTION; do
   case $OPTION in
      h)
         usage
         exit 0
         ;;
      n)
         optDonot=1
         ;;
      v)
         optVerbose=$(($optVerbose + 1))
         ;;
      *)
         usage >&2
         exit 1
   esac
done


ttl=128
base=""
[ -e ~/.publish-purge.cfg ] && . ~/.publish-purge.cfg

if [ -z "$base" ]; then
    echo >&2 "$0: Please setup \$base in ~/.publish-purge.cfg."
    exit 1
fi

if ! [ -e "$base" ]; then
    echo >&2 "$0: $base does not exist or is not a directory."
    exit 1
fi

cd "$base"
# We would not like to accidentily delete files in the wrong directory, hence
# the paranoia check
if ! [ -e .publish-purge.token ]; then
    echo >&2 "$0: Place a .publish-purge.token in $base."
    exit 1
fi

for d in *; do
    cleanit "$d" "$ttl"
done


# vim:set et:
# vim:set ts=4:
# vim:set shiftwidth=4: