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