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