-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkalisation
executable file
·99 lines (91 loc) · 2.2 KB
/
linkalisation
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
#!/bin/bash
#set -e
show_usage() {
cat <<- USAGE >&2
Creates symbolic links to files in the source directory from the target directory, recursively,
but only if a file exists in the target directory with the same name.
Before a link is created, the existing namesake file in the target is suffixed with '.upstream'
Both directores must have the same structure. I.e source and target files must have the same path
relative to source and target, as roots.
Usage:
$(basename $0) [-f] -s SOURCE -t TARGET | -r TARGET
Where:
-f Force replacement of existing symlinks.
-r Remove links and restore .upstream files to their original names.
USAGE
exit 85
}
if [ -z $# ]; then
show_usage
fi
check_path() {
typeset -n root=$1
root=$(readlink -e "$2")/
if [ $? -ne 0 ]; then
echo "Directory ./$2 doesn't exist. Exiting."
exit 1
fi
}
while [ $# -gt 0 ]; do
case "$1" in
-s|--source)
check_path sRoot $2
shift
;;
-t|--target)
check_path tRoot $2
shift
;;
-r|--restore)
check_path rRoot $2
shift
;;
-f|--force)
FORCE_LINKS=true
;;
-h|--help)
show_usage
;;
*)
show_usage
esac
shift
done
if [ -z "$sRoot" -a -z "$tRoot" -a ! -z "$rRoot" ]; then
echo -e "Restoring upstream files at $rRoot\n" >&2
c=0
for rFile in $(find -H "$rRoot" -name "*.upstream" -type f);
do
oFile=${rFile%.upstream}
if [ -h $oFile ]; then
rm "$oFile"
fi
mv "$rFile" "$oFile"
echo "Restored ./${oFile#$rRoot}" >&2
let c=c+1
done
echo -e "\nDone restoring $c files." >&2
exit 0
elif [ ! -z "$sRoot" -a ! -z "$tRoot" -a -z "$rRoot" ]; then
echo -e "Linking translation files\nfrom: $sRoot\nto: $tRoot\n" >&2
c=0
for sFile in $(find -H "$sRoot" -type f);
do
linkT=${sFile/#$sRoot/$tRoot}
if [[ -f $linkT || -h $linkT ]]; then
if ! mv --no-clobber "$linkT" "$linkT.upstream"; then
echo "Skipping $sFile: Target could not be backed up." >&2
else
if ln --symbolic --no-target-directory ${FORCE_LINKS+--force} "$sFile" "$linkT"; then
echo "Done ${FORCE_LINKS+forced} linking ./${linkT#$tRoot}" >&2
let c=c+1
fi
fi
else
echo "Skipping ./${sFile#$sRoot}: doesn't exist in target directory." >&2
fi
done
echo -e "\nDone linking $c files." >&2
exit 0
fi
show_usage