-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetuplinks.sh
executable file
·203 lines (189 loc) · 5.24 KB
/
setuplinks.sh
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/bash
##################################################
# MIT License
##################################################
# File: setuplinks
# Description: Sets up a list of symbolic links
# Author: Pavel Hajek
# License: MIT
VERSION=1.1
##################################################
# ===============================
# FUNCTIONS
# ===============================
Version()
{
# Display Version
echo "$VERSION"
}
Help()
{
# Display Help
echo "NAME:"
echo " setuplinks"
echo "SYNTAX:"
echo " setuplinks [-b|d|f|h|i|r|s|v|V] FILE"
echo "DESCRIPTION:"
echo " Creates a symbolic link LINKNAME->TARGET for each line of FILE"
echo " in the form 'LINKNAME;TARGET'. Only lines starting with '#' are ignored."
echo " By default, the interactive mode of 'ln' is invoked (asks before"
echo " replacement) and the progress is printed. The modifiers can be combined"
echo " as '-b -f -s' ('-bfs' does not work)."
echo "MODIFIERS:"
echo " -b Backup: If LINKNAME exists, it is moved to LINKNAME.bck"
echo " (in the same folder)."
echo " -d Dry run: Does not make any changes on files."
echo " -f Non-interactive mode of 'ln': If LINKNAME exists, it is"
echo " replaced by the newly created symlink without any confirmation."
echo " If combined with '-b', the backup LINKNAME.bck is created"
echo " before the replacement."
echo " -h Prints this help and quits."
echo " -i Interactive mode: Requires confirmation of every file operation."
echo " -r Restore: If LINKNAME.bck exists, it is switched with LINKNAME."
echo " -s Silent mode: Does not print any progress."
echo " -v Prints the version and quits."
echo " -V Verbose mode: Prints out each file-command."
echo "AUTHOR:"
echo " Pavel Hajek."
echo "COPYRIGHT:"
echo " MIT License © 2021 Pavel Hajek"
}
RunCommand()
{
if [ $VERBOSE -eq 1 ]; then
echo "$1"
fi
if [ $INTERACTIVE -eq 1 ]; then
read -p "Press [Yy] to continue: " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
CONTINUE=1
else
CONTINUE=0
fi
fi
if [ $DRY -eq 0 ]; then
CONTINUE=1
else
CONTINUE=0
fi
if [ $CONTINUE -eq 1 ]; then
eval "$1"
else
echo "Skipping."
fi
}
LoadLists()
{
while IFS=$';' read -r COL1 COL2 REM
do
if [[ ! "$COL1" =~ \#.* ]]; then
TARGETS+=("`eval echo $COL2`")
LINKNAMES+=("`eval echo $COL1`")
REMAINDERS+=("$REST")
fi
done < $1
}
Message()
{
if [ ! $SILENT -eq 1 ]; then
echo $1
fi
}
# ===============================
# PROCESS OPTIONS
# ===============================
DRY=0
BACKUP=0
FORCE=0
INTERACTIVE=0
VERBOSE=0
SILENT=0
REVERT=0
while [ -n "$1" ]
do
case "$1" in
-b) BACKUP=1 ;;
-d) DRY=1 ;;
-f) FORCE=1 ;;
-h) Help
exit ;;
-i) INTERACTIVE=1 ;;
-r) REVERT=1 ;;
-s) SILENT=1 ;;
-v) Version
exit ;;
-V) VERBOSE=1 ;;
*) FILENAME=`eval echo $1`
shift
if [ -n "$1" ]; then
echo "Error: Arguments in wrong format. Run with -h for help."
exit -1
else
break
fi ;;
esac
shift
done
# ===============================
# LOAD LISTS
# ===============================
if [ ! -n "$FILENAME" ]; then
echo "Error: Control file not specified. Run with -h for help."
exit -1
fi
if [ ! -f "$FILENAME" ]; then
echo "Error: '$FILENAME' does not exist."
exit -1
fi
declare -a LINKNAMES
declare -a TARGETS
declare -a REMAINDERS
LoadLists $FILENAME
LENLINKNAMES=${#LINKNAMES[@]}
LENTARGETS=${#TARGETS[@]}
if [ ! $LENTARGETS -eq $LENLINKNAMES ]; then
echo "Error: '$FILENAME' has wrong format. Run with -h for help."
exit -1
fi
# ===============================
# MAIN
# ===============================
if [ $REVERT -eq 1 ]; then
for ((i = 0 ; i < $LENTARGETS ; i++)); do
if [ -f "${LINKNAMES[$i]}.bck" ] || [ -d "${LINKNAMES[$i]}.bck" ]; then
Message "Swapping ${LINKNAMES[$i]} and ${LINKNAMES[$i]}.bck."
RANDSTRING=$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 20)
RunCommand "mv '${LINKNAMES[$i]}' '/tmp/$RANDSTRING'; mv '${LINKNAMES[$i]}.bck' '${LINKNAMES[$i]}'; mv '/tmp/$RANDSTRING' '${LINKNAMES[$i]}.bck'"
else
Message "Backup ${LINKNAMES[$i]}.bck does not exist. Skipping."
fi
done
exit
fi
for ((i = 0 ; i < $LENTARGETS ; i++)); do
if [ -f "${TARGETS[$i]}" ] || [ -d "${TARGETS[$i]}" ]; then
if [ -f "${LINKNAMES[$i]}" ] || [ -d "${LINKNAMES[$i]}" ]; then
if [ $BACKUP -eq 1 ]; then
Message "Backing up ${LINKNAMES[$i]} to ${LINKNAMES[$i]}.bck"
if [ -f "${LINKNAMES[$i]}.bck" ] || [ -d "${LINKNAMES[$i]}.bck" ]; then
Message "- Making space by moving ${LINKNAMES[$i]}.bck to /tmp/"
RANDSTRING=$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 20)
RunCommand "mv '${LINKNAMES[$i]}.bck' '/tmp/$RANDSTRING'"
fi
Message "- Moving ${LINKNAMES[$i]} to ${LINKNAMES[$i]}.bck"
RunCommand "mv '${LINKNAMES[$i]}' '${LINKNAMES[$i]}.bck'"
fi
fi
Message "Creating symbolic link ${LINKNAMES[$i]} with target ${TARGETS[$i]}"
mkdir -p "$(dirname ${LINKNAMES[$i]})"
if [ $FORCE -eq 1 ]; then
RunCommand "ln -n -f -s '${TARGETS[$i]}' '${LINKNAMES[$i]}'"
else
RunCommand "ln -n -i -s '${TARGETS[$i]}' '${LINKNAMES[$i]}'"
fi
else
Message "File or directory '${TARGETS[$i]}' does not exist. Skipping."
fi
done
exit 0