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