summarylogtreecommitdiffstats
path: root/tor-browser.sh
blob: 16eddc48ad9c415e4ecab273c6d5dc71902d55a6 (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
#!/usr/bin/env bash

# Copyright (C) 2009 Benjamin Drung <bdrung at ubuntu dot com>
# Copyright (C) 2012 Alessio Sergi <al3hex at gmail dot com>
# Copyright (C) 2017 grufo <madmurphy333 at gmail dot com> (Arch User Repository version)
# modified 2012 for tor-browser (Max Roder <maxroder at web dot de>)
# modified 2014 by Yardena Cohen <yardenack at gmail dot com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

set -e


# filled by PKGBUILD
_TB_PKGNAME_='__REPL_NAME__'
_TB_VERSION_='__REPL_VERSION__'
_TB_RELEASE_='__REPL_RELEASE__'
_TB_LANGUAGE_="__REPL_LANGUAGE__"
_TB_ARCH_='__REPL_ARCH__'

# other constants and variables
_TB_HOME_DIR_=~/".${_TB_PKGNAME_}"
_TB_APP_DIR_="${_TB_HOME_DIR_}/app"
_TB_VER_FILE_="${_TB_HOME_DIR_}/VERSION"
_TB_LOG_FILE_="${_TB_HOME_DIR_}/LOG"
_TB_REFRESH_=0


# syntax: _notify_ "${TITLE}" "${MESSAGE}"
_notify_() {

	if [ $(which zenity 2>/dev/null) ]; then
		zenity --info --title "${1}" --text "${2}"
	elif [ $(which notify-send 2>/dev/null) ]; then
		notify-send "${1}" "${2}"
	elif [ $(which kdialog 2>/dev/null) ]; then
		kdialog --title "${1}" --passivepopup "${2}"
	else
		echo -e "${0}: [${1}] ${2}" >&2
	fi

}


# syntax: _compare_ver_ "${INSTALLED_VERSION}" "${LATEST_VERSION}"
_compare_ver_() {

	[[ "${1}" == "${2}" ]] && return 1 || [[ "${1}" == "`echo -e "${1}\n${2}" | sort -V | head -n1`" ]]

}


_refresh_local_() {

	echo "${0}: Extracting files to ${_TB_APP_DIR_}." >> "${_TB_LOG_FILE_}"
	rm -rf "${_TB_APP_DIR_}"/*
	tar --strip-components=1 -xJf "/opt/${_TB_PKGNAME_}/tor-browser-${_TB_ARCH_}-${_TB_VERSION_}_${_TB_LANGUAGE_}.tar.xz" \
		-C "${_TB_APP_DIR_}" >> "${_TB_LOG_FILE_}" 2>&1 || _notify_ 'Error' \
		"The tor-browser archive could not be extracted to your home directory. \
		\nCheck permissions of ${_TB_APP_DIR_}. \
		\nThe error log can be found in ${_TB_LOG_FILE_}."

	[[ -f "${_TB_APP_DIR_}/Browser/start-tor-browser" ]] && echo "${_TB_VERSION_}" > "${_TB_VER_FILE_}"

}


_aur_update_() {

	local DO_UPDATE=0

	if [[ "$(id -u)" == '0' ]]; then
		echo 'It is not a good idea to do this as root. Abort.' 1>&2
		exit 1
	fi

	local TMP_PKGBUILD="$(mktemp -d)"

	cd "${TMP_PKGBUILD}"

	if ! { curl --silent --fail "https://aur.archlinux.org/cgit/aur.git/snapshot/${_TB_PKGNAME_}.tar.gz" | tar xz ;} 2>/dev/null; then
		echo 'Unable to retrieve the PKGBUILD. Abort.'
		rm -rf "${TMP_PKGBUILD}"
		exit 1
	fi

	cd "${TMP_PKGBUILD}/${_TB_PKGNAME_}"

	local AUR_VERSION="$(grep 'pkgver' '.SRCINFO' | cut -d = -f2 | sed -e 's/^[[:space:]]*//')"
	local AUR_RELEASE="$(grep 'pkgrel' '.SRCINFO' | cut -d = -f2 | sed -e 's/^[[:space:]]*//')"

	if _compare_ver_ "${_TB_VERSION_}" "${AUR_VERSION}"; then
		echo "Found new version (${AUR_VERSION})..."
		local DO_UPDATE=1
	elif [[ "${_TB_VERSION_}" == "${AUR_VERSION}" ]] && [[ "${_TB_RELEASE_}" != "${AUR_RELEASE}" ]] && [[ "${_TB_RELEASE_}" == "`echo -e "${_TB_RELEASE_}\n${AUR_RELEASE}" | sort | head -n1`" ]]; then
		echo 'Found new PKGBUILD...'
		local DO_UPDATE=1
	else
		echo "Everything is up to date (current version: ${_TB_VERSION_})."
	fi

	[[ ${DO_UPDATE} -eq 1 ]] && makepkg -si

	rm -rf "${TMP_PKGBUILD}"

}


_usage_() {

	cat <<EOF
Usage: ${0##*/} [option]

Options:
  -h|--help         Show this help message and exit
  -u|--update       Search in AUR for a new release and install it
  -f|--refresh      Force refresh of the copy in your home directory
  --dir=<directory> The Tor-Browser directory to use

  All unrecognized arguments will be passed to the browser.
EOF

}


# remove old INSTALL and APP directories (temporary command, to be removed in the next versions)
if [[ -d "${_TB_HOME_DIR_}/INSTALL" ]] || [[ -d "${_TB_HOME_DIR_}/APP" ]]; then
	rm -rf "${_TB_HOME_DIR_}"
fi

args=()
for arg; do
	case "${arg}" in
		-h|--help) _usage_; exit 0 ;;
		-u|--update) _aur_update_; exit 0 ;;
		-f|--refresh) _TB_REFRESH_=1 ;;
		--dir=*) _TB_HOME_DIR_="${arg#*=}" ;;
		*) args+=("$arg") ;;
	esac
done

# create directory, if it is missing (e.g. first run)
[[ ! -d "${_TB_APP_DIR_}" ]] && mkdir -p "${_TB_APP_DIR_}"
cd "${_TB_HOME_DIR_}"

# create version file if missing
[[ ! -f "${_TB_VER_FILE_}" ]] && echo 0 > "${_TB_VER_FILE_}"

# get the installed version
while read _TB_VER_LINE_
do
	_TB_INSTALLED_VERSION_="${_TB_VER_LINE_}"
done < "${_TB_VER_FILE_}"

# start update if old or no tor-browser is installed
if [[ "${_TB_INSTALLED_VERSION_}" == "${_TB_VERSION_}" ]] && [[ ${_TB_REFRESH_} -eq 0 ]]; then
	# clear log
	> "${_TB_LOG_FILE_}"
else
	echo "${0}: Your version in ${_TB_HOME_DIR_} is outdated or you do not have installed ${_TB_PKGNAME_} yet." > "${_TB_LOG_FILE_}"
	_refresh_local_
fi

# start tor-browser
"${_TB_APP_DIR_}/Browser/start-tor-browser" "${args[@]}"