-
Notifications
You must be signed in to change notification settings - Fork 3
/
bootstrap-spackle
executable file
·232 lines (203 loc) · 6.74 KB
/
bootstrap-spackle
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
226
227
228
229
230
231
232
#!/bin/bash
#
# Installs spackle
#
# Show line num and func name when debugging with 'set -x'
export PS4='+${BASH_SOURCE}:${LINENO}: ' #:${FUNCNAME[0]}: '
arch="auto"
prefix="$HOME/local"
if [[ "$arch" == "auto" ]]
then
arch=$(uname -m)
fi
source $(dirname $0)/colors
success() {
echo -en "$GREEN"
echo "$@"
echo -en "$RESET"
}
emph() {
echo -en "$LIGHTBLUE"
echo "$@"
echo -en "$RESET"
}
report() {
echo -en "$LIGHTWHITE"
echo "$@"
echo -en "$RESET"
}
err() {
echo -en "$RED"
echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:${FUNCNAME[1]} "
echo "$@"
echo -en "$RESET"
exit 1
}
patch_spackle() {
sed -i -e "s|@INSTALL_DATE@|$(date)|" spackle
sed -i -e "s|@GIT_COMMIT@|$(cd $scriptDir && git rev-parse HEAD)|" spackle
sed -i -e "s|^prefix=.*|prefix=$prefix|" spackle
}
patch_spackle_pathify() {
sed -i -e "s|^SPACKLE_PREFIX=.*|SPACKLE_PREFIX=$prefix|" spackle-pathify
}
patch_makepkg() {
# Use tar instead of bsdtar since we may not have bsdtar/libarchive
sed -i -e 's/\(tar -tf "$file"\) -q/\1/' makepkg
sed -i -e "s/bsdtar/tar/g" makepkg
sed -i -e "s|^confdir=.*|confdir=$prefix/etc|" makepkg
sed -i -e '/^create_package/,/Creating package/ s|pkgdir|pkgdir$prefix|' makepkg
sed -i -e "/warning .*Running %s as an unprivileged user/,+3 {s/^/#/;/sleep/ a\
true
}" makepkg
}
patch_makepkg_conf() {
#sed -i -e '/^BUILDENV/ s/fakeroot/!fakeroot/' makepkg.conf
sed -i -e 's/x86_64-unknown-linux-gnu/x86_64-linux-gnu/' makepkg.conf
sed -i -e "s|^#PKGDEST=/home/packages|PKGDEST=$packageDir|" makepkg.conf
sed -i -e "/^CFLAGS/ i prefix=\"$prefix\"" makepkg.conf
sed -i -e '/^CFLAGS/ s|"$| -I$prefix/usr/include"|' makepkg.conf
sed -i -e '/^CXXFLAGS/ s|"$| -I$prefix/usr/include"|' makepkg.conf
sed -i -e '/^CPPFLAGS/ s|"$| -I$prefix/usr/include"|' makepkg.conf
# Remove "-pipe" to reduce gcc memory usage
sed -i -e "/^CFLAGS/ s|-pipe *||" makepkg.conf
sed -i -e "/^CXXFLAGS/ s|-pipe *||" makepkg.conf
sed -i -e '/^LDFLAGS/ s|"$| -L$prefix/usr/lib"|' makepkg.conf
sed -i -e '/^LDFLAGS/ s|"$| -Wl,-rpath,$prefix/usr/lib"|' makepkg.conf
# append LD_RUN_PATH
sed -i -e '/^LDFLAGS/ a\
export LD_RUN_PATH="$prefix/usr/lib"\
export PKG_CONFIG_PATH="$prefix/usr/lib/pkgconfig"' makepkg.conf
sed -i -e 's|^#MAKEFLAGS="-j2"|MAKEFLAGS="-j1"|' makepkg.conf
#TODO prompt for packager name & email
}
patch_pacman_conf() {
local pacman_conf="pacman.conf"
[[ "$arch" == "x86_64" ]] && pacman_conf="pacman.conf.x86_64"
# Comment out ArchLinux repos
sed -i -e 's/^\[core]/#&/' "$pacman_conf"
sed -i -e 's/^\[extra]/#&/' "$pacman_conf"
sed -i -e 's/^\[community]/#&/' "$pacman_conf"
sed -i -e 's/^Include/#&/' "$pacman_conf"
# Add spackle repo for built packages
cat >> "$pacman_conf" << END
[spackle]
Server = file://$packageDir
END
}
patch_pacman_c() {
sed -i -e '/if(myuid > 0 && needs_root()) {/,+3 s@^@//@' pacman.c
}
patch_fakeroot() {
sed -i -e "s|^FAKEROOT_PREFIX=|FAKEROOT_PREFIX=$prefix|" fakeroot
sed -i -e "s|^FAKEROOT_BINDIR=|FAKEROOT_BINDIR=$prefix|" fakeroot
sed -i -e "s|^PATHS=|PATHS=$prefix|" fakeroot
}
# Bootstrap from archlinux binary packages
bootstrap_from_repo() {
local mirror repo name fullname pkg
[[ -e "$prefix/tmp" ]] || mkdir -p "$prefix/tmp"
pushd "$prefix/tmp" >/dev/null
mirror="http://mirrors.kernel.org/archlinux"
repo="core"
[[ -e "$repo.db" ]] || wget -nv "$mirror/$repo/os/$arch/$repo.db"
# Get makepkg from pacman
report "Bootstrapping makepkg"
name="pacman"
fullname=$(tar -tf "$repo.db" | grep "^$name-[^-]*-[^-]*/\$" | sed -e 's|/$||')
pkg="$fullname-$arch.pkg.tar.xz"
[[ -e "$pkg" ]] || wget -nv -N "$mirror/$repo/os/$arch/$pkg"
tar -C "$prefix" -xf "$pkg" "usr/bin/makepkg" "etc/makepkg.conf"
(cd "$prefix/etc" && patch_makepkg_conf)
(cd "$prefix/usr/bin" && patch_makepkg)
# Fakeroot
report "Bootstrapping fakeroot"
name="fakeroot"
fullname=$(tar -tf "$repo.db" | grep "^$name-[^-]*-[^-]*/\$" | sed -e 's|/$||')
pkg="$fullname-$arch.pkg.tar.xz"
[[ -e "$pkg" ]] || wget -nv "$mirror/$repo/os/$arch/$pkg"
tar -C "$prefix" -xf "$pkg"
(cd "$prefix/usr/bin" && patch_fakeroot)
# Cleanup
hash -r
popd >/dev/null
#rm -rf "$prefix/tmp"
}
bootstrap_spackle() {
while (( "$#" > 0 ))
do
case "$1" in
--prefix=*) eval prefix=${1#*=} ;;
--prefix) shift; prefix=$1 ;;
*) err "Unrecognized option '$1'" ;;
esac
shift
done
[[ "${prefix:0:1}" != "/" ]] && err "prefix must be an absolute path (prefix='$prefix')"
report "Installing spackle to '$prefix'"
sleep 1
mkdir -p "$prefix"
# Set prefix to an absolute path
prefix=$(cd $prefix; pwd)
packageDir="$prefix/var/lib/spackle/pkg"
bootstrap_from_repo
report "Installing spackle script"
mkdir -p "$prefix/usr/bin"
cp "$scriptDir/spackle" "$prefix/usr/bin"
(cd $prefix/usr/bin && patch_spackle)
mkdir -p "$prefix/etc/spackle"
cp "$scriptDir/rbashrc" "$prefix/etc/spackle/rbashrc"
cp "$scriptDir/colors" "$prefix/etc/spackle/colors"
cp "$scriptDir/spackle-pathify" "$prefix/etc/spackle/spackle-pathify"
(cd "$prefix/etc/spackle" && patch_spackle_pathify)
ln -fs "$prefix/etc/spackle/spackle-pathify" "$HOME/.spackle"
report "Installing shell hooks"
if [[ -e ~/.bashrc ]] && ! grep -q "spackle" ~/.bashrc
then
echo "source ~/.spackle" >> ~/.bashrc
fi
if [[ -e ~/.bash_profile ]] && ! grep -q "spackle" ~/.bash_profile
then
echo "source ~/.spackle" >> ~/.bash_profile
fi
if [[ -e ~/.zshrc ]] && ! grep -q "spackle" ~/.zshrc
then
echo "source ~/.spackle" >> ~/.zshrc
fi
source "$prefix/etc/spackle/spackle-pathify"
report "Installing spackle dependencies"
"$prefix/usr/bin/spackle" install jansson jshon
#zlib bzip2 xz openssl
#libarchive pacman
success ""
success "Done"
emph ""
emph "Setup spackle paths in your current shell by running"
emph ""
report " source ~/.spackle"
emph ""
emph "or logout and log back into your shell."
}
clean() {
rm -rf "$prefix/tmp"
}
delete() {
local choice
read -p "Are you sure you want to delete your entire spackle system? (The entire '$prefix' directory will be permanently deleted.) [y,n] " choice
case ${choice:0:1} in
y|Y) echo "";;
*) exit 1;;
esac
rm -rf "$prefix"
}
# Don't run anything if this script gets sourced
if [[ "$0" = "$BASH_SOURCE" ]]
then
currentDir="$PWD"
scriptDir=$(cd $(dirname $0) && pwd)
case "$1" in
delete) shift; delete ;;
clean) shift; clean ;;
*) bootstrap_spackle "$@" ;;
esac
fi