aboutsummaryrefslogtreecommitdiffstats
path: root/tools/addlink
blob: 66e93da80fc2141ad56865d6d0a000f8941a3872 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/sh
set -e

target="$1"
link="$2"

if ! test $# = 2; then
	printf 'usage: %s target link\n' "$0" >&2
	exit 1
fi

if ! test -d scalable || ! test -r icons.mk; then
	printf 'could not find both scalable/ and icons.mk the current working directory\n' >&2
	exit 1
fi

T="$target"
L="$link"
while test $(printf '%s\n' "$T" "$L" | grep / | wc -l) = 2; do
	Td="$(printf '%s\n' "$T" | cut -d / -f 1)"
	Ld="$(printf '%s\n' "$L" | cut -d / -f 1)"
	if test ! "$Td" = "$Ld"; then
		break
	fi
	T="$(printf '%s\n' "$T" | cut -d / -f 2-)"
	L="$(printf '%s\n' "$L" | cut -d / -f 2-)"
done
while printf '%s\n' "$L" | grep / > /dev/null; do
	T="$(printf '../%s\n' "$T")"
	L="$(printf '%s\n' "$L" | cut -d / -f 2-)"
done
expectedtarget="${T}.svg"

if test -f "scalable/${link}.svg"; then
	if test ! -L "scalable/${link}.svg"; then
		currenttarget="not a symbolic link"
	elif test "$(readlink "scalable/${link}.svg")" = "$expectedtarget"; then
		currenttarget="links to $target"
	else
		currenttarget="symbolic link -> $target"
	fi
	printf '%s already exists (%s)\n' "scalable/${link}.svg" "$currenttarget" >&2
	exit 1
fi

targetentry="$(grep '^[[:space:]]*'"$target"'\\$' < icons.mk)"
test -n "$targetentry"
test $(printf '%s\n' "$targetentry" | wc -l) = 1

if grep '^[[:space:]]*'"$link"'\\$' < icons.mk > /dev/null; then
	printf '%s is already listed in icons.mk, however its file does not exist\n' "$link" >&2
	exit 1
fi

indent="$(sed -n 's:^\([[:space:]]*\)'"$target"'\\$:\1:p' < icons.mk)"
targetentrysed="$(printf '^%s$\n' "$targetentry" | sed 's:[/\\]:\\&:g')"

newiconsfile="$(sed /"$targetentrysed"/q < icons.mk && \
                printf '%s\t%s\\\n' "$indent" "$link" && \
                sed 1,/"$targetentrysed"/d < icons.mk)"

test $(printf '%s\n' "$newiconsfile" | diff icons.mk - | wc -l) = 2

( ! printf '%s\n' "$newiconsfile" | diff -U1 icons.mk - >&2 )

T="$target"
L="$link"
while test $(printf '%s\n' "$T" "$L" | grep / | wc -l) = 2; do
	Td="$(printf '%s\n' "$T" | cut -d / -f 1)"
	Ld="$(printf '%s\n' "$L" | cut -d / -f 1)"
	if test ! "$Td" = "$Ld"; then
		break
	fi
	T="$(printf '%s\n' "$T" | cut -d / -f 2-)"
	L="$(printf '%s\n' "$L" | cut -d / -f 2-)"
done
while printf '%s\n' "$L" | grep / > /dev/null; do
	T="$(printf '../%s\n' "$T")"
	L="$(printf '%s\n' "$L" | cut -d / -f 2-)"
done

target="$expectedtarget"
link="scalable/${link}.svg"
printf 'ln -s %s %s\n' "$target" "$link" >&2

if ! cp icons.mk icons.mk~; then
	printf 'failed to create backup of icons.mk\n' >&2
	exit 1
fi
if ! ln -s "$target" "$link"; then
	printf 'failed to create symbolic link %s -> %s\n' "$link" "$target" >&2
	rm -f icons.mk~ >&2
	exit 1
fi
if ! printf '%s\n' "$newiconsfile" > icons.mk; then
	printf 'failed to insert entry into icons.mk\n' >&2
	if ! unlink "$link" >&2; then
		printf '\x1b[31mfailed to remove created symbolic link (%s)\x1b[m\n' "$link" >&2
	fi
	if ! mv icons.mk~ icons.mk >&2; then
		printf '\x1b[1;31mfailed to restore icons.mk using icons.mk~\x1b[m\n' >&2
	fi
	exit 1
fi