File: bm_import.sh

package info (click to toggle)
clifm 1.26.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,816 kB
  • sloc: ansic: 64,595; sh: 3,133; python: 1,851; makefile: 567
file content (65 lines) | stat: -rwxr-xr-x 1,550 bytes parent folder | download
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
#!/bin/sh

# Clifm plugin to import bookmarks from either Ranger or Midnight Commander
# Description: Import Ranger/MC bookmarks from FILE
# Author: L. Abramovich
# License: GPL2+

if [ -z "$1" ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
	name="${CLIFM_PLUGIN_NAME:-$(basename "$0")}"
	printf "Import Ranger or Midnight Commander bookmarks from FILE

\x1b[1mUSAGE\x1b[0m\n  %s FILE\n" "$name"
	exit 0
fi

file="$(echo "$1" | sed 's/\\//g')"

if ! [ -f "$file" ]; then
	printf "clifm: %s: No such file or directory\n" "$file" >&2
	exit 1
fi

if [ -z "$CLIFM" ] || ! [ -f "${CLIFM}/bookmarks.clifm" ]; then
	printf "Bookmarks file for Clifm not found\n" >&2
	exit 1
fi

clifm_bm="${CLIFM}/bookmarks.clifm"
bmn=0

if grep -q ^"ENTRY " "$file"; then
	fm="mc"
else
	fm="ranger"
fi

while read -r line; do
	if [ "$fm" = "mc" ]; then
		name="$(echo "$line" | awk '{print $2}' | sed 's/\"//g')"
		path="$(echo "$line" | awk '{print $4}' | sed 's/\"//g')"
	else
		name="$(echo "$line" | cut -d':' -f1 2>/dev/null)"
		path="$(echo "$line" | cut -d':' -f2 2>/dev/null)"
	fi

	if [ -z "$name" ] || [ -z "$path" ]; then
		printf "clifm: %s: Bookmark cannot be imported\n" "$line" >&2
		continue
	fi
	bmn=$((bmn + 1))
	printf "[%s]%s\n" "$name" "$path" >> "$clifm_bm"
done < "$file"

if [ "$bmn" -gt 0 ]; then
	printf "clifm: %d bookmark(s) successfully imported\n" "$bmn"
	if [ -n "$CLIFM_BUS" ]; then
		echo "bm reload" > "$CLIFM_BUS"
	else
		printf "Restart CliFM for changes to take effect\n"
	fi
else
	printf "clifm: No bookmarks imported\n"
fi

exit 0