xref: /aoo42x/main/shell/source/unix/misc/open-url.sh (revision cdf0e10c)
1#!/bin/sh
2
3# tries to locate the executable specified
4# as first parameter in the user's path.
5which() {
6  if [ ! -z "$1" ]; then
7    for i in `echo $PATH | sed -e 's/^:/.:/g' -e 's/:$/:./g' -e 's/::/:.:/g' -e 's/:/ /g'`; do
8      if [ -x "$i/$1" -a ! -d "$i/$1" ]; then
9        echo "$i/$1"
10        break;
11      fi
12    done
13  fi
14}
15
16# checks for the original mozilla start script(s)
17# and restrict the "-remote" semantics to those.
18run_mozilla() {
19  if file "$1" | grep "script" > /dev/null && grep "NPL" "$1" > /dev/null; then
20    "$1" -remote 'ping()' 2>/dev/null >/dev/null
21    if [ $? -eq 2 ]; then
22      "$1" "$2" &
23    else
24      "$1" -remote "openURL($2, new-window)" &
25    fi
26  else
27    "$1" "$2" &
28  fi
29}
30
31# checks the browser value for a %s as defined in
32# http://www.catb.org/~esr/BROWSER/index.html
33run_browser() {
34  echo "$1|$2" | awk '
35{
36    FS="|";
37    $syscmd="";
38    if (index($1,"%s") > 0) {
39        $syscmd=sprintf($1,$2);
40    } else {
41        $syscmd=sprintf("%s \"%s\"",$1,$2);
42    }
43    system($syscmd " &");
44}' > /dev/null
45}
46
47# special handling for mailto: uris
48if echo $1 | grep '^mailto:' > /dev/null; then
49  # check for xdg-email
50  mailer=`which xdg-email`
51  if [ ! -z "$mailer" ]; then
52    $mailer "$1" &
53    exit 0
54  fi
55  # check $MAILER variable
56  if [ ! -z "$MAILER" ]; then
57    $MAILER "$1" &
58    exit 0
59  fi
60  # mozilla derivates may need -remote semantics
61  for i in thunderbird mozilla netscape; do
62    mailer=`which $i`
63    if [ ! -z "$mailer" ]; then
64      run_mozilla "$mailer" "$1"
65      exit 0
66    fi
67  done
68  # handle all non mozilla mail clients below
69  # ..
70else
71  # check for xdg-open
72  browser=`which xdg-open`
73  if [ ! -z "$browser" ]; then
74    $browser "$1" &
75    exit 0
76  fi
77  # check $BROWSER variable
78  if [ ! -z "$BROWSER" ]; then
79    $BROWSER "$1" &
80    exit 0
81  fi
82  # mozilla derivates may need -remote semantics
83  for i in firefox mozilla netscape; do
84    browser=`which $i`
85    if [ ! -z "$browser" ]; then
86      run_mozilla "$browser" "$1"
87      exit 0
88    fi
89  done
90  # handle all non mozilla browers below
91  # ..
92fi
93exit 1
94