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