#!/usr/bin/env bash # This is an attempt to replace xdg-open with something sane. # Source the config file. cfg_file="$HOME/.config/sx-open.cfg" [[ -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_fs_target() { local target="${1##*file://}" target_filename="${target##*/}" for n in "${!filename_handlers[@]}"; do [[ "$target_filename" =~ ${filename_handlers[${n}]} ]] && { ${n} "$target" & return 0 } done 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 [[ -e "$target" || "$target" == 'file://'* ]]; then handle_fs_target "$target" else handle_uri "$target" fi [[ "$?" -gt 0 ]] && { echo "No handlers found for $target"; } return 0 } main "$@"