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