aboutsummaryrefslogtreecommitdiff
path: root/sdn-open
blob: 5eb048f9ee813826f69d21fed8cf63477cbeb5dc (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
#!/bin/sh -e
# sdn-open: an opener for sdn that makes use of Midnight Commander configuration
# to make more kinds of files directly openable

if [ "$#" -ne 1 ]
then
	echo "Usage: $0 FILE" >&2
	exit 2
fi

# This handles both MC_DATADIR and odd installation locations.
datadir=
if command -v mc >/dev/null
then datadir=$(mc --datadir | sed 's/ (.*)$//')
fi

config=
for dir in "$HOME"/.config/mc "$datadir" /etc/mc
do
	if [ -n "$dir" -a -f "$dir/mc.ext.ini" ]
	then
		config=$dir/mc.ext.ini
		break
	fi
done

# This is often used in %env{} expansion, so let's be on the same page.
export PAGER=${PAGER:-less}

export MC_EXT_FILENAME=$(realpath "$1")
export MC_EXT_BASENAME=$(basename "$1")
export MC_EXT_CURRENTDIR=$(dirname "$MC_EXT_FILENAME")
output=$(sdn-mc-ext <"$config" "$(file -Lbz "$1")" \
	"$MC_EXT_FILENAME" "$MC_EXT_BASENAME" "$MC_EXT_CURRENTDIR" Open || :)
kind=$(echo "$output" | sed -n 1p)
command=$(echo "$output" | sed -n 2p)

# We're trying to retain any explicit user preferences while navigating through:
#  - Debian-based systems have /etc/alternatives/open as /usr/bin/open,
#    pointing to either /usr/bin/xdg-open or /usr/bin/run-mailcap;
#  - macOS has /usr/bin/open, and typically no xdg-open;
#  - Windows Subsystem for Linux has explorer.exe in PATH,
#    where launched applications may have problems with UNC paths,
#    and possibly also xdg-open that may not be capable of opening much.
#
# Both macOS open and Windows explorer.exe are capable of opening files,
# directories, as well as URLs through native associations.
if [ -n "$MC_XDG_OPEN" ]
then :
elif command -v explorer.exe >/dev/null
then export MC_XDG_OPEN=explorer.exe
elif command -v open >/dev/null
then export MC_XDG_OPEN=open
elif command -v xdg-open >/dev/null
then export MC_XDG_OPEN=xdg-open
fi

case "$kind" in
cd)
	# These mostly enter virtual filesystems, which we do not understand.
	"$MC_XDG_OPEN" "$MC_EXT_FILENAME"
	;;
'')
	if [ -n "$command" ]
	then eval "$command"
	else "$MC_XDG_OPEN" "$MC_EXT_FILENAME"
	fi
	;;
*)
	echo "Unsupported: $kind" >&2
	exit 1
esac