summary refs log tree commit diff
path: root/bin/trash
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2020-01-26 17:16:07 +0100
committerRobert Günzler <r@gnzler.io>2020-01-26 18:00:59 +0100
commitf5d56602df70950de6b7d40966b00c9939c81763 (patch)
treeb50ffeb5496fe3525cc1bab2629389ced95435b7 /bin/trash
Intial commit of v2
See https://drewdevault.com/2019/12/30/dotfiles.html
Diffstat (limited to 'bin/trash')
-rwxr-xr-xbin/trash53
1 files changed, 53 insertions, 0 deletions
diff --git a/bin/trash b/bin/trash
new file mode 100755
index 0000000..5b810c1
--- /dev/null
+++ b/bin/trash
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+set -e
+
+TRASH="$HOME/.trash"
+if [ ! -d "$TRASH" ]; then
+    mkdir -p "$TRASH"
+fi
+
+empty_trash() {
+    if [ ! -d "$TRASH" ] || [ -z "$(ls "$TRASH")" ]; then
+        exit 0
+    fi
+    echo " +++ trash +++"
+    ls "$TRASH"
+    printf " +++ take it out? [yN] "; read -r yn
+    if [ "$yn" = "y" ]; then
+        printf ' +++ removing %s/* ' "$TRASH"
+        rm -rf "${TRASH:?}/*"
+        echo 'done.'
+    fi
+}
+
+install_auto_empty() {
+    case "$SHELL" in
+        *zsh) logoutfile="$ZDOTDIR/.zlogout" ;;
+        *bash) logoutfile="$HOME/.bash_logout" ;;
+        *)
+            echo "unknown shell $SHELL" 1>&2; exit 1 ;;
+    esac
+    cat > "$logoutfile" <<EOF
+# auto-empty-trash routine of $0
+yes | $0 --empty
+EOF
+}
+
+case "$1" in
+    -e|--empty)
+        empty_trash
+        exit 0 ;;
+    -i|--install)
+        install_auto_empty
+        exit 0 ;;
+    -h|--help)
+        echo "usage: trash [-e|--empty] FILE..."
+        exit 1 ;;
+esac
+
+# main
+for f in "$@"; do
+    echo "+ $(basename "$f")"
+    mv "$f" "$TRASH/"
+done