1#! /bin/sh
2
3
4test_pdf_viewer () {
5    if [ "$VIEWER" = "" ]; then
6	for DIR in `echo $PATH | sed 's/:/ /g'`; do
7	    if [ -f "$DIR/$1" ]; then
8		VIEWER=$1
9	    fi
10	done
11    fi
12}
13
14open_pdf_viewer () {
15
16    # adapted from IDL's online_help_pdf (May 2010)
17    # added gv
18    # added evince 2012-02-29
19    this_code=open_pdf_viewer
20
21
22# If viewer is not specified by the environment variable, choose a default
23#    VIEWER=$IDL_ONLINE_HELP_PDF_VIEWER
24    VIEWER=$HEALPIX_PDF_VIEWER
25    if [ \( "$VIEWER" = "" \) -a \( `uname` = "Darwin" \) ]; then
26	VIEWER=darwin_open
27    fi
28
29    # try general purpose file opener (Linux)
30    test_pdf_viewer "xdg-open"
31
32    # try general purpose file opener (Gnome)
33    test_pdf_viewer "gnome-open"
34
35    # If we see Adobe's acroread, we'll default to it.
36    test_pdf_viewer "acroread"
37
38    # If we didn't find acroread, then try the free Xpdf viewer
39    test_pdf_viewer "xpdf"
40
41    # fall back on evince # (Gnome)
42    test_pdf_viewer "evince"
43
44    # fall back on okular # (KDE)
45    test_pdf_viewer "okular"
46
47    # fall back on foxit #
48    test_pdf_viewer "foxit"
49
50    # fall back on sumatra (Windows)
51    test_pdf_viewer "sumatra"
52
53    # fall back on gv
54    test_pdf_viewer "gv"
55
56    if [ "x$VIEWER" = "x" ]; then
57    # No view in evidence.
58	echo "PDF viewer (xdg-open, gnome-open, acroread, xpdf, evince, okular, foxit, sumatra, gv) not found in user's PATH" 1>&2
59	aboutCustomization
60	exit 1
61    fi
62
63    case $VIEWER in
64	darwin_open)
65        open $1 &
66	;;
67
68	xdg-open*|gnome-open*)
69	$VIEWER $1 > /dev/null 2>&1  &
70	;;
71
72	acroread*|xpdf*|evince*|okular*|foxit*|gv*)
73	$VIEWER $1 &
74	;;
75
76	sumatra*)
77	$VIEWER $1 &
78	;;
79
80	*)
81	$VIEWER $1 &
82	;;
83
84    esac
85
86}
87
88# ======================================================================
89
90test_html_browser () {
91    if [ "$BROWSER" = "" ]; then
92	found=`which $1 2> /dev/null`
93	[ "$found" ] && BROWSER=$1
94    fi
95
96}
97
98open_html_browser () {
99    # adapted from IDL's online_help_html (May 2010)
100
101# If browser is not specified by the environment variable, then use the default
102BROWSER=$HEALPIX_HTML_BROWSER
103    if [ "$BROWSER" = "" ]; then
104	if [ `uname` = "Darwin" ]; then
105	    BROWSER=darwin_open
106	else
107	    test_html_browser "xdg-open"
108	    test_html_browser "gnome-open"
109	    test_html_browser "firefox"
110	    test_html_browser "mozilla"
111	    test_html_browser "netscape"
112	    test_html_browser "opera"
113	    test_html_browser "lynx"
114	    if [ "x$BROWSER" = "x" ]; then
115    # No browser in evidence.
116		echo "HMTL browser (xdg-open, gnome-open, firefox, mozilla, netscape, opera, lynx) not found in user's PATH" 1>&2
117		aboutCustomization
118		exit 1
119	    fi # empty
120	fi # Darwin
121    fi # $BROWSER
122
123
124    case "$BROWSER" in
125
126	"darwin_open")
127        href="\"file://$1\""
128	osascript -e  'open location '$href
129	;;
130
131	"xdg-open"|"gnome-open")
132	$BROWSER $1 > /dev/null 2>&1  &
133	;;
134
135	"lynx")
136	xterm -e lynx "$1" &
137	;;
138
139	"mozilla"|"firefox")
140	$BROWSER -remote "openURL(file:$1, new-tab)" 2>/dev/null # try to open tab in existing session
141	if [ $? != 0 ]; then # if it fails, start a fresh session
142	    $BROWSER "file:$1" &
143	fi
144	;;
145
146	"netscape")
147	$BROWSER -remote "openURL(file:$1)" 2>/dev/null
148	if [ $? != 0 ]; then
149	    $BROWSER "$1" &
150	fi
151	;;
152
153	"opera")
154	$BROWSER -remote "openURL(file:$1)" & # open existing or new session
155	;;
156
157	*)
158	$BROWSER "$1"
159	;;
160
161    esac
162
163}
164
165# ======================================================================
166aboutCustomization(){
167    echo ""
168    echo "Be default, $0 will look for a PDF viewer and HTML browser among the standard ones,"
169    echo "unless the variables \$HEALPIX_PDF_VIEWER and \$HEALPIX_HTML_BROWSER are set by the user."
170    echo "Currently: "
171    echo "HEALPIX_PDF_VIEWER = $HEALPIX_PDF_VIEWER"
172    echo "HEALPIX_HTML_BROWSER = $HEALPIX_HTML_BROWSER"
173    echo ""
174}
175# ======================================================================
176testDocLocation(){
177    docdir=""
178
179    # try $HEALPIXDOC (custom installation)
180    [ "x${HEALPIXDOC}" != "x" ] && docdir=${HEALPIXDOC}
181
182    # try $HEALPIX/doc (standard installation)
183    if [ "x${docdir}" = "x" ]; then
184	if [ "x${HEALPIX}" = "x" ]; then
185	    echo ""
186	    echo "WARNING: \$HEALPIX variable not set"
187	    echo "         It must be defined before running Healpix"
188	    echo ""
189	else
190	    docdir=${HEALPIX}/doc
191	fi
192    fi
193
194    # try around current script (standard, but unfinished, installation)
195    if [ ! -d "${docdir}"    ]; then
196	thiscode=$0
197	# poor man's emulation of recursive 'readlink -f'
198	[ -L "${thiscode}" ] && thiscode=$( readlink ${thiscode} ) # follow link, if applicable
199	[ -L "${thiscode}" ] && thiscode=$( readlink ${thiscode} ) # follow link again
200	thispath=$( dirname ${thiscode} )
201	topdir=$( cd ${thispath} && pwd )
202	docdir=${topdir}/doc
203    fi
204
205    # try current directory (desperate)
206    [ ! -d ${docdir} ] && docdir=./doc
207
208    # test final value of $docdir
209    if [ ! -d ${docdir} ]; then
210	echo "Can not find local HEALPIX documentation"
211	echo "(expected to be in \${HEALPIX}/doc/ or in \${HEALPIXDOC}/)"
212	exit 1
213# 	echo "Will try to use remote HEALPIX documentation"
214# 	docdir="http://healpix.sourceforge.net/doc"
215    fi
216}
217# ======================================================================
218
219verbose=0 # set to 1 to know which tool is used
220
221# find documentation location
222testDocLocation
223
224file=""
225#[ ${#} -eq 1 ] && file=$1
226[ ${#} -ge 2 ] && file=$2
227
228type=0
229while [ ${type} -le 0 ]; do
230    if [ ${#} = 0 -o ${type} = -1 ]; then
231	echo " Healpix on line documentation "
232	echo
233	echo " healpix_doc [-h|-p] "
234	echo "   -h:  browse HTML documentation "
235	echo "   -p:  view   PDF  documentation "
236	echo
237	printf " Enter your choice:  h, p, ? (getting help) or 0 (to exit): "
238	read answer
239	if [ "x$answer" = "x0" ]; then
240	    echo "Bye ..."
241	    exit 0
242	fi
243	[ "x$answer" = "xH" -o "x$answer" = "xh" ] && type=1
244	[ "x$answer" = "xP" -o "x$answer" = "xp" ] && type=2
245	if [ "x$answer" = "x?" ]; then
246	    aboutCustomization
247	    exit 0
248	fi
249	if [ $type -le 0 ] ; then
250	    echo "unkwnown answer ..."
251	    exit 1
252	fi
253    else
254	type=-1
255	[ "${1:0:2}" = "-h" ] && type=1
256	[ "${1:0:2}" = "-p" ] && type=2
257    fi
258done
259##echo $file
260
261
262case $type in
263    1)
264	echo "opening HEALPIX HTML documention"
265	deffile=${docdir}/html/main.htm
266	[ "x$file" != "x" ] && deffile=${file}
267	open_html_browser  ${deffile}
268	[ ${verbose} -eq 1 ] && echo "browser: ${BROWSER}"
269    ;;
270    2)
271	echo "opening HEALPIX PDF documention"
272	deffile=${docdir}/pdf/pdf_index.pdf
273	[ "x$file" != "x" ] && deffile=${file}
274	open_pdf_viewer    ${deffile}
275	[ ${verbose} -eq 1 ] && echo "viewer: ${VIEWER}"
276    ;;
277    *)
278    echo "Unknown option"
279    exit 1
280    ;;
281esac
282
283exit 0
284
285
286
287