forked from sheharyaar/ptokax
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathptokax-setup.sh
executable file
·146 lines (129 loc) · 5.79 KB
/
ptokax-setup.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
#!/bin/bash
set -eou pipefail
# Colors - Obvious no?
export TERM=xterm
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
WHITE=$(tput setaf 7)
ORANGE=$(tput setaf 9)
# IP of Pi
RASPI_IP=$(ip addr | grep inet | grep eth0 | cut -d' ' -f6)
if [ "$(systemctl is-active dhcpcd)" == "inactive" ]; then
echo -e "${GREEN}[+] ${BLUE}Starting DHCP service${WHITE}"
sudo service dhcpcd start
else
echo -e "${YELLOW}[-] ${BLUE}DHCP service already running${WHITE}"
fi
if [ "$(systemctl is-enabled dhcpcd)" == "disabled" ]; then
echo -e "${GREEN}[+] ${BLUE}Enabling DHCP service${WHITE}"
sudo systemctl enable dhcpcd 2>/dev/null
else
echo -e "${YELLOW}[-] ${BLUE}DHCP service already enabled${WHITE}"
fi
# Adding the static ip config only if an entry doesn't already exist
IS_IP_STATIC=$(grep -q '#Static IP for PtokaX' /etc/dhcpcd.conf && echo true || echo false)
if [ "$IS_IP_STATIC" == "false" ]; then
echo -e "${GREEN}[+] ${BLUE}Making RPi's IP address static${WHITE}"
GATEWAY_IP=$(ip route | grep default | cut -d' ' -f3)
DNS_ADDRESS=$(grep nameserver -m 1 /etc/resolv.conf | cut -d' ' -f2)
cat <<- EOF >> /etc/dhcpcd.conf
#Static IP for PtokaX
interface eth0
static ip_address=${RASPI_IP}
static routers=${GATEWAY_IP}
static domain_name_servers=${DNS_ADDRESS}
EOF
else
echo -e "${YELLOW}[-] ${BLUE}RPi's IP address is already static${WHITE}"
fi
# Making the all the scripts executable
for file in /home/pi/Metahub/*; do
if [ -f "$file" ] && [ "${file##*.}" == "sh" ] && [ ! -x "$file" ]; then
echo -e "${GREEN}[+] ${BLUE}Making $file executable${WHITE}"
chmod +x "$file"
fi
done
# Configuring PtokaX Aliases
ALIAS_CONFIGURED=$(grep -q 'source /home/pi/Metahub/psm/ptokax-alias' /home/pi/.bashrc && echo true || echo false)
if [ "$ALIAS_CONFIGURED" == "false" ]; then
echo "source /home/pi/Metahub/psm/ptokax-alias" >> /home/pi/.bashrc
source /home/pi/.bashrc
fi
echo -e "${GREEN}[+] ${BLUE}Installing / Updating required packages${WHITE}"
# Curl for downloading source code
# mysql - required for scripts
# LUA 5.2.2 - Latest lua not installed as scripts are in lua version 5.2.2
sudo apt-get update
sudo apt-get install -y curl liblua5.2-dev make g++ zlib1g-dev libtinyxml-dev default-libmysqlclient-dev lua-sql-mysql libcap2-bin
sudo apt-get autoremove -y
# Editing SettingDefaults.h file
IS_BUG_FIXED_1=$(grep -q "${RASPI_IP%%/*}" /home/pi/Metahub/PtokaX/core/SettingDefaults.h && echo true || echo false)
if [ "$IS_BUG_FIXED_1" == "false" ]; then
echo -e "${GREEN}[+] ${BLUE}Modifying ${YELLOW}/home/pi/Metahub/PtokaX/core/SettingDefaults.h${WHITE}"
sed -i "s/.*HUB_NAME/ \"Metahub\", \/\/HUB_NAME/" /home/pi/Metahub/PtokaX/core/SettingDefaults.h
sed -i "s/.*HUB_ADDRESS/ \"${RASPI_IP%%/*}\", \/\/HUB_ADDRESS/" /home/pi/Metahub/PtokaX/core/SettingDefaults.h
sed -i "s/.*REDIRECT_ADDRESS/ \"${RASPI_IP%%/*}:411\", \/\/REDIRECT_ADDRESS/" /home/pi/Metahub/PtokaX/core/SettingDefaults.h
else
echo -e "${YELLOW}[-] ${BLUE}BUG is already fixed in ${YELLOW}/home/pi/Metahub/PtokaX/core/SettingDefaults.h${WHITE}"
fi
# Editing Settings.h file
IS_BUG_FIXED_2=$(grep -q "${RASPI_IP%%/*}:411" /home/pi/Metahub/PtokaX/cfg/Settings.pxt && echo true || echo false)
if [ "$IS_BUG_FIXED_2" == "false" ]; then
echo -e "${GREEN}[+] ${BLUE}Modifying ${YELLOW}/home/pi/Metahub/PtokaX/cfg/Settings.pxt${WHITE}"
sed -i "s/.*HubName.*/#HubName = Metahub/" /home/pi/Metahub/PtokaX/cfg/Settings.pxt
sed -i "s/.*HubAddress.*/#HubAddress = ${RASPI_IP%%/*}/" /home/pi/Metahub/PtokaX/cfg/Settings.pxt
sed -i "s/.*RedirectAddress.*/#RedirectAddress = ${RASPI_IP%%/*}:411/" /home/pi/Metahub/PtokaX/cfg/Settings.pxt
else
echo -e "${YELLOW}[-] ${BLUE}BUG is already fixed in ${YELLOW}/home/pi/Metahub/PtokaX/cfg/Settings.pxt${WHITE}"
fi
# Updating MOTD with the IP of PI
IS_IP_UPDATED=$(grep -q "dc.metakgp.org" /home/pi/Metahub/PtokaX/cfg/Motd.txt && echo true || echo false)
if [ "$IS_IP_UPDATED" == "false" ]; then
echo -e "${GREEN}[+] ${BLUE}Modifying ${YELLOW}/home/pi/Metahub/PtokaX/cfg/Motd.txt${WHITE}"
sed -i "s/.*Hub Address.*/ Hub Address - dc.metakgp.org/" /home/pi/Metahub/PtokaX/cfg/Motd.txt
else
echo -e "${YELLOW}[-] ${BLUE}IP is already updated in ${YELLOW}/home/pi/Metahub/PtokaX/cfg/Motd.txt${WHITE}"
fi
# Compiling PtokaX
if [ ! -f /home/pi/Metahub/PtokaX/skein/skein.a ]; then
echo -e "${GREEN}[+] ${BLUE}Compiling PtokaX${WHITE}"
cd /home/pi/Metahub/PtokaX/ || (echo "cd to /home/pi/Metahub/PtokaX failed" && exit)
for dir in "obj" "skein/obj"; do
if [ ! -d "$dir" ]; then
mkdir obj skein/obj
fi
done
make -f makefile-mysql lua52
cd ~ || (echo "cd to ~ failed" && exit)
sudo rm -f /usr/local/bin/PtokaX
else
echo -e "${YELLOW}[-] ${BLUE}PtokaX already compiled${WHITE}"
fi
# Installing PtokaX
if [ ! -f /usr/local/bin/PtokaX ]; then
echo -e "${GREEN}[+] ${BLUE}Installing PtokaX${WHITE}"
cd /home/pi/Metahub/PtokaX/ || (echo "cd to /home/pi/Metahub/PtokaX failed" && exit)
sudo make install
cd ~ || (echo "cd to ~ failed" && exit)
else
echo -e "${YELLOW}[-] ${BLUE}PtokaX already installed${WHITE}"
fi
# Handling PtokaX service
if [ ! -f /etc/systemd/system/ptokax.service ]; then
echo -e "${GREEN}[+] ${BLUE}Creating PtokaX service${WHITE}"
chmod 644 /home/pi/Metahub/psm/systemd/ptokax.service
sudo cp /home/pi/Metahub/psm/systemd/ptokax.service /etc/systemd/system/
sudo chmod 777 /etc/systemd/system/ptokax.service
sudo systemctl daemon-reload
else
echo -e "${YELLOW}[-] ${BLUE}PtokaX service already created${WHITE}"
fi
if [ "$(systemctl is-enabled ptokax)" == "disabled" ]; then
echo -e "${GREEN}[+] ${BLUE}Enabling PtokaX service${WHITE}"
sudo systemctl enable ptokax
else
echo -e "${YELLOW}[-] ${BLUE}PtokaX service already enabled${WHITE}"
fi
echo -e "${RED}[*] ${BLUE}Run PtokaX server using ${ORANGE}ptokax.start ${WHITE}"