xref: /trunk/main/shell/source/unix/misc/open-url.sh (revision 9f22d7c2)
1#!/bin/sh
2# *************************************************************
3#
4#  Licensed to the Apache Software Foundation (ASF) under one
5#  or more contributor license agreements.  See the NOTICE file
6#  distributed with this work for additional information
7#  regarding copyright ownership.  The ASF licenses this file
8#  to you under the Apache License, Version 2.0 (the
9#  "License"); you may not use this file except in compliance
10#  with the License.  You may obtain a copy of the License at
11#
12#    http://www.apache.org/licenses/LICENSE-2.0
13#
14#  Unless required by applicable law or agreed to in writing,
15#  software distributed under the License is distributed on an
16#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17#  KIND, either express or implied.  See the License for the
18#  specific language governing permissions and limitations
19#  under the License.
20#
21# *************************************************************
22
23# tries to locate the executable specified
24# as first parameter in the user's path.
25which() {
26  if [ ! -z "$1" ]; then
27    for i in `echo $PATH | sed -e 's/^:/.:/g' -e 's/:$/:./g' -e 's/::/:.:/g' -e 's/:/ /g'`; do
28      if [ -x "$i/$1" -a ! -d "$i/$1" ]; then
29        echo "$i/$1"
30        break;
31      fi
32    done
33  fi
34}
35
36# checks for the original mozilla start script(s)
37# and restrict the "-remote" semantics to those.
38run_mozilla() {
39  if file "$1" | grep "script" > /dev/null && grep "NPL" "$1" > /dev/null; then
40    "$1" -remote 'ping()' 2>/dev/null >/dev/null
41    if [ $? -eq 2 ]; then
42      "$1" "$2" &
43    else
44      "$1" -remote "openURL($2, new-window)" &
45    fi
46  else
47    "$1" "$2" &
48  fi
49}
50
51# checks the browser value for a %s as defined in
52# http://www.catb.org/~esr/BROWSER/index.html
53run_browser() {
54  echo "$1|$2" | awk '
55{
56    FS="|";
57    $syscmd="";
58    if (index($1,"%s") > 0) {
59        $syscmd=sprintf($1,$2);
60    } else {
61        $syscmd=sprintf("%s \"%s\"",$1,$2);
62    }
63    system($syscmd " &");
64}' > /dev/null
65}
66
67# special handling for mailto: uris
68if echo $1 | grep '^mailto:' > /dev/null; then
69  # check for xdg-email
70  mailer=`which xdg-email`
71  if [ ! -z "$mailer" ]; then
72    $mailer "$1" &
73    exit 0
74  fi
75  # check $MAILER variable
76  if [ ! -z "$MAILER" ]; then
77    $MAILER "$1" &
78    exit 0
79  fi
80  # mozilla derivates may need -remote semantics
81  for i in thunderbird mozilla netscape; do
82    mailer=`which $i`
83    if [ ! -z "$mailer" ]; then
84      run_mozilla "$mailer" "$1"
85      exit 0
86    fi
87  done
88  # handle all non mozilla mail clients below
89  # ..
90else
91  # check for xdg-open
92  browser=`which xdg-open`
93  if [ ! -z "$browser" ]; then
94    $browser "$1" &
95    exit 0
96  fi
97  # check $BROWSER variable
98  if [ ! -z "$BROWSER" ]; then
99    $BROWSER "$1" &
100    exit 0
101  fi
102  # mozilla derivates may need -remote semantics
103  for i in firefox mozilla netscape; do
104    browser=`which $i`
105    if [ ! -z "$browser" ]; then
106      run_mozilla "$browser" "$1"
107      exit 0
108    fi
109  done
110  # handle all non mozilla browers below
111  # ..
112fi
113exit 1
114