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