]> glassweightruler.freedombox.rocks Git - xdg-ninja.git/blob - xdg-ninja.sh
Add disclaimer about glow
[xdg-ninja.git] / xdg-ninja.sh
1 #!/bin/bash
2
3 USE_GLOW=true
4 if ! command -v glow &> /dev/null
5 then
6 echo "Glow not found, markdown rendering not available."
7 echo "Output will be raw markdown and might look weird."
8 echo "Install glow for easier reading & copy-paste."
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 echo -e "$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 ! command -v jq &> /dev/null
48 then
49 echo "jq is needed to run this script, but it wasn't found. Please install it to be able to use this script."
50 exit
51 fi
52
53 ERR=0
54 WARN=1
55 INFO=2
56 SUCS=3
57 HELP=4
58
59 # Function to expand enviornment variables in string
60 # https://stackoverflow.com/a/20316582/11110290
61 apply_shell_expansion() {
62 declare data="$1"
63 declare delimiter="__apply_shell_expansion_delimiter__"
64 declare command="cat <<$delimiter"$'\n'"$data"$'\n'"$delimiter"
65 eval "$command"
66 }
67
68 # Returns 0 if the path doesn't lead anywhere
69 # Return 1 if the path points to a file, 2 if it points to a directory
70 check_not_exists_file() {
71 FILE_PATH=$(apply_shell_expansion "$1")
72 if [ -f "$FILE_PATH" ]; then
73 return 1
74 elif [ -d "$FILE_PATH" ]; then
75 return 2
76 else
77 return 0
78 fi
79 }
80
81
82 # Function to handle the formatting of output
83 log() {
84 MODE="$1"
85 NAME="$2"
86 FILENAME="$3"
87 HELP="$4"
88
89 case "$MODE" in
90
91 ERR)
92 printf "[\e[1;31m$NAME\e[1;0m]: \e[1;3m$FILENAME\e[1;0m\n"
93 ;;
94
95 WARN)
96 printf "[\e[1;33m$NAME\e[1;0m]: \e[1;3m$FILENAME\e[1;0m\n"
97 ;;
98
99 INFO)
100 printf "[\e[1;36m$NAME\e[1;0m]: \e[1;3m$FILENAME\e[1;0m\n"
101 ;;
102
103 SUCS)
104 if [ "$SKIP_OK" = false ]; then
105 printf "[\e[1;32m$NAME\e[1;0m]: \e[1;3m$FILENAME\e[1;0m\n"
106 fi
107 ;;
108
109 HELP)
110 if $USE_GLOW; then
111 echo "$HELP" | glow -
112 else
113 echo "$HELP"
114 fi
115 ;;
116
117 esac
118 }
119
120 # Checks that the given file does not exist, otherwise outputs help
121 check_file() {
122 INPUT="$1"
123 NAME="$2"
124
125 FILENAME=$(echo -E "$INPUT" | jq -r .path)
126 MOVABLE=$(echo -E "$INPUT" | jq -r .movable)
127 HELP=$(echo -E "$INPUT" | jq -r .help)
128
129 check_not_exists_file "$FILENAME"
130
131 case $? in
132
133 0)
134 log SUCS "$NAME" "$FILENAME" "$HELP"
135 ;;
136
137 1 | 2)
138 if "$MOVABLE"; then
139 log ERR "$NAME" "$FILENAME" "$HELP"
140 else
141 log WARN "$NAME" "$FILENAME" "$HELP"
142 fi
143 if ! [ -z "$HELP" ]; then
144 log HELP "$NAME" "$FILENAME" "$HELP"
145 else
146 log HELP "$NAME" "$FILENAME" "_No help available._"
147 fi
148 ;;
149
150 esac
151 }
152
153 # Reads a file from programs/, calls check_file on each file specified for the program
154 check_program() {
155 INPUT=$1
156
157 NAME=$(echo "$INPUT" | jq -r .name)
158
159
160 while IFS= read -r file; do
161 check_file "$file" "$NAME"
162 done <<< "$(echo "$INPUT" | jq -rc '.files[]')"
163 }
164
165 # Loops over all files in the programs/ directory and calls check_program
166 enumerate_programs() {
167 echo -e "\e[1;3mStarting to check your \e[1;36m\$HOME.\e[1;0m"
168 echo -e ""
169 for prog_filename in ./programs/*; do
170 check_program "$(cat $prog_filename)"
171 done
172 echo -e "\e[1;3mDone checking your \e[1;36m\$HOME.\e[1;0m"
173 echo -e ""
174 echo -e "\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"
175 echo -e ""
176 }
177
178 enumerate_programs