aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/getplugs
blob: d8750746bc5faeb3f22122156613329bf26d021f (plain) (blame)
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
#!/usr/bin/env sh

# Description: Update nnn plugins to installed nnn version
#
# Shell: POSIX compliant
# Authors: Arun Prakash Jana, KlzXS

CONFIG_DIR=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/
PLUGIN_DIR=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins

# is_cmd_exists () {
#     which "$1" > /dev/null 2>&1
#     echo $?
# }

merge () {
	if which nvim >/dev/null 2>&1; then
		nvim -d "$1" "$2"
	else
		vimdiff +0 "$1" "$2"
	fi
}

prompt () {
	printf "%s\n" "Plugin $1 already exists and is different."
	printf "Keep (k), merge (m), overwrite (o) [default: k]? "
	read -r operation

	if [ "$operation" = "m" ]; then
		op="merge"
	elif [ "$operation" = "o" ]; then
		op="cp -vRf"
	else
		op="true"
	fi
}

# if [ "$(is_cmd_exists sudo)" -eq "0" ]; then
#     sucmd=sudo
# elif [ "$(is_cmd_exists doas)" -eq "0" ]; then
#     sucmd=doas
# else
#     sucmd=: # noop
# fi

if [ "$1" = "master" ] ; then
    VER="master"
    ARCHIVE_URL=https://github.com/jarun/nnn/archive/master.tar.gz
elif which nnn >/dev/null 2>&1; then
    VER=$(nnn -V)
    ARCHIVE_URL=https://github.com/jarun/nnn/releases/download/v"$VER"/nnn-v"$VER".tar.gz
else
    echo "nnn is not installed"
    exit 1
fi

# backup any earlier plugins
if [ -d "$PLUGIN_DIR" ]; then
    tar -C "$CONFIG_DIR" -czf "$CONFIG_DIR""plugins-$(date '+%Y%m%d%H%M').tar.gz" plugins/
fi

mkdir -p "$PLUGIN_DIR"
cd "$CONFIG_DIR" || exit 1
curl -Ls "$ARCHIVE_URL" -o nnn-"$VER".tar.gz
tar -zxf nnn-"$VER".tar.gz

cd nnn-"$VER"/plugins || exit 1

# shellcheck disable=SC2044
# We do not use obnoxious names for plugins
for f in $(find . -maxdepth 1 \( ! -iname "." ! -iname "*.md" \)); do
	if [ -f ../../plugins/"$f" ]; then
		if [ "$(diff --brief "$f" ../../plugins/"$f")" ]; then
			prompt "$f"
			$op "$f" ../../plugins/
		fi
	else
		cp -vRf "$f" ../../plugins/
	fi
done
cd ../.. || exit 1

rm -rf nnn-"$VER"/ nnn-"$VER".tar.gz