-
Notifications
You must be signed in to change notification settings - Fork 0
/
intercpp.sh
176 lines (164 loc) · 4.06 KB
/
intercpp.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
#!/usr/bin/env bash
this="${1}"
shift
appArgs="${@}" #Requires testing, adding here for later.
cwd="$(pwd)"
filedir="$cwd/$this"
exec_name="cexecutable_binary"
#Define Colors
cReset='\033[0m'
cRed='\033[0;31m'
cGreen='\033[0;32m'
cYellow='\033[0;33m'
cBlue='\033[0;34m'
cPurple='\033[0;35m'
cCyan='\033[0;36m'
cWhite='\033[0;37m'
cBWhite='\033[1;37m'
cBRed='\033[1;31m'
cBGreen='\033[1;32m'
regcommand="gcc"
stdincheck()
{
if [ "$this" == "--uninstall" ]; then
if [ "$EUID" -ne 0 ]
then echo "${cYellow}[Warning] There might be permission issues if not ran as sudo. Continue? [Y/n]${cReset}"
read -p 'Continue? [Y/n]' permask
if [ "$permask" == "n" ]
then exit
fi
fi
echo -e "${cBRed}[Shell] Uninstall flag${cReset}"
printf "continue(y/n)?"
read input
if [ $input == "y" ]; then
rm -f "/usr/bin/intercpp"
if command -v intercpp bash; then
rm -f "$0"
fi
echo -e "${cBGreen}Uninstalled${cReset}"
exit 0
else
exit 0
fi
fi
}
gcc_check()
{
if command -v gcc && command -v g++ ; then
echo "ok"
else
echo "err"
fi
}
clangcheck()
{
if command -v clang && command -v clang++ ; then
echo "ok"
else
echo "err"
fi
}
init()
{
if [[ ! $EUID -ne 0 ]]; then
echo -e "${cBRed}[Shell] Root mode${cReset}"
fi
gccstatus="$(gcc_check)"
clstatus="$(clangcheck)"
if [[ $this =~ ^[A-Za-z0-9._%+-]+\.[Cc][Pp][Pp]$ ]]; then
if [ "$gccstatus" != "err" ]; then
regcommand="g++"
elif [ "$clstatus" != "err" ]; then
regcommand="clang++"
else
return 1
fi
elif [[ $this =~ ^[A-Za-z0-9._%+-]+\.[Cc]$ ]]; then
if [ "$gccstatus" != "err" ]; then
regcommand="gcc"
elif [ "$clstatus" != "err" ]; then
regcommand="clang"
else
return 1
fi
else
echo -e "${cBRed}[Shell] Invalid extension${cReset}"
fi
cd "$HOME"
if ! [ -d ".cache" ]; then
mkdir ".cache"
fi
if ! [ -d ".cache/intercpp" ]; then
mkdir ".cache/intercpp"
fi
cd "$cwd"
}
removebinary()
{
cd "$HOME/.cache/intercpp"
rm $exec_name
}
openbinary()
{
cd "$HOME/.cache/intercpp"
chmod 777 "$exec_name"
#echo -e "${cBWhite}[Shell] Running binary--${cReset}"
#./"$exec_name $appArgs" #Requires testing, adding here for later.
./"$exec_name"
removebinary
}
checkbinary()
{
cd "$cwd"
if [ -f "$exec_name" ]; then
local output="$(mv -f "$exec_name" "$HOME/.cache/intercpp" 2>&1)"
return 0
else
return 1
fi
}
stdincheck
if [ -f "$filedir" ]; then
init
if [ $? == 1 ]; then
echo -e "${cBRed}[Shell] No C/C++ compilers found${cReset}"
exit 1
fi
#echo -e "${cBWhite}[Shell] compiling${cReset}"
case $2 in
gcc) MYVARIABLE="$(gcc "$filedir" -o "$exec_name" 2>&1)" ;;
g++) MYVARIABLE="$(g++ "$filedir" -o "$exec_name" 2>&1)" ;;
clang) MYVARIABLE="$(clang "$filedir" -o "$exec_name" 2>&1)" ;;
clang++) MYVARIABLE="$(clang++ "$filedir" -o "$exec_name" 2>&1)" ;;
*) MYVARIABLE="$(${regcommand} "$filedir" -o "$exec_name" 2>&1)" ;;
esac
if [ "$MYVARIABLE" ]; then
echo -e "${cRed}$MYVARIABLE${cReset}"
checkbinary
case $? in
0) printf "continue (y/n)?"
read input
if [ "$input" != "y" ]; then
exit 0
fi
openbinary
exit 0 ;;
1) echo -e "${cBRed}[Shell] binary not found${cReset}"
exit 1 ;;
*) echo -e "${cBWhite}<Dbug> Nothing to do${cReset}"
exit 2 ;;
esac
else
checkbinary
case $? in
0) openbinary
exit 0 ;;
1) echo -e "${cBRed}[Shell] binary not found${cReset}"
exit 1 ;;
esac
fi
else
echo -e "${cBRed}[Shell] file not found${cReset}"
exit 1
fi