summaryrefslogtreecommitdiff
path: root/mac/.local/bin/browse
blob: 976e2a420b3ca84ec7f3ee26f9a58ba7c83d283b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/sh

# Usage message
usage() {
  echo "Searches query in a terminal or browser"
  echo ""
  echo "Usage: ${0##*/} [-d | --ddgr | ddgr] [-h | --help | help] [<search_engine>] <search_query>"
  echo ""
  echo "Options:"
  echo "  -d, --ddgr, ddgr    : Use ddgr to search and open the result in a terminal"
  echo "  -h, --help, help    : Display this help message"
  echo "  <search_engine>     : (Optional) Search engine to use (bing, duckduckgo, google, naver, yahoo, youtube)"
  echo "  <search_query>      : The search term or query to use"
}

# Set default values
search_tool="web"
search_engine="searx"

# Determine the open command based on the operating system
case "$(uname -s)" in
Darwin)
  open_cmd='open'
  ;;
*)
  open_cmd='xdg-open'
  ;;
esac

# Check the first argument for flags or help using case
case "$1" in
-d | --ddgr | ddgr)
  # Check if ddgr is installed (only needed for ddgr options)
  if ! command -v ddgr >/dev/null 2>&1; then
    echo "Error: ddgr is not installed." >&2
    exit 1
  fi
  search_tool="ddgr"
  shift # Remove this argument from the list
  ;;
-h | --help | help)
  usage && exit 0
  ;;
bing | duckduckgo | google | naver | yahoo | youtube)
  search_engine="$1"
  shift # Remove the search engine from the list
  ;;
esac

# Store the remaining arguments as the search query
search_query="$*"

# Ensure a search query is provided; if not, show usage
[ -z "$search_query" ] && usage && exit 1

# Execute the corresponding search tool using case
case $search_tool in
ddgr)
  # Run DuckDuckGo search in the terminal
  setsid -f "$TERMINAL" -e ddgr "$search_query"
  ;;
web)
  # Construct the URL based on the search engine
  case "$search_engine" in
  bing | google | yahoo | youtube)
    base_url="https://www.${search_engine}.com/search?q="
    ;;
  duckduckgo)
    base_url="https://duckduckgo.com/?q="
    ;;
  naver)
    base_url="https://search.naver.com/search.naver?query="
    ;;
  searx | *)
    base_url="https://www.searx.thesiah.xyz/search?q="
    ;;
  esac

  # Encode the search query
  search_query_encoded=$(echo "$search_query" | sed 's/ /+/g')

  # Open the search URL in the default browser
  $open_cmd "${base_url}${search_query_encoded}"
  ;;
*)
  usage && exit 1
  ;;
esac