c63bbb7f14b785b1bbb9761c98f60c1f21f2b919
[amiga-uae-virus-scan.git] / amiga-uae-virus-scan.sh
1 #!/bin/bash
2
3 # Ensure we have something to scan
4 if [ $# -lt 1 ]
5 then
6         echo "Usage: $0 floppy-to-test.adf"
7         echo "Usage: $0 directory-to-scan"
8         exit 1
9 fi
10
11
12 # Ensure we have all native tools
13 if [ -z "$(which curl)" -o \
14      -z "$(which lha)" -o \
15      -z "$(which fs-uae)" -o \
16      -z "$(which sha256sum)" ]
17 then
18         echo -e "\033[1m\033[31mERROR:\033[0m"
19         echo "This script requires the following programs to be in $PATH:"
20         echo "  curl"
21         echo "  lha"
22         echo "  fs-uae"
23         echo "  sha256sum"
24         exit 1
25 fi
26
27
28 # Directories
29 CWD="$PWD"
30 INSTALLERS="$CWD/installers"
31 DHDIR=dh0
32
33
34 # Alias for curl
35 CURL="curl --progress-bar --location -o"
36
37
38
39
40 # Provide a way to bail.
41 # This does NOT clean up a running emulation session.
42 function int_func()
43 {
44         tset
45         echo "$0: Interrupt."
46         exit 1
47 }
48 trap int_func INT
49
50
51 function colorprint()
52 {
53         echo -en "\033[1m\033[3$1m"
54         shift
55         echo "$@"
56         echo -en "\033[0m"
57 }
58 trap int_func INT
59
60
61
62 # Download any packages we don't have yet
63 function download_installers()
64 {
65         colorprint 3 "Downloading missing components..."
66
67         mkdir -p "$INSTALLERS"
68
69         for n in $(cat installer_urls)
70         do
71                 LHANAME=${n##*/}
72                 TXTNAME=${LHANAME%.lha}.readme
73
74                 if [ ! -e "$INSTALLERS/$LHANAME" ]
75                 then
76                         echo -e "\033[1m\033[34mDownloading\033[0m $LHANAME ..."
77                         $CURL "$INSTALLERS/$LHANAME" "$n"
78                 fi
79
80                 if [ ! -e "$INSTALLERS/$TXTNAME" ]
81                 then
82                         echo -e "\033[1m\033[34mDownloading\033[0m $TXTNAME ..."
83                         $CURL "$INSTALLERS/$TXTNAME" "${n%.lha}.readme"
84                 fi
85         done
86
87         colorprint 2 "...done"
88         echo
89 }
90
91
92
93 # Checksum all files we get from Aminet
94 function verify_installers()
95 {
96         colorprint 3 "Checking integrity..."
97
98         if ! sha256sum --strict --quiet --check sha256sums
99         then
100                 echo
101                 echo -e "\033[1m\033[31mERROR:\033[0m"
102                 echo "Integrity check failed."
103                 echo "Try removing the broken file and re-downloading it."
104                 echo "If you just downloaded these files, then maybe Aminet is currently having problems serving those files?"
105                 echo
106                 exit 1
107         fi
108
109         colorprint 2 "...done"
110         echo
111 }
112
113
114
115 # This prepares a folder to be a virtual partition,
116 # with an Amiga system ready to run and scan a directory.
117 function populate_dh0()
118 {
119         mkdir -p "$TD/$DHDIR"
120
121
122         # Programs
123         mkdir -p "$TD/$DHDIR/C"
124         lha xqiw="$TD/$DHDIR/C" "$INSTALLERS"/CheckX.lha CheckX/CheckX
125         lha xqiw="$TD/$DHDIR/C" "$INSTALLERS"/UAEquit.lha UAEquit
126
127
128         # Libraries
129         mkdir -p "$TD/$DHDIR/Libs"
130
131         mkdir -p "$TD/$DHDIR/Libs/xad"
132         lha xqiw="$TD/$DHDIR/Libs" "$INSTALLERS"/xadmaster000.lha xad/Libs/xadmaster.library
133         lha xqiw="$TD/$DHDIR/Libs/xad" "$INSTALLERS"/xadmaster000.lha xad/Libs/xad/*
134
135         mkdir -p "$TD/$DHDIR/Libs/xfd"
136         lha xqiw="$TD/$DHDIR/Libs" "$INSTALLERS"/xfdmaster.lha xfd_User/Libs/xfdmaster.library
137         lha xqiw="$TD/$DHDIR/Libs/xfd" "$INSTALLERS"/xfdmaster.lha xfd_User/Libs/xfd/*
138
139         lha xqiw="$TD/$DHDIR/Libs" "$INSTALLERS"/xvslibrary.lha xvs/libs/xvs.library
140
141
142         # Startup-sequence, config, registration keys
143         mkdir -p "$TD/$DHDIR/S"
144         lha xqiw="$TD/$DHDIR/S" "$INSTALLERS"/xadmaster-key.lha xadmaster.key
145
146         cat > "$TD/$DHDIR/S/startup-sequence" <<-EOF
147                 CheckX ALL FROM SYS:scandir LOG SYS:checkx-scandir.log
148                 UAEquit
149         EOF
150 }
151
152
153
154 function write_uae_config()
155 {
156         # Write main UAE config
157         cat > "$TD/amiga-virus-scan.fs-uae" <<-EOF
158                 [fs-uae]
159                 amiga_model = A4000
160                 automatic_input_grab = 0
161                 end_config = 1
162                 expect_version = 2.8.0
163                 floppies_dir = ./
164                 #floppy_drive_1 = ./df1.adf
165                 fullscreen = 0
166                 hard_drive_0 = ./$DHDIR
167                 initial_input_grab = 0
168                 jit_compiler = 1
169                 joystick_port_0 = Mouse
170                 joystick_port_0_mode = mouse
171                 joystick_port_1 = none
172                 joystick_port_1_mode = nothing
173                 joystick_port_2 = none
174                 joystick_port_2_mode = none
175                 joystick_port_3 = none
176                 joystick_port_3_mode = none
177                 keep_aspect = 1
178                 maximized = 0
179                 save_disk = 0
180                 uae_sound_output = none
181                 video_sync = 1
182                 window_hidden = 1
183                 zoom = full
184         EOF
185
186
187         # Prevent FS-UAE from printing audio errors
188         cat > "$TD"/.alsoftrc <<-EOF
189                 [general]
190                 drivers = null
191         EOF
192 }
193
194
195
196
197 #
198 #
199 # Main script
200 #
201
202 colorprint 7 " *******************************************"
203 colorprint 7 "   Amiga virus scanner wrapper starting up"
204 colorprint 7 " *******************************************"
205 echo
206
207
208 # Get external Amiga software
209 download_installers
210 verify_installers
211
212
213 # Prepare emulation environment
214 colorprint 3 "Preparing emulation environment..."
215
216 TD="$(mktemp --directory)"
217 populate_dh0
218 write_uae_config
219
220 # Copy files to scan
221 colorprint 7 "Scan target: $1 (will be copied to emulation first)"
222
223 cp -r "$1" "$TD/$DHDIR/scandir"
224 chmod -R u+rwX "$TD/$DHDIR/scandir"
225
226 colorprint 2 "...done"
227 echo
228
229
230 # Run emulator
231 colorprint 3 "Running emulator..."
232 cd "$TD"
233 export HOME="$TD"
234 fs-uae amiga-virus-scan.fs-uae > /dev/null
235
236 colorprint 2 "...done"
237 echo
238
239
240 # Print results
241 echo
242 echo
243 colorprint 2 "Scan results:"
244 echo
245 cat ./dh0/checkx-scandir.log
246
247
248 # Clean up
249 cd ..
250 rm -rf "$TD"
251
252 echo