play.it 2.77 KB
Newer Older
1 2 3 4 5 6 7 8
#!/bin/sh -e

set -o errexit

INPUT_FILE="$1"

LIB_ONLY=true

9
: "${XDG_DATA_HOME:="$HOME/.local/share"}"
10

11
if [ -z "$PLAYIT_LIB2" ]; then
12
	for path in\
13 14 15 16 17 18 19
		"$PWD"\
		"$(dirname "$0")/play.it-2/lib"\
		"$XDG_DATA_HOME/play.it"\
		'/usr/local/share/games/play.it'\
		'/usr/local/share/play.it'\
		'/usr/share/games/play.it'\
		'/usr/share/play.it'
20
	do
21
		if [ -e "$path/libplayit2.sh" ]; then
22 23 24 25
			PLAYIT_LIB2="$path/libplayit2.sh"
			break
		fi
	done
26 27 28 29 30
fi
if [ -z "$PLAYIT_LIB2" ]; then
	printf '\n\033[1;31mError:\033[0m\n'
	printf 'libplayit2.sh not found.\n'
	exit 1
31
fi
32
# shellcheck source=play.it-2/lib/libplayit2.sh
33 34
. "$PLAYIT_LIB2"

35 36 37 38 39
if [ "$#" = 0 ]; then
	help
	exit 0
fi

40 41 42 43
if [ ! -f "$INPUT_FILE" ]; then
	print_error
	case "${LANG%_*}" in
		('fr')
44
			# shellcheck disable=SC1112
45 46 47 48 49 50
			string='%s n’est pas un fichier valide\n'
		;;
		('en'|*)
			string='%s is not a valid file\n'
		;;
	esac
51
	# shellcheck disable=SC2059
52
	printf "$string" "$INPUT_FILE"
53
	exit 1
54 55
fi

56 57 58 59
# Locate script by file name
FILE_NAME="${INPUT_FILE##*/}"
case "${LANG%_*}" in
	('fr')
60
		# shellcheck disable=SC1112
61 62 63 64 65 66
		string='Recherche d’un script par nom de fichier pour le fichier %s\n'
	;;
	('en'|*)
		string='Looking for a script by file name for file %s\n'
	;;
esac
67
# shellcheck disable=SC2059
68
printf "$string" "$FILE_NAME"
69 70 71 72 73 74 75 76 77
SCRIPT="$(find\
	"$(dirname "$0")/play.it-2/games"\
	"$XDG_DATA_HOME/play.it"\
	'/usr/local/share/games/play.it'\
	'/usr/local/share/play.it'\
	'/usr/share/games/play.it'\
	'/usr/share/play.it'\
	! -path '*/prefixes/*' -type f -name '*.sh' -exec\
	grep --regexp="ARCHIVE_.\\+='$FILE_NAME'" --files-with-matches '{}' + 2>/dev/null | head --lines=1)"
78 79 80 81 82

# Locate script by file MD5 checksum
if [ -z "$SCRIPT" ]; then
	case "${LANG%_*}" in
		('fr')
83
			# shellcheck disable=SC1112
84 85 86 87 88 89
			string='Recherche d’un script par somme de contrôle MD5 pour le fichier %s\n'
		;;
		('en'|*)
			string='Looking for a script by MD5 hash sum for file %s\n'
		;;
	esac
90
	# shellcheck disable=SC2059
91 92
	printf "$string" "$FILE_NAME"
	check_deps 'md5sum'
93
	FILE_MD5="$(md5sum "$INPUT_FILE" | cut --delimiter=' ' --fields=1)"
94 95 96 97 98 99 100 101 102
	SCRIPT="$(find\
		"$(dirname "$0")/play.it-2/games"\
		"$XDG_DATA_HOME/play.it"\
		'/usr/local/share/games/play.it'\
		'/usr/local/share/play.it'\
		'/usr/share/games/play.it'\
		'/usr/share/play.it'\
		! -path '*/prefixes/*' -type f -name '*.sh' -exec\
		grep --regexp="$FILE_MD5" --files-with-matches '{}' + 2>/dev/null | head --lines=1)"
103 104
	SCRIPT_OPTIONS='--checksum=none'
fi
105 106 107 108 109

if [ -z "$SCRIPT" ]; then
	print_error
	case "${LANG%_*}" in
		('fr')
110
			string='Impossible de trouver un script pour le fichier %s\n'
111 112
		;;
		('en'|*)
113
			string='Could not find script for file %s\n'
114 115
		;;
	esac
116
	# shellcheck disable=SC2059
117
	printf "$string" "$FILE_NAME"
118
	exit 1
119 120 121 122
fi

export PLAYIT_LIB2

123
shift 1
124
"$SCRIPT" "$INPUT_FILE" $SCRIPT_OPTIONS "$@"
125 126

exit 0