summary refs log tree commit diff
path: root/bin/saferm
blob: 10c59a71dff7b9c34b9cf77e36a696d5cfaa0ee9 (plain) (blame)
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
71
72
73
74
75
#!/bin/bash

set -e
[ -n "$DEBUG" ] && set -x

if [ ! -t 1 ] || [ -n "$UNSAFE_RM" ]; then
	# shellcheck disable=SC2068
	exec /bin/rm "$@"
fi

# stop me from being stupid

OPTIND=0
for o in "$@"; do
	OPTIND+=1
	if [ -d "$o" ] || [ -f "$o" ]; then
		break
	fi
	[ "$o" = "-r" ] && recurse=1
	[ "$o" = "-f" ] && force=1
	[ "$o" = "-rf" ] && force=1 && recurse=1
	[ "$o" = "-h" ] && {
		printf "usage: saferm\n\n"
		exit 0
	}
done

if [ -n "${force}" ] && [ -n "${recurse}" ]; then
	tput setaf 1
	(
		echo "!"
		echo "!" "recursive force not allowed"
		echo "!"
	) >&2
	tput sgr0
	exit 1
fi

rm_prompt_arg=-I
files=()
for ((idx = OPTIND; idx <= $#; idx++)); do
	if [ -e "${!idx}" ]; then
		if [ -d "${!idx}" ]; then
			tput setaf 1
			(
				exa --color=never --all --tree "${!idx}" >&2
				echo "!"
				echo "!" "$(du -sh --total "${!idx}" | grep total)"
				echo "!" "$(fd --no-ignore --type file . "${!idx}" | wc -l)" "files"
				echo "!" "$(fd --no-ignore --type directory . "${!idx}" | wc -l)" "directories"
				echo "!"
			) >&2
			tput sgr0
		else
			tput setaf 1
			echo "${!idx}" >&2
			files+=("${!idx}")
		fi
	fi
done

if [ ${#files[@]} -gt 0 ]; then
	rm_prompt_arg=-i
	tput setaf 1
	(
		echo "!"
		echo "!" "$(du -sh --total "${files[@]}" | grep total)"
		echo "!" "${#files[@]}" "files"
		echo "!"
	) >&2
	tput sgr0
fi

# shellcheck disable=SC2068
exec /bin/rm ${rm_prompt_arg} --one-file-system "$@"