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