summaryrefslogtreecommitdiff
path: root/jh-checksource.sh
blob: 2f7969df09fd3fafb46183724e1a7dfc2486bd44 (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
#!/bin/bash

sep='<nofileevercontainsthis>'
resep='@'

safe_types_regexp=('text' '(GIF|JPEG|PNG) image data')
safe_types_string=()
safe_files_regexp=('.*/\.git/.*')
safe_files_string=()

normalize_filename() {
	local cwd="`pwd`"
	readlink -m -- "$1"|sed "s|^$cwd/|./|"
}

matches_string() {
	local needle=$1
	shift
	for straw in "$@"; do
		if [[ "$needle" = "$straw" ]]; then
			return 0
		fi
	done
	return 1
}

matches_regexp() {
	local needle=$1
	shift
	for straw in "$@"; do
		if [[ "$needle" =~ $straw ]]; then
			return 0
		fi
	done
	return 1
}

main() {
	# Parse arguments
	for file in "$@"; do safe_files_string+=("$(normalize_filename "$file")"); done

	# Init
	unsafe_files="$(mktemp)"

	# Heavy lifting
	find . -type f -exec file -F"$sep" {} + | while read line; do
		file="$(echo "$line"|sed "s${resep}${sep}.*${resep}${resep}")"
		type="$(echo "$line"|sed "s${resep}.*${sep}\s*${resep}${resep}")"

		file="$(normalize_filename "$file")"

		if \
			matches_string "$file" "${safe_files_string[@]}" || \
			matches_string "$type" "${safe_types_string[@]}" || \
			matches_regexp "$file" "${safe_files_regexp[@]}" || \
			matches_regexp "$type" "${safe_types_regexp[@]}" ; then
			: # do nothing
		else
			printf "%s\n" "$file"
		fi
	done > "$unsafe_files"

	if [[ -n "$(cat "$unsafe_files")" ]]; then
		echo "==> WARNING: The source directory `pwd` contains binary files:"
		<"$unsafe_files" sort | sed 's/./  -> &/'
		rm -f "$unsafe_files"
		exit 1
	else
		rm -f "$unsafe_files"
	fi
}

main "$@"