61 lines
1.1 KiB
Bash
Executable File
61 lines
1.1 KiB
Bash
Executable File
#!/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"
|
|
cfg_uri_regex='^[A-Za-z]([A-Za-z0-9+.-]+)?://.+'
|
|
[[ -f "$cfg_file" ]] && { source "$cfg_file"; }
|
|
|
|
usage() { echo "${0##*/} <uri/file>"; }
|
|
|
|
handle_uri() {
|
|
local target="$1"
|
|
|
|
for h in "${uri_handlers[@]}"; do
|
|
IFS='=' read cmd regex <<< "$h"
|
|
|
|
[[ "$target" =~ ${regex} ]] && {
|
|
${cmd} "$target" &
|
|
return 0
|
|
}
|
|
done
|
|
|
|
return 1
|
|
}
|
|
|
|
handle_mime() {
|
|
target_mimetype=$(file -ib "$target")
|
|
|
|
for m in "${mime_handlers[@]}"; do
|
|
IFS='=' read cmd regex <<< "$m"
|
|
|
|
[[ "$target_mimetype" =~ ${regex} ]] && {
|
|
${cmd} "$target" &
|
|
return 0
|
|
}
|
|
done
|
|
|
|
return 1
|
|
}
|
|
|
|
main() {
|
|
target="$1"
|
|
[[ "$target" ]] || { usage; exit; }
|
|
|
|
handle_uri "$target" || {
|
|
[[ "$target" =~ file://.+ ]] && { target="${target##*file://}"; }
|
|
|
|
[[ -e "$target" ]] && {
|
|
[[ "$target" =~ ^/.* ]] || { target="${PWD}/${target}"; }
|
|
|
|
handle_mime "$target"
|
|
}
|
|
}
|
|
|
|
[[ "$?" -gt 0 ]] && { echo "No handlers found for $target"; }
|
|
|
|
return 0
|
|
}
|
|
|
|
main "$@"
|