change URL at comments
[tools/sbs.git] / install-sbs
1 #!/bin/sh
2
3 # die: print message to stderr and exit with error code
4 #
5 # PARAMETERS
6 #   $1 $2 ... - message to be printed
7 die()
8 {
9         echo "$CE$N: fatal error: $*$CN" >&2
10         echo "For detailed log, see: $LOG"
11         exit 1
12 }
13
14 warn()
15 {
16         echo "$CE$N: warning: $*$CN" >&2
17 }
18
19 notice()
20 {
21         echo "$CX$N: $*$CN" >&2
22 }
23
24 lx()
25 {
26         notice "[exec]: $*"
27         "$@"
28 }
29
30 # make sure we're not root
31 if test `id -u` = "0"
32 then
33         die "Please install SBS in a user account, not as root"
34 fi
35
36 # enable colors only on terminal devices
37 TPUT="`which tput`"
38 if test -t 0 -a -t 1 -a -n "$TPUT"
39 then
40     CI="`$TPUT setf 6 || true`"
41     CE="`$TPUT setf 4 || true`"
42     CX="`$TPUT setf 2 || true`"
43     CN="`$TPUT sgr0 || true`"
44 else
45     CI=''
46     CE=''
47     CX=''
48     CN=''
49 fi
50
51 N="`basename \"$0\"`"
52
53 # default logging is none
54 LOG="$(mktemp /tmp/.sbs-install-XXXXXX)"
55 [ $? -eq 0 ] || { echo "$CE$N: error: unable to open log file$CN" >&2; exit 1; }
56 echo "SBS install begin" > "$LOG"
57 date >> "$LOG"
58 git rev-parse --short HEAD >> "$LOG"
59 echo >> "$LOG"
60
61 echo
62 echo "$CI""SBS install script$CN"
63 echo "$CI""git version: `git rev-parse --short HEAD`$CN"
64 echo "$CI""last commit: `git log HEAD^1.. | grep ^Date:`$CN"
65 echo "$CI""For help see: https://source.tizen.org$CN"
66 echo
67
68 check_email_and_complain()
69 {
70         local email name
71
72         # figure out the user's single ID
73         while true
74         do
75                 email="`git config --global --get user.email`"
76
77                 if [ "$email" ]
78                 then
79                         break
80                 fi
81
82                 echo "git username and email address are not set correctly."
83                 echo "SBS will configure these for you now."
84                 echo "Example name:  David Kim"
85                 echo "Example email: david.kim@your_company.com"
86                 echo
87                 echo -n "Your name: "
88                 read name
89                 echo -n "Your email: "
90                 read email
91                 git config --global user.name "$name"
92                 git config --global user.email "$email"
93                 echo
94         done
95 }
96
97 copy_key_from_scratchbox()
98 {
99         if ! test -f "$HOME/.ssh/id_rsa"
100         then
101                 if test -f "/scratchbox/users/$USER/home/$USER/.ssh/id_rsa"
102                 then
103                         echo "Copying public key from old scratchbox install"
104                         mkdir -p "$HOME/.ssh"
105                         cp -f "/scratchbox/users/$USER/home/$USER/.ssh"/id_rsa* "$HOME/.ssh"
106                 fi
107         fi
108 }
109
110 check_sbs_uptodate()
111 {
112         local branch
113
114         # update sbs
115         echo -n "Updating sbs... "
116
117         # using origin/master because this is the default setup by git
118         (git fetch "$REMOTE""tools/sbs" master:origin/master -q >> "$LOG") || die "failed to fetch sbs repository"
119         if ! git diff-index --quiet HEAD
120         then
121                 echo
122                 echo
123                 echo "$CE""Files have been changed. Please checkin changes or remove them with:""$CN"
124                 echo "$CE""git reset --hard origin""$CN"
125                 echo
126                 die "Repository not clean"
127         fi
128
129         # check the current branch contains the latest we fetched from above
130         branch=`git branch --contains origin/master 2> /dev/null | grep "^\\* "`
131         if [ ! "$branch" ]
132         then
133                 warn "Please update to origin. e.g. git reset --hard origin"
134                 die "Repository not up-to-date"
135         fi
136
137         echo "done"
138 }
139
140 # location of server with all our source
141 REMOTE=${SBS_INSTALL_REMOTE:-"download.tizen.org:"}
142
143 # our original cwd
144 ROOTDIR="`pwd`"
145
146 # no user compile options, thanks
147 unset CC CFLAGS LDFLAGS MAKE MAKEFLAGS PANTS
148
149 if [ ! "$SBS_INSTALL_LAX" ]
150 then
151         check_email_and_complain
152         copy_key_from_scratchbox
153 #check_sbs_uptodate
154 fi
155
156 # which distribution are we installing on?
157 cat /etc/lsb-release >> "$LOG" 2> /dev/null
158
159 INSTALLDIR=${SBS_INSTALL_DIR:-"$HOME/sbs-install"}
160 BUILDDIR="$ROOTDIR/Build"
161 SB2DIR="$BUILDDIR/scratchbox2"
162 QEMUDIR="$BUILDDIR/qemu"
163 DEBOOTDIR="$BUILDDIR/debootstrap"
164 LUTILDIR="$BUILDDIR/lutil"
165
166 missing=''
167 pkg-config --version >/dev/null 2>&1 || { missing="$missing pkg-config"; warn "pkg-config missing - dependency checks won't be accurate"; }
168 pkg-config --exists libxml-2.0 >/dev/null 2>&1 || missing="$missing libxml2-dev"
169 pkg-config --exists uuid >/dev/null 2>&1 || missing="$missing uuid-dev"
170 cpp --version >/dev/null 2>&1 || { missing="$missing cpp"; warn "cpp missing - dependency checks won't be accurate"; }
171 echo | cpp -include archive.h >/dev/null 2>&1 || missing="$missing libarchive-dev"
172 echo | cpp -include lzo/lzo1x.h >/dev/null 2>&1 || missing="$missing liblzo2-dev"
173 gcc --version >/dev/null 2>&1 || { missing="$missing gcc"; warn "gcc missing - dependency checks won't be accurate"; }
174 [ `uname -m` != 'x86_64' ] || echo '#include <asm/errno.h>' | gcc -m32 -xc -c -o /dev/null - >/dev/null 2>&1 || missing="$missing gcc-multilib"
175
176 if ! pkg-config --exists zlib >/dev/null 2>&1; then
177         # Either pkg-config or zlib itself wasn't found (Debian provide .pc file, Ubuntu doesn't)
178         # We have to check zlib existence manually.
179         echo | cpp -include zlib.h >/dev/null 2>&1 || missing="$missing zlib1g-dev"
180 fi
181
182 # check for things needed to build scratchbox2
183 for prog in automake autoconf make fakeroot realpath; do
184         $prog --version >/dev/null 2>&1 || missing="$missing $prog"
185 done
186
187 # check for dpkg-dev
188 if ! dpkg-architecture --version >/dev/null 2>&1; then
189         missing="$missing dpkg-dev"
190 fi
191
192 [ -z "$missing" ] || die "please install required packages: $missing"
193
194 mkdir -p "$BUILDDIR"
195
196 git rev-parse HEAD > "$BUILDDIR/versions"
197
198 # download stuff
199 while read name
200 do
201         srcfile="$BUILDDIR/$name.tar.gz"
202         srcdir="$BUILDDIR/$name"
203         echo -n "Downloading $name... "
204         echo "Downloading $name" >> "$LOG"
205         rm -rf "$srcdir"
206         wget --no-check-certificate -P $BUILDDIR http://download.tizen.org/releases/1.0/sbs/$name.tar.gz
207         mkdir "$srcdir"
208         tar xzf "$srcfile" -C "$srcdir" --touch
209         rm -f "$srcfile"
210         echo "done"
211 done <<END
212 scratchbox2
213 qemu
214 debootstrap
215 lutil
216 END
217
218 # build scratchbox2
219 echo -n "Building scratchbox2... "
220 (cd "$SB2DIR" && make clean > /dev/null 2>&1)
221 (cd "$SB2DIR" && ./autogen.sh && ./configure && make 2>&1 ) >> "$LOG" || die "scratchbox2 build failed"
222 echo "done"
223 echo -n "Installing scratchbox2... "
224 (cd "$SB2DIR" && make install prefix="$INSTALLDIR" 2>&1 ) >> "$LOG" || die "scratchbox2 install failed"
225 echo "done"
226
227 # build qemu
228 echo -n "Building qemu... "
229 (cd "$QEMUDIR" && make clean > /dev/null 2>&1)
230 (cd "$QEMUDIR" && ./configure --target-list=arm-linux-user,i386-linux-user --disable-blobs --disable-uuid && make 2>&1) >> "$LOG" || die "qemu build failed"
231 echo "done"
232 echo -n "Installing qemu... "
233 (cd "$QEMUDIR" && make install prefix="$INSTALLDIR" 2>&1 ) >> "$LOG" || die "qemu install failed"
234 echo "done"
235
236 # build lutil and related tools
237 echo -n "Building lutil... "
238 (cd "$LUTILDIR" && ./autogen.sh && make prefix=/) >> "$LOG" || die "failed to build lutil"
239 (cd "$LUTILDIR" && make install prefix=/ DESTDIR="$INSTALLDIR" 2>&1 ) >> "$LOG" || die "lutil install failed"
240 echo "done"
241
242 # install debootstrap
243 echo -n "Installing debootstrap... "
244 (cd "$DEBOOTDIR" && fakeroot make devices.tar.gz) >> "$LOG" || die "failed to build debootstrap"
245 mkdir -p "$INSTALLDIR"/share/debootstrap/scripts/
246 cp -fd "$DEBOOTDIR"/scripts/* "$INSTALLDIR"/share/debootstrap/scripts/
247 cp -f "$DEBOOTDIR"/functions "$INSTALLDIR"/share/debootstrap
248 cp -f "$DEBOOTDIR"/devices.tar.gz "$INSTALLDIR"/share/debootstrap
249 cp -f "$DEBOOTDIR"/debootstrap "$INSTALLDIR"/bin/debootstrap
250 echo "done"
251
252 # update the sbs binary
253 echo -n "Installing sbs scripts... "
254 if [ -e "$INSTALLDIR"/sbs.list ]; then
255     notice "removing old sbs installation files" >>"$LOG" 2>&1
256     sort -r "$INSTALLDIR"/sbs.list |
257     while read path; do
258         if [ -d "$INSTALLDIR/$path" ]
259         then
260             lx rmdir "$INSTALLDIR/$path" || true
261         else
262             lx rm "$INSTALLDIR/$path" || true
263         fi
264     done >>"$LOG" 2>&1
265     mv -f "$INSTALLDIR"/sbs.list  "$INSTALLDIR"/sbs.list.old
266 fi
267 mkdir -p "$INSTALLDIR"
268 make install DESTDIR="$INSTALLDIR" prefix=/ >"$INSTALLDIR/sbs.list" 2>>$LOG || die "failed to install SBS"
269 echo "done"
270
271 # save the versions, to enable OSS license compliance
272 cp -f "$BUILDDIR"/versions "$INSTALLDIR"
273
274 SBS_ROOTDIR=${SBS_ROOTDIR:-$HOME/sbs}
275 if ! test -d "$SBS_ROOTDIR"
276 then
277         echo
278         echo "$CX""Run $INSTALLDIR/bin/sbs -c to create your initial environment$CN"
279         echo
280 else
281         echo
282         echo "$CX""SBS update successful$CN"
283         echo
284 fi
285