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