summaryrefslogtreecommitdiff
path: root/amiga-uae-virus-scan.sh
blob: c76b6b886a75b1fbcb8ac39e50cd77b32f9f422f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/bin/bash

# Ensure we have something to scan
if [ $# -lt 1 ]
then
	echo "Usage: $0 floppy-to-test.adf"
	echo "Usage: $0 directory-to-scan"
	exit 1
fi


# Ensure we have all native tools
if [ -z "$(which curl)" -o \
     -z "$(which lha)" -o \
     -z "$(which fs-uae)" -o \
     -z "$(which sha256sum)" ]
then
	echo -e "\033[1m\033[31mERROR:\033[0m"
	echo "This script requires the following programs to be in $PATH:"
	echo "  curl"
	echo "  lha"
	echo "  fs-uae"
	echo "  sha256sum"
	exit 1
fi


# Directories
CWD="$PWD"
INSTALLERS="$CWD/installers"
DHDIR=dh0


# Alias for curl
CURL="curl --progress-bar --location -o"




# Provide a way to bail.
# This does NOT clean up a running emulation session.
function int_func()
{
	tset
	echo "$0: Interrupt."
	exit 1
}
trap int_func INT


function colorprint()
{
	echo -en "\033[1m\033[3$1m"
	shift
	echo "$@"
	echo -en "\033[0m"
}
trap int_func INT



# Download any packages we don't have yet
function download_installers()
{
	colorprint 3 "Downloading missing components..."

	mkdir -p "$INSTALLERS"

	for n in $(cat installer_urls)
	do
		LHANAME=${n##*/}
		TXTNAME=${LHANAME%.lha}.readme

		if [ ! -e "$INSTALLERS/$LHANAME" ]
		then
			echo -e "\033[1m\033[34mDownloading\033[0m $LHANAME ..."
			$CURL "$INSTALLERS/$LHANAME" "$n"
		fi

		if [ ! -e "$INSTALLERS/$TXTNAME" ]
		then
			echo -e "\033[1m\033[34mDownloading\033[0m $TXTNAME ..."
			$CURL "$INSTALLERS/$TXTNAME" "${n%.lha}.readme"
		fi
	done

	colorprint 2 "...done"
	echo
}



# Checksum all files we get from Aminet
function verify_installers()
{
	colorprint 3 "Checking integrity..."

	if ! sha256sum --strict --quiet --check sha256sums
	then
		echo
		echo -e "\033[1m\033[31mERROR:\033[0m"
		echo "Integrity check failed."
		echo "Try removing the broken file and re-downloading it."
		echo "If you just downloaded these files, then maybe Aminet is currently having problems serving those files?"
		echo
		exit 1
	fi

	colorprint 2 "...done"
	echo
}



# This prepares a folder to be a virtual partition,
# with an Amiga system ready to run and scan a directory.
function populate_dh0()
{
	mkdir -p "$TD/$DHDIR"


	# Programs
	mkdir -p "$TD/$DHDIR/C"
	lha xqiw="$TD/$DHDIR/C" "$INSTALLERS"/CheckX.lha CheckX/CheckX
	lha xqiw="$TD/$DHDIR/C" "$INSTALLERS"/UAEquit.lha UAEquit


	# Libraries
	mkdir -p "$TD/$DHDIR/Libs"

	mkdir -p "$TD/$DHDIR/Libs/xad"
	lha xqiw="$TD/$DHDIR/Libs" "$INSTALLERS"/xadmaster000.lha xad/Libs/xadmaster.library
	lha xqiw="$TD/$DHDIR/Libs/xad" "$INSTALLERS"/xadmaster000.lha xad/Libs/xad/*

	mkdir -p "$TD/$DHDIR/Libs/xfd"
	lha xqiw="$TD/$DHDIR/Libs" "$INSTALLERS"/xfdmaster.lha xfd_User/Libs/xfdmaster.library
	lha xqiw="$TD/$DHDIR/Libs/xfd" "$INSTALLERS"/xfdmaster.lha xfd_User/Libs/xfd/*

	lha xqiw="$TD/$DHDIR/Libs" "$INSTALLERS"/xvslibrary.lha xvs/libs/xvs.library


	# Startup-sequence, config, registration keys
	mkdir -p "$TD/$DHDIR/S"
	lha xqiw="$TD/$DHDIR/S" "$INSTALLERS"/xadmaster-key.lha xadmaster.key

	cat > "$TD/$DHDIR/S/startup-sequence" <<-EOF
		;CheckX ALL FROM DF1: LOG SYS:checkx-scandir.log
		CheckX ALL FROM SYS:scandir LOG SYS:checkx-scandir.log
		UAEquit
	EOF
}



function write_uae_config()
{
	# Write main UAE config
	cat > "$TD/amiga-virus-scan.fs-uae" <<-EOF
		[fs-uae]
		amiga_model = A4000
		zorro_iii_memory = 1048576
		automatic_input_grab = 0
		end_config = 1
		expect_version = 2.8.0
		floppies_dir = ./
		#floppy_drive_1 = ./df1.adf
		fullscreen = 0
		governor_warning = 0
		hard_drive_0 = ./$DHDIR
		initial_input_grab = 0
		jit_compiler = 1
		joystick_port_0 = Mouse
		joystick_port_0_mode = mouse
		joystick_port_1 = none
		joystick_port_1_mode = nothing
		joystick_port_2 = none
		joystick_port_2_mode = none
		joystick_port_3 = none
		joystick_port_3_mode = none
		keep_aspect = 1
		maximized = 0
		save_disk = 0
		uae_sound_output = none
		video_sync = 1
		window_hidden = 0
		zoom = full
	EOF


	# Prevent FS-UAE from printing audio errors
	cat > "$TD"/.alsoftrc <<-EOF
		[general]
		drivers = null
	EOF
}




#
#
# Main script
#

colorprint 7 " *******************************************"
colorprint 7 "   Amiga virus scanner wrapper starting up"
colorprint 7 " *******************************************"
echo


# Get external Amiga software
download_installers
verify_installers


# Prepare emulation environment
colorprint 3 "Preparing emulation environment..."

TD="$(mktemp --directory)"
populate_dh0
write_uae_config

# Copy files to scan
colorprint 7 "Scan target: $1 (will be copied to emulation first)"

cp -r "$1" "$TD/$DHDIR/scandir"
chmod -R u+rwX "$TD/$DHDIR/scandir"

colorprint 2 "...done"
echo


# Run emulator
colorprint 3 "Running emulator..."
cd "$TD"
export HOME="$TD"
fs-uae amiga-virus-scan.fs-uae > /dev/null

colorprint 2 "...done"
echo


# Print results
echo
echo
colorprint 2 "Scan results:"
echo
cat ./dh0/checkx-scandir.log


# Clean up
cd ..
rm -rf "$TD"

echo