#!/usr/bin/env bash # This is an attempt to replace xdg-open with something sane. declare -A uri_handlers declare -A mime_handlers # Source the config file. cfg_file="$HOME/.config/sx-open.cfg" cfg_uri_regex='^[A-Za-z]([A-Za-z0-9+.-]+)?://.+' [[ -f "$cfg_file" ]] && { source "$cfg_file"; } usage() { echo "${0##*/} "; } handle_uri() { local target="$1" for h in "${!uri_handlers[@]}"; do [[ "$target" =~ ${uri_handlers[${h}]} ]] && { ${h} "$target" & return 0 } done return 1 } handle_mime() { target_mimetype=$(file -ib "$target") for m in "${!mime_handlers[@]}"; do [[ "$target_mimetype" =~ ${mime_handlers[${m}]} ]] && { ${m} "$target" & return 0 } done return 1 } main() { target="$1" [[ "$target" ]] || { usage; exit; } if [[ "$target" =~ ${cfg_uri_regex} ]]; then handle_uri "$target" elif [[ -e "$target" ]]; then [[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; } handle_uri "file://${target}" || { handle_mime "$target" } else echo "$target is not a uri nor is it an existing file. Bailing." return 1 fi [[ "$?" -gt 0 ]] && { echo "No handlers found for $target"; } return 0 } main "$@"