From 56c60d457432a050c48fbb96a4e3632f37deb7e4 Mon Sep 17 00:00:00 2001 From: Greg Anders Date: Fri, 4 Dec 2015 10:17:55 -0600 Subject: [PATCH] Parameterize install script: options can now be specified at installation instead of modifying janitor.sh --- janitor.sh => bin/janitor | 14 ++++----- crontab | 2 -- install.sh | 62 ++++++++++++++++++++++++++++++++------- 3 files changed, 59 insertions(+), 19 deletions(-) rename janitor.sh => bin/janitor (64%) delete mode 100755 crontab diff --git a/janitor.sh b/bin/janitor similarity index 64% rename from janitor.sh rename to bin/janitor index eb94bdc..d261925 100755 --- a/janitor.sh +++ b/bin/janitor @@ -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() { diff --git a/crontab b/crontab deleted file mode 100755 index 17024d4..0000000 --- a/crontab +++ /dev/null @@ -1,2 +0,0 @@ -# Crontab for Janitor -0 */6 * * * $HOME/.janitor.sh diff --git a/install.sh b/install.sh index 0d1ffbb..33eb946 100755 --- a/install.sh +++ b/install.sh @@ -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 -- 2.26.2