-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade.script
71 lines (71 loc) · 2.84 KB
/
upgrade.script
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
buildBinary(){
curl -s https://raw.githubusercontent.com/Sid-Sun/codeMKII/master/codeMK2.c > /tmp/codeMK2.c
cd /tmp/
read -r -p "Which compiler do you want to use? [gcc/clang] " response
if [[ "$response" =~ ^([gG][cC][cC]|[gG])+$ ]]
then
gcc -o codeMK2 codeMK2.c
else
clang -o codeMK2 codeMK2.c
fi
}
replaceBinary(){
export savedGroup=$(ls -l $pathToBinary | awk '{print $4}') #save group so we can chown later on and apply the previous group, do it before check as we need to store it on either cases
export savedPermissions=$(stat -c "%a %n" $pathToBinary | awk '{print $1}'); #Save file permissions too; in case user uses something other than 755.
if [[ $(ls -l $pathToBinary | awk '{print $3}') == $USER ]] #Check if current user owns the binary
then
mv /tmp/codeMK2 $pathToBinary
chmod $savedPermissions $pathToBinary
if [[ $USER != $savedGroup ]]
then
chown :$savedGroup $pathToBinary
fi
echo "Upgrade completed."
else
read -r -p "The binary file is not owned by "$USER", do you wish to try with superuser privileges? (y/n): " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
then
sudo mv /tmp/codeMK2 $pathToBinary
sudo chmod $savedPermissions $pathToBinary
sudo chown :$savedGroup $pathToBinary
echo "Upgrade completed successfully if the superuser privileges were granted."
else
echo "Upgrade aborted."
fi
fi
rm -f /tmp/codeMK2.c #Remove the curl'd source regardless of the upgrade succeeding.
}
readlinkRecursively(){
toCheck=$1
export checkedWithReadlink=$(readlink $toCheck | awk '{print $1}');
if [[ $checkedWithReadlink != '' ]]
then
readlinkRecursively "$checkedWithReadlink"
fi
}
export upstreamVersion=$(curl -s https://raw.githubusercontent.com/Sid-Sun/codeMKII/master/version.number | cat)
if [[ $currentlyInstalledVersion != $upstreamVersion ]]
then
read -r -p "Update to "$upstreamVersion" is available, installed version is "$currentlyInstalledVersion" do you want to upgrade now? (y/n): " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
then
export checkAliased=$(type $calledCommand | awk '{print $3}')
if [[ $checkAliased == "aliased" ]]
then
readlinkRecursively $(type $calledCommand | awk '{print $5}')
export pathToBinary=$toCheck
buildBinary
replaceBinary
else
readlinkRecursively $checkAliased
export pathToBinary=$toCheck
buildBinary
replaceBinary
fi
else
echo "Okay, Just run "$calledCommand" with -u argment again whenever you wish to upgrade."
fi
else
echo "You are running the latest version, "$currentlyInstalledVersion
fi
exit