~gpanders/pushbroom

56c60d457432a050c48fbb96a4e3632f37deb7e4 — Greg Anders 4 years ago b446e07
Parameterize install script: options can now be specified at installation instead of modifying janitor.sh
3 files changed, 59 insertions(+), 19 deletions(-)

R janitor.sh => bin/janitor
D crontab
M install.sh
R janitor.sh => bin/janitor +7 -7
@@ 1,15 1,15 @@
#!/usr/bin/env bash

# Define which folder to clean out
export TARGET_DIR=$HOME/Downloads
# Define which folder to clean out (default: ~/Downloads)
export TARGET_DIR=${1:-$HOME/Downloads}

# How long should a file be able to live in the folder?
export DAYS_TO_KEEP=30
# How many days should a file be able to live in the folder? (default: 30)
export DAYS_TO_KEEP=${2:-30}

# Delete this line if you want to hard-delete files instead of moving them to Trash
export TRASH_DIR=$HOME/.Trash/
# Trash folder to move deleted files to (default: ~/.Trash)
export TRASH_DIR=${3:-$HOME/.Trash/}

# Location to store log files, relative to the Janitor directory
# Location to store log files, relative to the Janitor directory (default: logs)
export LOG_DIR=logs

function clean() {

D crontab => crontab +0 -2
@@ 1,2 0,0 @@
# Crontab for Janitor
0  */6  *  *  * $HOME/.janitor.sh

M install.sh => install.sh +52 -10
@@ 1,21 1,63 @@
#!/usr/bin/env bash

# Symlink janitor.sh to /usr/local/bin/janitor
if [ ! -L $HOME/.janitor.sh ]; then
    echo "Creating symlinks."
    ln -s $(pwd)/janitor.sh $HOME/.janitor.sh 
fi
TARGET_DIR=$HOME/Downloads
NUM_DAYS=30
TRASH_DIR=$HOME/.Trash

while getopts "d:t:n:" opt; do
    case $opt in
        d)
            TARGET_DIR="$OPTARG"
            ;;
        t)
            TRASH_DIR="$OPTARG"
            ;;
        n)
            NUM_DAYS="$OPTARG"
            ;;
        \?)
            echo "Invalid option: -$OPTARG" >&2
            ;;
    esac
done

echo "Target directory is $TARGET_DIR"
echo "Deleting files older than $NUM_DAYS"
echo "Trash direcotry is $TRASH_DIR"

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

CURRENT_CRONTAB="$(crontab -l 2>/dev/null)"
function check_crontab() {
    if [[ $CURRENT_CRONTAB == *"# Begin Janitor job"* && \
          $CURRENT_CRONTAB == *"# End Janitor job"* ]]; then
        return 1
    else
        return 0
    fi
}


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

mkdir -p logs