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