summaryrefslogtreecommitdiff
path: root/publish-purge
blob: 8443cf09388a1598cef11d377f382c7df8087dc8 (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
#!/bin/bash

set -e


test_if_ancient()
{
    d=$1
    ttl=$2

    mtime=$(stat --format "%Y" "$d")
    tod=$(($mtime + 3600*24*$ttl))
    now=$(date +%s)

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

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


cleanit()
{
    d=$1
    [ -d "$d" ] || exit 0
    [ -e $d/.publish.ttl ] && ttl=`cat $d/.publish.ttl`

    if test_if_ancient $d $ttl; then
        [ "$optVerbose" -ge "1" ] && echo "Purge $d"
        rm -rf "$d"
    fi
    return 0
}

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

OPTIONS:
   -h     Show this message.
   -v     Give verbose output. Be more verbose with -vv.
EOF
}

optVerbose=0

while getopts "vh" OPTION; do
    case $OPTION in
        h)
            usage
            ;;
        v)
            optVerbose=$(($optVerbose + 1))
            ;;
    esac
done


ttl=14
[ -e ~/.publish-purge.cfg ] && . ~/.publish-purge.cfg

# We would not like that cleanit removes files in directories that mallory set
# as $base
if ! stat --format=%a ~/.publish-purge.cfg | egrep -q ".00"; then
    echo "For security reasons ~/.publish-purge.cfg should have no perms for group and others."
fi

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

if ! [ -e "$base" ]; then
    echo "Unknown base directory $base"
    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 "Place a .publish-purge.token in $base"
    exit 1
fi

for d in *; do
    pushd . > /dev/null
    # we launch a subshell so that setting $ttl within the subshell
    # is not visible outside the subshell
    ( cleanit $d )
    popd > /dev/null
done