~gpanders/pushbroom

ref: 927d4885b373a811fc47aba3001667d953db2e58 pushbroom/install.sh -rwxr-xr-x 1.6 KiB
927d4885Greg Anders Add quotes around argument variables to help with paths that contain spaces 4 years ago
                                                                                
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
#!/usr/bin/env bash

# Set defaults
TARGET_DIR=$HOME/Downloads
NUM_DAYS=30
TRASH_DIR=$HOME/.Trash

function usage() {
    echo "Usage: ./install.sh [-x] [-d <target directory>] [-t <trash directory>] [-n <integer>]"
}

while getopts "xd:t:n:" opt; do
    case $opt in
        d)
            TARGET_DIR="$OPTARG"
            ;;
        t)
            TRASH_DIR="$OPTARG"
            ;;
        x)
            TRASH_DIR=
            ;;
        n)
            NUM_DAYS="$OPTARG"
            ;;
        \?)
            usage
            exit 1
            ;;
    esac
done

echo "Target directory is $TARGET_DIR"
echo "Deleting files older than $NUM_DAYS"
if [ -z "$TRASH_DIR" ];  then
    echo "Hard deleting files - not using a Trash directory"
else
    echo "Trash direcotry is $TRASH_DIR"
fi

function create_crontab() {
    echo "# Begin Janitor job"
    echo "0  */6  *  *  * $(pwd)/bin/janitor \"$1\" \"$2\" \"$3\""
    echo "# End Janitor job"
}

function check_crontab() {
    if [[ $1 == *"# Begin Janitor job"* && \
          $1 == *"# End Janitor job"* ]]; then
        return 1
    else
        return 0
    fi
}

# Create crontab
if check_crontab "$(crontab -l 2>/dev/null)"; then
    # Crontab exists but does not already contain ours
    echo "Creating crontab entry."
    entry=$(create_crontab "$TARGET_DIR" "$NUM_DAYS" "$TRASH_DIR")
    (crontab -l 2>/dev/null; [[ $? -eq 0 ]] && echo " " ; echo "$entry") | crontab -
else
    echo "Janitor cronjob already exists. Remove the current job (using crontab -e) and re-run this installation script."
    exit 1
fi

mkdir -p logs

echo "Installation complete."
exit 0