summaryrefslogtreecommitdiff
path: root/unrar-emulator/unrar.sh
blob: 3dfd2fe16a7b40cd4e94885e886353a5fe6b812f (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
#!/bin/bash

# UNRAR-Emulator, script to emulate nonfree unrar binary; run with bash, libarchive-tar and unar
# Copyright (C) 2015  Márcio Silva <coadde@parabola.nu>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

unrar-emulator_usage() {
	echo 'UNRAR-Emulator v1.0 Free Software      Copyright (C) Márcio Silva'
	echo 'Script to emulate nonfree unrar binary; run with bash, libarchive-tar and unar'
	echo ''
	echo 'Usage: unrar <command> [-<switch1> -<switch2> ...] <archive> [...] <path_to_extract>'
	echo ''
	echo '<Commands>'
	echo '  e            Extract files without create a containing directory'
	echo '  l[b],v[b]    List archive contents [bare]'
	echo '  x            Extract files'
	echo ''
	echo '<Switches>'
	echo '  inul         Disable all messages'
	echo '  o[+|-]       Set the overwrite mode'
	echo '  or           Rename directories automatically'
	echo '  p[password]  Set password'
	exit 0
}

command="${1}"
parameters=("${@:2}")

for parameter in "${parameters[@]}"; do
	case "${parameter}" in
	-inul)
		verbose='no'
		unar_parameters+=('-q')
		;;
	-o+)
		unar_parameters+=('-f')
		;;
	-o-)
		tar_parameters+=('-k')
		unar_parameters+=('-s')
		;;
	-or)
		unar_parameters+=('-r')
		;;
	-p*)
		password="${parameter/-p/}"
		;;
	-*)
		echo "ERROR: Unknown option: ${parameter/-/}"
		exit 1
		;;
	*)
		if [ -z "${archive}" ]; then
			archive="${parameter}"
		elif [ -n "${archive}" ]; then
			directory="${parameter}"
		fi
		;;
	esac
done

if [ -z "${command}" ] || [ -z "${parameters}" ] || [ -z "${archive}" ]; then
	unrar-emulator_usage
fi

if [ "$command" == 'x' ]; then
	if [ -z "${password}" ]; then
		if [ -z "${directory}" ]; then
			unar "${unar_parameters[@]}" -k skip "${archive}"
		elif [ -n "${directory}" ]; then
			unar "${unar_parameters[@]}" -k skip -o "${directory}" "${archive}"
		fi
	elif [ -n "${password}" ]; then
		if [ -z "${directory}" ]; then
			unar "${unar_parameters[@]}" -k skip -p "${password}" "${archive}"
		elif [ -n "${directory}" ]; then
			unar "${unar_parameters[@]}" -k skip -p "${password}" -o "${directory}" "${archive}"
		fi
	fi
elif [ "${command}" == 'e' ]; then
	if [ -z "${password}" ]; then
		if [ -z "${directory}" ]; then
			unar -D "${unar_parameters[@]}" -k skip "${archive}"
		elif [ -n "${directory}" ]; then
			unar -D "${unar_parameters[@]}" -k skip -o "${directory}" "${archive}"
		fi
	elif [ -n "${password}" ]; then
		if [ -z "${directory}" ]; then
			unar -D "${unar_parameters[@]}" -k skip -p "${password}" "${archive}"
		elif [ -n "${directory}" ]; then
			unar -D "${unar_parameters[@]}" -k skip -p "${password}" -o "${directory}" "${archive}"
		fi
	fi
elif [ "${command}" == 'l' ] || [ "${command}" == 'lt' ] || [ "${command}" == 'lta' ] || [ "${command}" == 'v' ] || [ "${command}" == 'vt' ] || [ "${command}" == 'vta' ]; then
	if [ "${verbose}" != 'no' ]; then
		bsdtar tv "${tar_parameters[@]}" -f "${archive}"
	else
		exit 0
	fi
elif [ "${command}" == 'lb' ] || [ "${command}" == 'vb' ]; then
	if [ "${verbose}" != 'no' ]; then
		bsdtar t "${tar_parameters[@]}" -f "${archive}"
	else
		exit 0
	fi
else
	unrar-emulator_usage
fi