diff options
Diffstat (limited to 'src/webhttrack')
-rwxr-xr-x | src/webhttrack | 89 |
1 files changed, 80 insertions, 9 deletions
diff --git a/src/webhttrack b/src/webhttrack index 3b433bb..ca3f512 100755 --- a/src/webhttrack +++ b/src/webhttrack @@ -2,14 +2,16 @@ # # WebHTTrack launcher script # Initializes the htsserver GUI frontend and launch the default browser + BROWSEREXE= -SRCHBROWSEREXE="x-www-browser www-browser mozilla galeon konqueror opera netscape" +SRCHBROWSEREXE="x-www-browser www-browser mozilla firefox firebird galeon konqueror opera netscape" if test -n "${BROWSER}"; then # sensible-browser will f up if BROWSER is not set SRCHBROWSEREXE="sensible-browser ${SRCHBROWSEREXE}" fi -SRCHPATH="/usr/local/bin /usr/share/bin /usr/bin /usr/lib/httrack /usr/local/lib/httrack /usr/local/share/httrack ${HOME}/usr/bin ${HOME}/bin" -SRCHDISTPATH="/usr/share /usr/local /usr /local /usr/local/share ${HOME}/usr ${HOME}/usr/share ${HOME}/usr/local ${HOME}/usr/share" +SRCHPATH="/usr/local/bin /usr/share/bin /usr/bin /usr/lib/httrack /usr/local/lib/httrack /usr/local/share/httrack /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 /sw ${HOME}/usr/local ${HOME}/usr/share" ### # And now some famous cuisine @@ -19,6 +21,74 @@ echo "$0($$): $@" >&2 return 0 } +function mozillabrowser { +# returns 0, if the browser is mozilla type +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 +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 --user "$user_name" | grep -qE "(mozilla|netscape|firebird|firefox)" +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 +if mozillabrowser ${browser}; then + if ! ${browser} -remote "${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 @@ -45,6 +115,7 @@ LANGN=`grep "${HTSLANG}:" ${DISTPATH}/lang.indexes | cut -f2 -d':'` # 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 @@ -58,12 +129,12 @@ test -n "$BROWSEREXE" || ! log "cound not find any suitable browser" || exit 1 # "browse" command if test "$1" = "browse"; then -${BROWSEREXE} "file://${HOME}/websites/index.html" +launch_browser "${BROWSEREXE}" "file://${HOME}/websites/index.html" exit $? fi # Create a temporary filename -TMPSRVFILE="/tmp/.webhttrack.$$.`/usr/bin/head -c16 /dev/random | /usr/bin/md5sum | /usr/bin/cut -f1 -d' '`" +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}& @@ -74,15 +145,15 @@ 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=`/bin/grep -E URL= ${TMPSRVFILE} | /usr/bin/cut -f2- -d=` +SRVURL=`grep -E URL= ${TMPSRVFILE} | cut -f2- -d=` test ! "$SRVURL" = "error" || ! log "could not spawn htsserver" || exit 1 -test -n "$SRVURL" || /bin/sleep 1 +test -n "$SRVURL" || sleep 1 done # Cleanup function function cleanup { test -n "$1" && log "nasty signal caught, cleaning up.." -test -f ${TMPSRVFILE} && SRVPID=`/bin/grep -E PID= ${TMPSRVFILE} | /usr/bin/cut -f2- -d=` +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" @@ -93,7 +164,7 @@ return 0 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 -${BROWSEREXE} "${SRVURL}" +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 |