]> glassweightruler.freedombox.rocks Git - xdg-ninja.git/blob - xdg-ninja.sh
Fix disclaimer
[xdg-ninja.git] / xdg-ninja.sh
1 #!/usr/bin/env sh
2
3 USE_GLOW=false
4 USE_BAT=false
5 if command -v glow >/dev/null 2>/dev/null; then
6 USE_GLOW=true
7 elif command -v bat >/dev/null 2>/dev/null; then
8 USE_BAT=true
9 printf "Glow not found, markdown rendering will be done by bat.\n"
10 printf "Install glow for easier reading & copy-paste.\n"
11 else
12 printf "Glow or bat not found, markdown rendering not available.\n"
13 printf "Output will be raw markdown and might look weird.\n"
14 printf "Install glow for easier reading & copy-paste.\n"
15 fi
16
17 unalias -a
18
19 HELPSTRING="""\
20
21
22 \033[37;45;1mxdg-ninja\033[0m
23
24 \033[1;3mCheck your \$HOME for unwanted files.\033[1;0m
25
26 ────────────────────────────────────
27
28 \033[3m--help\033[0m \033[1mThis help menu\033[0m
29 \033[3m-h\033[0m
30
31 \033[3m--no-skip-ok\033[0m \033[1mDisplay messages for all files checked (verbose)\033[0m
32 \033[3m-v\033[0m
33
34 \033[3m--skip-ok\033[0m \033[1mDon't display anything for files that do not exist (default)\033[0m
35
36 """
37
38 SKIP_OK=true
39 for i in "$@"; do
40 if [ "$i" = "--help" ] || [ "$i" = "-h" ]; then
41 printf "%b" "$HELPSTRING"
42 exit
43 elif [ "$i" = "--skip-ok" ]; then
44 SKIP_OK=true
45 elif [ "$i" = "--no-skip-ok" ]; then
46 SKIP_OK=false
47 elif [ "$i" = "-v" ]; then
48 SKIP_OK=false
49 fi
50 done
51
52 if [ -z "${XDG_DATA_HOME}" ]; then
53 printf '\033[1;36m%s\033[1;0m\n' "The \$XDG_DATA_HOME environment variable is not set, make sure to add it to your shell's configuration before setting any of the other environment variables!"
54 printf "\033[1;36m ⤷ \033[1mThe recommended value is: \033[1;3m\$HOME/.local/share\033[1;0m\n"
55 fi
56 if [ -z "${XDG_CONFIG_HOME}" ]; then
57 printf '\033[1;36m%s\033[1;0m\n' "The \$XDG_CONFIG_HOME environment variable is not set, make sure to add it to your shell's configuration before setting any of the other environment variables!"
58 printf "\033[1;36m ⤷ \033[1mThe recommended value is: \033[1;3m\$HOME/.config\033[1;0m\n"
59 fi
60 if [ -z "${XDG_STATE_HOME}" ]; then
61 printf '\033[1;36m%s\033[1;0m\n' "The \$XDG_STATE_HOME environment variable is not set, make sure to add it to your shell's configuration before setting any of the other environment variables!"
62 printf "\033[1;36m ⤷ \033[1mThe recommended value is: \033[1;3m\$HOME/.local/state\033[1;0m\n"
63 fi
64 if [ -z "${XDG_CACHE_HOME}" ]; then
65 printf '\033[1;36m%s\033[1;0m\n' "The \$XDG_CACHE_HOME environment variable is not set, make sure to add it to your shell's configuration before setting any of the other environment variables!"
66 printf "\033[1;36m ⤷ \033[1mThe recommended value is: \033[1;3m\$HOME/.cache\033[1;0m\n"
67 fi
68 if [ -z "${XDG_RUNTIME_DIR}" ]; then
69 printf '\033[1;36m%s\033[1;0m\n' "The \$XDG_RUNTIME_DIR environment variable is not set, make sure to add it to your shell's configuration before setting any of the other environment variables!"
70 printf "\033[1;36m ⤷ \033[1mThe recommended value is: \033[1;3m/run/user/\$UID\033[1;0m\n"
71 fi
72
73 if ! command -v jq >/dev/null 2>/dev/null; then
74 printf "jq is needed to run this script, but it wasn't found. Please install it to be able to use this script.\n"
75 exit
76 fi
77
78 printf "\n"
79
80 # Function to expand environment variables in string
81 # https://stackoverflow.com/a/20316582/11110290
82 apply_shell_expansion() {
83 data="$1"
84 delimiter="__apply_shell_expansion_delimiter__"
85 command=$(printf "cat <<%s\n%s\n%s" "$delimiter" "$data" "$delimiter")
86 eval "$command"
87 }
88
89 # Returns 0 if the path doesn't lead anywhere
90 # Returns 1 if the path leads to something
91 check_if_file_exists() {
92 FILE_PATH=$(apply_shell_expansion "$1")
93 if [ -e "$FILE_PATH" ]; then
94 return 1
95 else
96 return 0
97 fi
98 }
99
100 decode_string() {
101 printf "%s\n" "$1" | sed -e 's/\\n/\
102 /g' -e 's/\\\"/\"/g' # Replace \n with literal newline and \" with "
103 }
104
105 # Function to handle the formatting of output
106 log() {
107 MODE="$1"
108 NAME="$2"
109 FILENAME="$3"
110 HELP="$4"
111
112 case "$MODE" in
113
114 ERR)
115 printf '[\033[1;31m%s\033[1;0m]: \033[1;3m%s\033[1;0m\n' "$NAME" "$FILENAME"
116 ;;
117
118 WARN)
119 printf '[\033[1;33m%s\033[1;0m]: \033[1;3m%s\033[1;0m\n' "$NAME" "$FILENAME"
120 ;;
121
122 INFO)
123 printf '[\033[1;36m%s\033[1;0m]: \033[1;3m%s\033[1;0m\n' "$NAME" "$FILENAME"
124 ;;
125
126 SUCS)
127 [ "$SKIP_OK" = false ] &&
128 printf '[\033[1;32m%s\033[1;0m]: \033[1;3m%s\033[1;0m\n' "$NAME" "$FILENAME"
129 ;;
130
131 HELP)
132 if [ "$USE_GLOW" = true ]; then
133 decode_string "$HELP" | glow -
134 elif [ "$USE_BAT" = true ]; then
135 decode_string "$HELP" | bat -pp --decorations=always --color=always --language markdown
136 else
137 decode_string "$HELP"
138 fi
139 ;;
140
141 esac
142 }
143
144 # Checks that the given file does not exist, otherwise outputs help
145 check_file() {
146 NAME="$1"
147 FILENAME="$2"
148 MOVABLE="$3"
149 HELP="$4"
150
151 check_if_file_exists "$FILENAME"
152
153 case $? in
154
155 0)
156 log SUCS "$NAME" "$FILENAME" "$HELP"
157 ;;
158
159 1)
160 if [ "$MOVABLE" = true ]; then
161 log ERR "$NAME" "$FILENAME" "$HELP"
162 else
163 log WARN "$NAME" "$FILENAME" "$HELP"
164 fi
165 if [ "$HELP" ]; then
166 log HELP "$NAME" "$FILENAME" "$HELP"
167 else
168 log HELP "$NAME" "$FILENAME" "_No help available._"
169 fi
170 ;;
171
172 esac
173 }
174
175 # Reads files from programs/, calls check_file on each file specified for each program
176 do_check_programs() {
177 while IFS="
178 " read -r name; read -r filename; read -r movable; read -r help; do
179 check_file "$name" "$filename" "$movable" "$help"
180 done <<EOF
181 $(jq 'inputs as $input | $input.files[] as $file | $input.name, $file.path, $file.movable, $file.help' "$(dirname "$0")"/programs/* | sed -e 's/^"//' -e 's/"$//')
182 EOF
183 # sed is to trim quotes
184 }
185
186 check_programs() {
187 printf "\033[1;3mStarting to check your \033[1;36m\$HOME.\033[1;0m\n"
188 printf "\n"
189 do_check_programs
190 printf "\033[1;3mDone checking your \033[1;36m\$HOME.\033[1;0m\n"
191 printf "\n"
192 printf "\033[3mIf you have files in your \033[1;36m\$HOME\033[1;0m that shouldn't be there, but weren't recognised by xdg-ninja, please consider creating a configuration file for it and opening a pull request on github.\033[1;0m\n"
193 printf "\n"
194 }
195
196 check_programs