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