-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.sh
executable file
·225 lines (183 loc) · 6.54 KB
/
functions.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
NONE='\033[00m'
RED='\033[01;31m'
GREEN='\033[01;32m'
YELLOW='\033[01;33m'
PURPLE='\033[01;35m'
CYAN='\033[01;36m'
WHITE='\033[01;37m'
BOLD='\033[1m'
UNDERLINE='\033[4m'
TITLE=${BOLD}${UNDERLINE}
function abort {
echo ""
echo -e "${RED}${BOLD}I can't work under these conditions!${NONE}"
exit 1
}
function change_rd {
read -p "New RamDisk Name: " NEW_DISK_NAME
DISK_NAME=$NEW_DISK_NAME
echo "Changing ramdisk to ${MOUNT_POINT}/${DISK_NAME}."
}
function ls_rd {
find ${MOUNT_POINT}/${DISK_NAME} |less
}
function eject_rd {
if [ $OS = "Linux" ]; then
# want to try without sudo first
# if that fails, try with sudo
# if that fails, change_rd
umount ${MOUNT_POINT}/${DISK_NAME}
elif [ $OS = "Darwin" ]; then
diskutil unmount ${MOUNT_POINT}/${DISK_NAME}
if [ -d ${MOUNT_POINT}/${DISK_NAME} ]; then
echo "Sorry, ejecting ${MOUNT_POINT}/${DISK_NAME} was unsuccessful. Let's just create a new ramdisk instead."
change_rd
else
echo "${MOUNT_POINT}/${DISK_NAME} was successfully ejected."
fi
fi
}
function create_rd {
while ! [ -d ${MOUNT_POINT}/${DISK_NAME} ]; do
# Completely untested, with no checks in place for failure.
# Won't work in linux, because the mount point already exists!
if [ $OS = "Linux" ]; then
sudo mkfs -q /dev/ram0 8192
sudo mount /dev/ram0 ${MOUNT_POINT}/${DISK_NAME}
sudo chown ${USER}:${USER} ${MOUNT_POINT}/${DISK_NAME}
chmod 700 ${MOUNT_POINT}/${DISK_NAME}
elif [ $OS = "Darwin" ]; then
BLOCKS=${MB_SIZE}
let "BLOCKS *= 2048"
echo Creating ${MB_SIZE} MB ram disk named ${DISK_NAME}
echo hdid -nomount ram://$BLOCKS
CREATED_RAMDISK=`hdid -nomount ram://$BLOCKS`
echo New block device: ${CREATED_RAMDISK}
#DISK_NAME=`basename ${CREATED_RAMDISK}`
echo Creating volume named: ${DISK_NAME}
newfs_hfs -v ${DISK_NAME} /dev/$CREATED_RAMDISK
echo Mounting in ${MOUNT_POINT}/${DISK_NAME}
diskutil mount ${CREATED_RAMDISK}
chmod 700 ${MOUNT_POINT}/${DISK_NAME}
fi
done
#make this output prettier!
echo ""
echo `mount |grep ${MOUNT_POINT}/${DISK_NAME}`
echo `ls -lsd ${MOUNT_POINT}/${DISK_NAME}`
echo `df -h ${MOUNT_POINT}/${DISK_NAME}`
echo ""
}
function create_dotdir (){
#checking symlinks
#DOTDIR="gnupg"
DOTDIR=$1
echo ""
echo -e "${TITLE}Checking out ~/.$DOTDIR ${NONE}"
echo ""
SSHPATH=`$READLINK ~/.ssh`
GPGPATH=`$READLINK ~/.gnupg`
# does directory already exist?
# if not, we'll just create symlink, and that's that
# if it does exist, is it a symlink?
# if not, we'll offer to move existing dir, and then make symlink
# if it is a symlink, is it correct?
# if not, we'll offer to delete it, and then make the correct symlink
# we should make this all a function, since we'll be doing the same thing on multiple directories.
# -d ~/.$DOTDIR is true when
# - directory exists
# - symlink points to a valid target
# -d ~/.$DOTDIR is false when
# - directory does not exist
# - symlink points to an invalid target
# both symlink states should be treated the same.
# Should probably rewrite all this to check for symlink first, and then if no symlink, check if directory exists.
# does directory already exist?
if [ -d ~/.$DOTDIR ]; then
# But is it a symlink?
if [ -h ~/.$DOTDIR ]; then
echo -e "~/.$DOTDIR is already symlinked: ${STRONG}$DOTDIRLINK${NONE}"
#echo -e "But is it the right symlink?"
#echo "What we want: ${MOUNT_POINT}/${DISK_NAME}/$DOTDIR"
DOTDIRLINK=`$READLINK ~/.$DOTDIR`
#echo -e "What we got: $DOTDIRLINK"
if [ "${MOUNT_POINT}/${DISK_NAME}/$DOTDIR" == "$DOTDIRLINK" ]; then
echo -e "${MOUNT_POINT}/${DISK_NAME}/$DOTDIR equals $DOTDIRLINK"
echo -e "Our job here is done."
else
read -p "Replace with symlink to ${MOUNT_POINT}/${DISK_NAME}/$DOTDIR? " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo ""
rm -rf ~/.$DOTDIR && echo "$DOTDIRLINK symlink deleted." || abort
echo "ln -s ${MOUNT_POINT}/${DISK_NAME}/$DOTDIR ~/.$DOTDIR"
ln -s ${MOUNT_POINT}/${DISK_NAME}/$DOTDIR ~/.$DOTDIR && echo "ls -la ~/.$DOTDIR"; ls -la ~/.$DOTDIR || abort
else
abort
fi
fi
else
echo "~/.$DOTDIR exists."
echo "Here are its contents:"
ls -lh ~/.$DOTDIR
echo ""
read -p "In order to proceed, we need to delete it. Would you like to back everything up first? " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo ""
BACKUP_DOTDIR=~/.$DOTDIR.`date +%s`
echo "Backing up ~/.$DOTDIR to $BACKUP_DOTDIR."
echo ""
mv ~/.$DOTDIR $BACKUP_DOTDIR && echo "ls -lh $BACKUP_DOTDIR"; ls -lh $BACKUP_DOTDIR || abort
echo ""
echo "Creating symlink with"
echo "ln -s ${MOUNT_POINT}/${DISK_NAME}/$DOTDIR ~/.$DOTDIR"
ln -s ${MOUNT_POINT}/${DISK_NAME}/$DOTDIR ~/.$DOTDIR && echo "ls -la ~/.$DOTDIR"; ls -la ~/.$DOTDIR || abort
else
echo ""
read -p "Okay to delete existing ~/.$DOTDIR ? " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo ""
rm -rf ~/.$DOTDIR && echo "~/.$DOTDIR deleted." || $ABORT
echo "Creating symlink with"
echo "ln -s ${MOUNT_POINT}/${DISK_NAME}/$DOTDIR ~/.$DOTDIR"
ln -s ${MOUNT_POINT}/${DISK_NAME}/$DOTDIR ~/.$DOTDIR && echo "ls -la ~/.$DOTDIR"; ls -la ~/.$DOTDIR || abort
else
echo ""
abort
fi
fi
fi
else
# directory does not already exist. Is it already symlinked?
if [ -h ~/.$DOTDIR ]; then
echo -e "~/.$DOTDIR is already symlinked: ${STRONG}$DOTDIRLINK${NONE}"
echo -e "But is it the right symlink? ${RED}Probably not, since this symlink doesn't point to a valid directory${NONE}."
#echo "What we want: ${MOUNT_POINT}/${DISK_NAME}/$DOTDIR"
DOTDIRLINK=`$READLINK ~/.$DOTDIR`
#echo -e "What we got: $DOTDIRLINK"
if [ "${MOUNT_POINT}/${DISK_NAME}/$DOTDIR" == "$DOTDIRLINK" ]; then
echo -e "${MOUNT_POINT}/${DISK_NAME}/$DOTDIR equals $DOTDIRLINK"
echo -e "Our job here is done."
else
read -p "Replace with symlink to ${MOUNT_POINT}/${DISK_NAME}/$DOTDIR? " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo ""
rm -rf ~/.$DOTDIR && echo "$DOTDIRLINK symlink deleted." || abort
echo "ln -s ${MOUNT_POINT}/${DISK_NAME}/$DOTDIR ~/.$DOTDIR"
ln -s ${MOUNT_POINT}/${DISK_NAME}/$DOTDIR ~/.$DOTDIR && echo "ls -la ~/.$DOTDIR"; ls -la ~/.$DOTDIR || abort
else
abort
fi
fi
else
#We can create the symlink and go.
echo "~/.$DOTDIR does not exist. We should probably create it."
echo "Creating symlink with"
echo "ln -s ${MOUNT_POINT}/${DISK_NAME}/$DOTDIR ~/.$DOTDIR"
ln -s ${MOUNT_POINT}/${DISK_NAME}/$DOTDIR ~/.$DOTDIR && echo "ls -la ~/.$DOTDIR"; ls -la ~/.$DOTDIR || abort
fi
fi
}