]> glassweightruler.freedombox.rocks Git - xdg-ninja.git/blob - xdg-ninja.sh
Add avidemux, BurpSuite, kite and Osmos (game) (#57)
[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 # Return 1 if the path points to a file, 2 if it points to a directory
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 # Function to handle the formatting of output
101 log() {
102 MODE="$1"
103 NAME="$2"
104 FILENAME="$3"
105 HELP="$4"
106
107 case "$MODE" in
108
109 ERR)
110 printf '[\033[1;31m%s\033[1;0m]: \033[1;3m%s\033[1;0m\n' "$NAME" "$FILENAME"
111 ;;
112
113 WARN)
114 printf '[\033[1;33m%s\033[1;0m]: \033[1;3m%s\033[1;0m\n' "$NAME" "$FILENAME"
115 ;;
116
117 INFO)
118 printf '[\033[1;36m%s\033[1;0m]: \033[1;3m%s\033[1;0m\n' "$NAME" "$FILENAME"
119 ;;
120
121 SUCS)
122 [ "$SKIP_OK" = false ] &&
123 printf '[\033[1;32m%s\033[1;0m]: \033[1;3m%s\033[1;0m\n' "$NAME" "$FILENAME"
124 ;;
125
126 HELP)
127 if $USE_GLOW; then
128 printf "%s\n" "$HELP" | glow -
129 elif $USE_BAT; then
130 printf "%s\n" "$HELP" | bat -pp --decorations=always --color=always --language markdown
131 else
132 printf "%s\n" "$HELP"
133 fi
134 ;;
135
136 esac
137 }
138
139 # Checks that the given file does not exist, otherwise outputs help
140 check_file() {
141 NAME="$1"
142 FILENAME="$2"
143 MOVABLE="$3"
144 HELP="$4"
145
146 check_if_file_exists "$FILENAME"
147
148 case $? in
149
150 0)
151 log SUCS "$NAME" "$FILENAME" "$HELP"
152 ;;
153
154 1)
155 if "$MOVABLE"; then
156 log ERR "$NAME" "$FILENAME" "$HELP"
157 else
158 log WARN "$NAME" "$FILENAME" "$HELP"
159 fi
160 if [ "$HELP" ]; then
161 log HELP "$NAME" "$FILENAME" "$HELP"
162 else
163 log HELP "$NAME" "$FILENAME" "_No help available._"
164 fi
165 ;;
166
167 esac
168 }
169
170 decode_string() {
171 tmp="${1#\"}" # Trim leading quote
172 tmp="${tmp%\"}" # Trim traling quote
173 printf "%s" "$(echo "$tmp" | sed -e 's/\\n/\
174 /g' -e 's/\\\"/\"/g')" # Replace \n with literal newline and \" with "
175 }
176
177 # Reads a file from programs/, calls check_file on each file specified for the program
178 check_program() {
179 PROGRAM=$1
180
181 while IFS="
182 " read -r name; read -r filename; read -r movable; read -r help; do
183 check_file "$(decode_string "$name")" "$(decode_string "$filename")" "$movable" "$(decode_string "$help")"
184 done <<EOF
185 $(jq '.files[] as $files | .name, $files.path, $files.movable, $files.help' "$PROGRAM")
186 EOF
187 }
188
189 # Loops over all files in the programs/ directory and calls check_program
190 enumerate_programs() {
191 printf "\033[1;3mStarting to check your \033[1;36m\$HOME.\033[1;0m\n"
192 printf "\n"
193 for prog_filename in "$(dirname "$0")"/programs/*; do
194 check_program "$prog_filename"
195 done
196 printf "\033[1;3mDone checking your \033[1;36m\$HOME.\033[1;0m\n"
197 printf "\n"
198 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"
199 printf "\n"
200 }
201
202 enumerate_programs