summaryrefslogtreecommitdiff
path: root/src/webhttrack
blob: dfb7dbaad42732d9a0423fe208a08d9c123fda2f (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
#!/bin/bash
#
# WebHTTrack launcher script
# Initializes the htsserver GUI frontend and launch the default browser

BROWSEREXE=
SRCHBROWSEREXE="x-www-browser www-browser iceape mozilla firefox icecat iceweasel abrowser firebird galeon konqueror opera chrome chromium chromium-browser netscape"
if test -n "${BROWSER}"; then
# sensible-browser will f up if BROWSER is not set
SRCHBROWSEREXE="xdg-open sensible-browser ${SRCHBROWSEREXE}"
fi
# Patch for Darwin/Mac by Ross Williams
if test "`uname -s`" == "Darwin"; then
# Darwin/Mac OS X uses a system 'open' command to find
# the default browser. The -W flag causes it to wait for
# the browser to exit
BROWSEREXE="/usr/bin/open -W"
fi
SRCHPATH="/usr/local/bin /usr/share/bin /usr/bin /usr/lib/httrack /usr/local/lib/httrack /usr/local/share/httrack /opt/local/bin /sw/bin ${HOME}/usr/bin ${HOME}/bin"
SRCHPATH="$SRCHPATH "`echo $PATH | tr ":" " "`
SRCHDISTPATH="/usr/share /usr/local /usr /local /usr/local/share ${HOME}/usr ${HOME}/usr/share /opt/local/share /sw ${HOME}/usr/local ${HOME}/usr/share"

###
# And now some famous cuisine

function log {
echo "$0($$): $@" >&2
return 0
}

function mozillabrowser {
# returns 0, if the browser is mozilla type
echo "$1" | grep -q "iceape"
[ $? -eq 0 ] && return 0 
echo "$1" | grep -q "mozilla"
[ $? -eq 0 ] && return 0 
echo "$1" | grep -q "netscape"
[ $? -eq 0 ] && return 0 
echo "$1" | grep -q "firebird"
[ $? -eq 0 ] && return 0 
echo "$1" | grep -q "firefox"
[ $? -eq 0 ] && return 0 
echo "$1" | grep -q "iceweasel"
[ $? -eq 0 ] && return 0 
echo "$1" | grep -q "abrowser"
[ $? -eq 0 ] && return 0 
echo "$1" | grep -q "icecat"
[ $? -eq 0 ] && return 0 
return 1;
}
function mozillaloaded  {
user_name=`logname 2>/dev/null`
if ! test -n "${user_name}"; then
user_name=`id -un`
fi
if test -n "${user_name}"; then
ps -e -U "$user_name" | grep -E "(iceape|mozilla|netscape|firebird|firefox)" | grep -qv "grep -E"
else
false
fi
}

function launch_browser {
log "launching $1"
start_t=`date +%s`
browser=$1
url=$2
moz=
if mozillaloaded; then
moz=1
fi
# launch any browser
# if it is a mozilla like browser, check if the browser is running and use 
# -remote if needed. Change the URL into openURL($url) too. 
# (thanks to Torsten Werner for the patch)
# see http://www.mozilla.org/unix/remote.html
# 04/2006: openurl() fix from Samuel Suther
if mozillabrowser ${browser}; then
	if ! ${browser} -remote "openurl(${url})"; then
		log "spawning browser.."
		${browser} "${url}"
	fi
else
	log "spawning regular browser.."
	${browser} "${url}"
fi
# this is a real pain in the neck: browser can hiddenly use the -remote feature of
# mozilla and therefore return immediately
# this loop is the only reliable solution AFAIK
end_t=`date +%s`
if test -n "$start_t" -a -n "$end_t"; then
	int_t=$[$end_t-$start_t]
else
	int_t=0
fi
if test -n "${int_t}" -a "${int_t}" -lt 60; then
	if test -n "$moz"; then
		log "waiting for browser to terminate.."
		while mozillaloaded; do
			sleep 3
		done
		log "browser seems to have been closed.."
	fi
fi
log "browser exited"
}

# First ensure that we can launch the server
BINPATH=
for i in ${SRCHPATH}; do
	! test -n "${BINPATH}" && test -x ${i}/htsserver && BINPATH=${i}
done
for i in ${SRCHDISTPATH}; do
	! test -n "${DISTPATH}" && test -f "${i}/httrack/lang.def" && DISTPATH="${i}/httrack"
done
test -n "${BINPATH}" || ! log "could not find htsserver" || exit 1
test -n "${DISTPATH}" || ! log "could not find httrack directory" || exit 1
test -f ${DISTPATH}/lang.def || ! log "could not find ${DISTPATH}/lang.def" || exit 1
test -f ${DISTPATH}/lang.indexes || ! log "could not find ${DISTPATH}/lang.indexes" || exit 1
test -d ${DISTPATH}/lang || ! log "could not find ${DISTPATH}/lang" || exit 1
test -d ${DISTPATH}/html || ! log "could not find ${DISTPATH}/html" || exit 1

# Locale
HTSLANG="${LC_MESSAGES}"
! test -n "${HTSLANG}" && HTSLANG="${LC_ALL}"
! test -n "${HTSLANG}" && HTSLANG="${LANG}"
test -n "${HTSLANG}" && HTSLANG="`echo ${HTSLANG} | cut -c1-2` | tr 'A-Z' 'a-z'"
LANGN=`grep "${HTSLANG}:" ${DISTPATH}/lang.indexes | cut -f2 -d':'`
! test -n "${LANGN}" && LANGN=1

# Find the browser
# note: not all systems have sensible-browser or www-browser alternative
# thefeore, we have to find a bit more if sensible-browser could not be found

for i in ${SRCHBROWSEREXE}; do
for j in ${SRCHPATH}; do
if test -x ${j}/${i}; then
BROWSEREXE=${j}/${i}
fi
test -n "$BROWSEREXE" && break
done
test -n "$BROWSEREXE" && break
done
test -n "$BROWSEREXE" || ! log "cound not find any suitable browser" || exit 1

# "browse" command
if test "$1" = "browse"; then
if test -f "${HOME}/.httrack.ini"; then
INDEXF=`cat ${HOME}/.httrack.ini | tr '\r' '\n' | grep -E "^path=" | cut -f2- -d'='`
if test -n "${INDEXF}" -a -d "${INDEXF}" -a -f "${INDEXF}/index.html"; then
INDEXF="${INDEXF}/index.html"
else
INDEXF=""
fi
fi
if ! test -n "$INDEXF"; then 
INDEXF="${HOME}/websites/index.html"
fi
launch_browser "${BROWSEREXE}" "file://${INDEXF}"
exit $?
fi

# Create a temporary filename
TMPSRVFILE="/tmp/.webhttrack.$$.`head -c16 /dev/random | md5sum | cut -f1 -d' '`"
>${TMPSRVFILE} || ! log "cound not create the temporary file ${TMPSRVFILE}" || exit 1
# Launch htsserver binary and setup the server
(${BINPATH}/htsserver "${DISTPATH}/" path "${HOME}/websites" lang "${LANGN}" $@; echo SRVURL=error) > ${TMPSRVFILE}&
# Find the generated SRVURL
SRVURL=
MAXCOUNT=60
while ! test -n "$SRVURL"; do
MAXCOUNT=$[$MAXCOUNT - 1]
test $MAXCOUNT -gt 0 || exit 1
test $MAXCOUNT -lt 50 && echo "waiting for server to reply.."
SRVURL=`grep -E URL= ${TMPSRVFILE} | cut -f2- -d=`
test ! "$SRVURL" = "error" || ! log "could not spawn htsserver" || exit 1
test -n "$SRVURL" || sleep 1
done

# Cleanup function
function cleanup {
test -n "$1" && log "nasty signal caught, cleaning up.."
test -f ${TMPSRVFILE} && SRVPID=`grep -E PID= ${TMPSRVFILE} | cut -f2- -d=`
test -n "${SRVPID}" && kill -9 ${SRVPID}
test -f ${TMPSRVFILE} && rm ${TMPSRVFILE}
test -n "$1" && log "..done"
return 0
}

# Cleanup in case of emergency
trap "cleanup now; exit" 1 2 3 4 5 6 7 8 9 11 13 14 15 16 19 24 25

# Got SRVURL, launch browser
launch_browser "${BROWSEREXE}" "${SRVURL}"

# That's all, folks!
trap "" 1 2 3 4 5 6 7 8 9 11 13 14 15 16 19 24 25
cleanup
exit 0