forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinit-alpine.sh
More file actions
170 lines (129 loc) · 4.43 KB
/
init-alpine.sh
File metadata and controls
170 lines (129 loc) · 4.43 KB
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
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/share/bin:/usr/share/sbin:/usr/local/bin:/usr/local/sbin:/system/bin:/system/xbin:$PREFIX/local/bin
export PS1="\[\e[38;5;46m\]\u\[\033[39m\]@localhost \[\033[39m\]\w \[\033[0m\]\\$ "
export HOME=/home
export TERM=xterm-256color
required_packages="bash command-not-found tzdata wget"
missing_packages=""
for pkg in $required_packages; do
if ! apk info -e "$pkg" >/dev/null 2>&1; then
missing_packages="$missing_packages $pkg"
fi
done
if [ -n "$missing_packages" ]; then
echo -e "\e[34;1m[*] \e[0mInstalling important packages\e[0m"
apk update && apk upgrade
apk add $missing_packages
if [ $? -eq 0 ]; then
echo -e "\e[32;1m[+] \e[0mSuccessfully installed\e[0m"
fi
echo -e "\e[34m[*] \e[0mUse \e[32mapk\e[0m to install new packages\e[0m"
fi
if [ ! -f /linkerconfig/ld.config.txt ]; then
mkdir -p /linkerconfig
touch /linkerconfig/ld.config.txt
fi
if [ "$1" = "--installing" ]; then
echo "Configuring timezone..."
if [ -n "$ANDROID_TZ" ] && [ -f "/usr/share/zoneinfo/$ANDROID_TZ" ]; then
ln -sf "/usr/share/zoneinfo/$ANDROID_TZ" /etc/localtime
echo "$ANDROID_TZ" > /etc/timezone
echo "Timezone set to: $ANDROID_TZ"
else
echo "Failed to detect timezone"
fi
mkdir -p "$PREFIX/.configured"
echo "Installation completed."
exit 0
fi
if [ "$#" -eq 0 ]; then
echo "$$" > "$PREFIX/pid"
chmod +x "$PREFIX/axs"
if [ ! -e "$PREFIX/alpine/etc/acode_motd" ]; then
cat <<EOF > "$PREFIX/alpine/etc/acode_motd"
Welcome to Alpine Linux in Acode!
Working with packages:
- Search: apk search <query>
- Install: apk add <package>
- Uninstall: apk del <package>
- Upgrade: apk update && apk upgrade
EOF
fi
# Create initrc if it doesn't exist
#initrc runs in bash so we can use bash features
if [ ! -e "$PREFIX/alpine/initrc" ]; then
cat <<'EOF' > "$PREFIX/alpine/initrc"
# Source rc files if they exist
if [ -f "/etc/profile" ]; then
source "/etc/profile"
fi
if [ -f "$HOME/.bashrc" ]; then
source "$HOME/.bashrc"
fi
if [ -f /etc/bash/bashrc ]; then
source /etc/bash/bashrc
fi
# Environment setup
export PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/share/bin:/usr/share/sbin:/usr/local/bin:/usr/local/sbin
export HOME=/home
export TERM=xterm-256color
SHELL=/bin/bash
export PIP_BREAK_SYSTEM_PACKAGES=1
# Smart path shortening function (fish-style: ~/p/s/components)
_shorten_path() {
local path="$PWD"
if [[ "$HOME" != "/" && "$path" == "$HOME" ]]; then
echo "~"
return
elif [[ "$HOME" != "/" && "$path" == "$HOME/"* ]]; then
path="~${path#$HOME}"
fi
[[ "$path" == "~" ]] && echo "~" && return
local parts result=""
IFS='/' read -ra parts <<< "$path"
local len=${#parts[@]}
for ((i=0; i<len; i++)); do
[[ -z "${parts[i]}" ]] && continue
if [[ $i -lt $((len-1)) ]]; then
result+="${parts[i]:0:1}/"
else
result+="${parts[i]}"
fi
done
[[ "$path" == /* ]] && echo "/$result" || echo "$result"
}
# Update prompt vars before each command
PROMPT_COMMAND='_PS1_PATH=$(_shorten_path); _PS1_EXIT=$?'
# Display MOTD if available
if [ -s /etc/acode_motd ]; then
cat /etc/acode_motd
fi
# Command-not-found handler
command_not_found_handle() {
cmd="$1"
pkg=""
green="\e[1;32m"
reset="\e[0m"
pkg=$(apk search -x "cmd:$cmd" 2>/dev/null | awk -F'-[0-9]' '{print $1}' | head -n 1)
if [ -n "$pkg" ]; then
echo -e "The program '$cmd' is not installed.\nInstall it by executing:\n ${green}apk add $pkg${reset}" >&2
else
echo "The program '$cmd' is not installed and no package provides it." >&2
fi
return 127
}
EOF
fi
# Add PS1 only if not already present
if ! grep -q 'PS1=' "$PREFIX/alpine/initrc"; then
# Smart path shortening (fish-style: ~/p/s/components)
echo 'PS1="\[\033[1;32m\]\u\[\033[0m\]@localhost \[\033[1;34m\]\$_PS1_PATH\[\033[0m\] \[\$([ \$_PS1_EXIT -ne 0 ] && echo \"\033[31m\")\]\$\[\033[0m\] "' >> "$PREFIX/alpine/initrc"
# Simple prompt (uncomment below and comment above if you prefer full paths)
# echo 'PS1="\[\033[1;32m\]\u\[\033[0m\]@localhost \[\033[1;34m\]\w\[\033[0m\] \$ "' >> "$PREFIX/alpine/initrc"
fi
chmod +x "$PREFIX/alpine/initrc"
#actual source
#everytime a terminal is started initrc will run
"$PREFIX/axs" -c "bash --rcfile /initrc -i"
else
exec "$@"
fi