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