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
|
function path_join(a, b, x, y) {
x = a
y = b
sub(/\/+$/, "", x) # remove trailing / from a
sub(/^\/+/, "", y) # remove leading / from b
if (x == "")
return y
if (y == "")
return x
return x "/" y
}
BEGIN { FS = "," } ;
ARGIND == 1 {
# requested dirs
dir=path_join(ENVIRON["HOME"], $1)
cmd="mkdir -p " dir
print cmd
system(cmd)
next
}
ARGIND == 2 {
# block for locations
src=path_join(ENVIRON["PWD"], $1)
dst=path_join(ENVIRON["HOME"], $2)
testcmd="test -e " dst
linkcmd="ln -sf " src " " dst
diffcmd="diff -ursN " dst " " src
if (system(testcmd) == 0) {
system("ls -lda " dst)
printf "Exists, overwrite? y/N/d" # no newline
getline resp < "/dev/tty"
while (1) {
if (resp == "d" || resp == "D") {
# dst is exists src is new, order is right
print "Showing diff to apply (empty is none)"
system(diffcmd)
printf "Exists, overwrite? y/N/d" # no newline
getline resp < "/dev/tty"
continue
} else if (resp == "y" || resp == "Y") {
print linkcmd
system(linkcmd)
break
} else if (resp == "n" || resp == "N" || resp == "") {
print "Skipped"
break
}
print "Exists, overwrite? y/N/d"
getline resp < "/dev/tty"
}
} else {
print linkcmd
system(echo linkcmd)
}
}
|