summaryrefslogtreecommitdiff
path: root/saferm
blob: 4d8fc7a41194bae794454cb28df0137c58300332 (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
#!/bin/sh

archive="$HOME/.deleted"
realrm="$(which rm)"
copy="$(which cp) -R"

if [ $# -eq 0 ]; then
    exec $realrm
fi

flags=""

while getopts "dfiPRrvW" opt
do
    case $opt in
        f ) exec $realrm "$@"    ;;
        * ) flags="$flags -$opt" ;;
    esac
done
shift $(( OPTIND - 1 ))

if [ ! -d "$archive" ]; then
    if [ ! -w "$HOME" ]; then
        echo "$0 failed: can't create $archive in $HOME" >&2
        exit 1
    fi
    mkdir "$archive"
    chmod 700 "$archive"
fi

for arg
do
    newname="$archive/$(date "+%S.%M.%H.%d.%m").$(basename "$arg")"
    if [ -f "$arg" ] || [ -d "$arg" ]; then
        $copy "$arg" "$newname"
    fi
done

exec $realrm "$flags" "$@"