vdr backup include added
[ipfire-2.x.git] / make.sh
1 #!/bin/bash
2 ############################################################################
3 #                                                                          #
4 # This file is part of the IPFire Firewall.                                #
5 #                                                                          #
6 # IPFire is free software; you can redistribute it and/or modify           #
7 # it under the terms of the GNU General Public License as published by     #
8 # the Free Software Foundation; either version 2 of the License, or        #
9 # (at your option) any later version.                                      #
10 #                                                                          #
11 # IPFire is distributed in the hope that it will be useful,                #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of           #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
14 # GNU General Public License for more details.                             #
15 #                                                                          #
16 # You should have received a copy of the GNU General Public License        #
17 # along with IPFire; if not, write to the Free Software                    #
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA #
19 #                                                                          #
20 # Copyright (C) 2007 IPFire-Team <info@ipfire.org>.                        #
21 #                                                                          #
22 ############################################################################
23 #
24
25 NAME="IPFire"                                                                           # Software name
26 SNAME="ipfire"                                                                  # Short name
27 VERSION="2.1"                                                                           # Version number
28 SLOGAN="www.ipfire.org"                                 # Software slogan
29 CONFIG_ROOT=/var/ipfire                                 # Configuration rootdir
30 NICE=10                                                                                                 # Nice level
31 MAX_RETRIES=1                                                                           # prefetch/check loop
32 KVER=`grep --max-count=1 VER lfs/linux | awk '{ print $3 }'`
33 MACHINE=`uname -m`
34
35 IPFVER="full"                           # Which versions should be compiled? (full|devel)
36
37 # Debian specific settings
38 if [ ! -e /etc/debian_version ]; then
39         FULLPATH=`which $0`
40 else
41         if [ -x /usr/bin/realpath ]; then
42                 FULLPATH=`/usr/bin/realpath $0`
43         else
44                 echo "ERROR: Need to do apt-get install realpath"
45                 exit 1
46         fi
47 fi
48
49 PWD=`pwd`
50 BASENAME=`basename $0`
51 BASEDIR=`echo $FULLPATH | sed "s/\/$BASENAME//g"`
52 LOGFILE=$BASEDIR/log/_build.preparation.log
53 export BASEDIR LOGFILE
54 DIR_CHK=$BASEDIR/cache/check
55 mkdir $BASEDIR/log/ 2>/dev/null
56
57 # Include funtions
58 . tools/make-functions
59
60 if [ -f .config ]; then
61         . .config
62 else
63         echo -e  "${BOLD}No configuration found!${NORMAL}"
64         echo -ne "Do you want to create one (y/N)?"
65         read CREATE_CONFIG
66         echo ""
67         if [ "$CREATE_CONFIG" == "y" ]; then
68                 make_config
69         fi
70 fi
71
72 if [ -z $EDITOR ]; then
73         for i in nano emacs vi; do
74                 EDITOR=$(which $i 2>/dev/null)
75                 if ! [ -z $EDITOR ]; then
76                         export EDITOR=$EDITOR
77                         break
78                 fi
79         done
80         [ -z $EDITOR ] && exiterror "You should have installed an editor."
81 fi
82
83 prepareenv() {
84     ############################################################################
85     #                                                                          #
86     # Are we running the right shell?                                          #
87     #                                                                          #
88     ############################################################################
89     if [ ! "$BASH" ]; then
90                         exiterror "BASH environment variable is not set.  You're probably running the wrong shell."
91     fi
92
93     if [ -z "${BASH_VERSION}" ]; then
94                         exiterror "Not running BASH shell."
95     fi
96
97
98     ############################################################################
99     #                                                                          #
100     # Trap on emergency exit                                                   #
101     #                                                                          #
102     ############################################################################
103     trap "exiterror 'Build process interrupted'" SIGINT SIGTERM SIGKILL SIGSTOP SIGQUIT
104
105
106     ############################################################################
107     #                                                                          #
108     # Resetting our nice level                                                 #
109     #                                                                          #
110     ############################################################################
111     echo -ne "Resetting our nice level to $NICE" | tee -a $LOGFILE
112     renice $NICE $$ > /dev/null
113     if [ `nice` != "$NICE" ]; then
114                         beautify message FAIL
115                         exiterror "Failed to set correct nice level"
116     else
117                         beautify message DONE
118     fi
119
120
121     ############################################################################
122     #                                                                          #
123     # Checking if running as root user                                         #
124     #                                                                          #
125     ############################################################################
126     echo -ne "Checking if we're running as root user" | tee -a $LOGFILE
127     if [ `id -u` != 0 ]; then
128                         beautify message FAIL
129                         exiterror "Not building as root"
130     else
131                         beautify message DONE
132     fi
133
134
135     ############################################################################
136     #                                                                          #
137     # Checking for necessary temporary space                                   #
138     #                                                                          #
139     ############################################################################
140     echo -ne "Checking for necessary space on disk $BASE_DEV" | tee -a $LOGFILE
141     BASE_DEV=`df -P -k $BASEDIR | tail -n 1 | awk '{ print $1 }'`
142     BASE_ASPACE=`df -P -k $BASEDIR | tail -n 1 | awk '{ print $4 }'`
143     if (( 2048000 > $BASE_ASPACE )); then
144                         BASE_USPACE=`du -skx $BASEDIR | awk '{print $1}'`
145                         if (( 2048000 - $BASE_USPACE > $BASE_ASPACE )); then
146                                 beautify message FAIL
147                                 exiterror "Not enough temporary space available, need at least 2GB on $BASE_DEV"
148                         fi
149     else
150                         beautify message DONE
151     fi
152
153     ############################################################################
154     #                                                                          #
155     # Building Linux From Scratch system                                       #
156     #                                                                          #
157     ############################################################################
158     # Set umask
159     umask 022
160
161     # Set LFS Directory
162     LFS=$BASEDIR/build
163
164     # Check /tools symlink
165     if [ -h /tools ]; then
166         rm -f /tools
167     fi
168     if [ ! -a /tools ]; then
169                         ln -s $BASEDIR/build/tools /
170     fi
171     if [ ! -h /tools ]; then
172                         exiterror "Could not create /tools symbolic link."
173     fi
174
175     # Setup environment
176     set +h
177     LC_ALL=POSIX
178     MAKETUNING="-j6"
179     export LFS LC_ALL CFLAGS CXXFLAGS MAKETUNING
180     unset CC CXX CPP LD_LIBRARY_PATH LD_PRELOAD
181
182     # Make some extra directories
183     mkdir -p $BASEDIR/build/{tools,etc,usr/src} 2>/dev/null
184     mkdir -p $BASEDIR/build/{dev/{shm,pts},proc,sys}
185     mkdir -p $BASEDIR/{cache,ccache} 2>/dev/null
186     mkdir -p $BASEDIR/build/usr/src/{cache,config,doc,html,langs,lfs,log,src,ccache}
187
188     mknod -m 600 $BASEDIR/build/dev/console c 5 1 2>/dev/null
189     mknod -m 666 $BASEDIR/build/dev/null c 1 3 2>/dev/null
190
191     # Make all sources and proc available under lfs build
192     mount --bind /dev            $BASEDIR/build/dev
193     mount --bind /dev/pts        $BASEDIR/build/dev/pts
194     mount --bind /dev/shm        $BASEDIR/build/dev/shm
195     mount --bind /proc           $BASEDIR/build/proc
196     mount --bind /sys            $BASEDIR/build/sys
197     mount --bind $BASEDIR/cache  $BASEDIR/build/usr/src/cache
198     mount --bind $BASEDIR/ccache $BASEDIR/build/usr/src/ccache
199     mount --bind $BASEDIR/config $BASEDIR/build/usr/src/config
200     mount --bind $BASEDIR/doc    $BASEDIR/build/usr/src/doc
201     mount --bind $BASEDIR/html   $BASEDIR/build/usr/src/html
202     mount --bind $BASEDIR/langs  $BASEDIR/build/usr/src/langs
203     mount --bind $BASEDIR/lfs    $BASEDIR/build/usr/src/lfs
204     mount --bind $BASEDIR/log    $BASEDIR/build/usr/src/log
205     mount --bind $BASEDIR/src    $BASEDIR/build/usr/src/src
206
207     # This is a temporary hack!!!
208     if [ ! -f /tools/bin/hostname ]; then
209       cp -f /bin/hostname /tools/bin/hostname 2>/dev/null
210     fi
211
212     # Run LFS static binary creation scripts one by one
213     export CCACHE_DIR=$BASEDIR/ccache
214     export CCACHE_HASHDIR=1
215
216     # Remove pre-install list of installed files in case user erase some files before rebuild
217     rm -f $BASEDIR/build/usr/src/lsalr 2>/dev/null
218 }
219
220 buildtoolchain() {
221     LOGFILE="$BASEDIR/log/_build.toolchain.log"
222     export LOGFILE
223     ORG_PATH=$PATH
224     NATIVEGCC=`gcc --version | grep GCC | awk {'print $3'}`
225     export NATIVEGCC GCCmajor=${NATIVEGCC:0:1} GCCminor=${NATIVEGCC:2:1} GCCrelease=${NATIVEGCC:4:1}
226     lfsmake1 ccache
227     lfsmake1 binutils   PASS=1
228     lfsmake1 gcc                PASS=1
229     export PATH=$BASEDIR/build/usr/local/bin:$BASEDIR/build/tools/bin:$PATH
230     lfsmake1 linux-libc-header
231     lfsmake1 glibc
232     lfsmake1 cleanup-toolchain PASS=1
233     lfsmake1 tcl
234     lfsmake1 expect
235     lfsmake1 dejagnu
236     lfsmake1 gcc                PASS=2
237     lfsmake1 binutils   PASS=2
238     lfsmake1 ncurses
239     lfsmake1 bash
240     lfsmake1 bzip2
241     lfsmake1 coreutils
242     lfsmake1 diffutils
243     lfsmake1 findutils
244     lfsmake1 gawk
245     lfsmake1 gettext
246     lfsmake1 grep
247     lfsmake1 gzip
248     lfsmake1 m4
249     lfsmake1 make
250     lfsmake1 patch
251     lfsmake1 perl
252     lfsmake1 sed
253     lfsmake1 tar
254     lfsmake1 texinfo
255     lfsmake1 util-linux
256     lfsmake1 cleanup-toolchain  PASS=2
257     export PATH=$ORG_PATH
258 }
259
260 buildbase() {
261     LOGFILE="$BASEDIR/log/_build.base.log"
262     export LOGFILE
263     lfsmake2 stage2
264     lfsmake2 linux-libc-header
265     lfsmake2 man-pages
266     lfsmake2 glibc
267     lfsmake2 cleanup-toolchain  PASS=3
268     lfsmake2 binutils
269     lfsmake2 gcc
270     lfsmake2 berkeley
271     lfsmake2 coreutils
272     lfsmake2 iana-etc
273     lfsmake2 m4
274     lfsmake2 bison
275     lfsmake2 ncurses
276     lfsmake2 procps
277     lfsmake2 sed
278     lfsmake2 libtool
279     lfsmake2 perl
280     lfsmake2 readline
281     lfsmake2 zlib
282     lfsmake2 autoconf
283     lfsmake2 automake
284     lfsmake2 bash
285     lfsmake2 bzip2
286     lfsmake2 diffutils
287     lfsmake2 e2fsprogs
288     lfsmake2 ed
289     lfsmake2 file
290     lfsmake2 findutils
291     lfsmake2 flex
292     lfsmake2 gawk
293     lfsmake2 gettext
294     lfsmake2 grep
295     lfsmake2 groff
296     lfsmake2 gzip
297     lfsmake2 inetutils
298     lfsmake2 iproute2
299     lfsmake2 kbd
300     lfsmake2 less
301     lfsmake2 libaal
302     lfsmake2 make
303     lfsmake2 man
304     lfsmake2 mktemp
305     lfsmake2 modutils
306     lfsmake2 mtd
307     lfsmake2 net-tools
308     lfsmake2 patch
309     lfsmake2 psmisc
310     lfsmake2 reiser4progs
311     lfsmake2 shadow
312     lfsmake2 sysklogd
313     lfsmake2 sysvinit
314     lfsmake2 tar
315     lfsmake2 texinfo
316     lfsmake2 udev
317     lfsmake2 util-linux
318     lfsmake2 vim
319     lfsmake2 grub
320 }
321
322 buildipfire() {
323   LOGFILE="$BASEDIR/log/_build.ipfire.log"
324   export LOGFILE
325   ipfiremake configroot
326   ipfiremake backup
327   ipfiremake dhcp
328   ipfiremake dhcpcd
329   ipfiremake libusb
330   ipfiremake libpcap
331   ipfiremake ppp
332   ipfiremake rp-pppoe
333   ipfiremake unzip
334   ipfiremake linux                      SMP=1
335   ipfiremake ipp2p                      SMP=1
336   ipfiremake zaptel                     SMP=1
337   ipfiremake r8169                      SMP=1
338   ipfiremake r8168                      SMP=1
339   ipfiremake mcs7830                    SMP=1
340   ipfiremake atl1                       SMP=1
341   ipfiremake dm9601                     SMP=1
342   ipfiremake kqemu                      SMP=1
343   ipfiremake v4l-dvb                    SMP=1
344   ipfiremake sane               KMOD=1  SMP=1
345   ipfiremake linux
346   ipfiremake ipp2p
347   ipfiremake zaptel
348   ipfiremake r8169
349   ipfiremake r8168
350   ipfiremake mcs7830
351   ipfiremake atl1
352   ipfiremake dm9601
353   ipfiremake kqemu
354   ipfiremake v4l-dvb
355   ipfiremake sane               KMOD=1
356   ipfiremake pkg-config
357   ipfiremake linux-atm
358   ipfiremake cpio
359   ipfiremake klibc
360   ipfiremake mkinitcpio
361   ipfiremake udev                               KLIBC=1
362   ipfiremake expat
363   ipfiremake gdbm
364   ipfiremake gmp
365   ipfiremake pam
366   ipfiremake openssl
367   ipfiremake curl
368   ipfiremake python
369   ipfiremake libnet
370   ipfiremake libidn
371   ipfiremake libjpeg
372   ipfiremake libpng
373   ipfiremake libtiff
374   ipfiremake libart
375   ipfiremake freetype
376   ipfiremake gd
377   ipfiremake popt
378   ipfiremake pcre
379   ipfiremake slang
380   ipfiremake newt
381   ipfiremake libcap
382   ipfiremake pciutils
383   ipfiremake usbutils
384   ipfiremake libxml2
385   ipfiremake libxslt
386   ipfiremake BerkeleyDB
387   ipfiremake mysql
388   ipfiremake cyrus-sasl
389   ipfiremake openldap
390   ipfiremake apache2
391   ipfiremake php
392   ipfiremake apache2                    PASS=C
393   ipfiremake arping
394   ipfiremake beep
395   ipfiremake bind
396   ipfiremake cdrtools
397   ipfiremake dnsmasq
398   ipfiremake dosfstools
399   ipfiremake squashfstools
400   ipfiremake reiserfsprogs
401   ipfiremake xfsprogs
402   ipfiremake sysfsutils
403   ipfiremake fuse
404   ipfiremake ntfs-3g
405   ipfiremake ethtool
406   ipfiremake ez-ipupdate
407   ipfiremake fcron
408   ipfiremake perl-GD
409   ipfiremake GD-Graph
410   ipfiremake GD-TextUtil
411   ipfiremake gnupg
412   ipfiremake hdparm
413   ipfiremake sdparm
414   ipfiremake mtools
415   ipfiremake initscripts
416   ipfiremake whatmask
417   ipfiremake iptables
418   ipfiremake libupnp
419   ipfiremake ipp2p                      IPT=1
420   ipfiremake linux-igd
421   ipfiremake ipac-ng
422   ipfiremake ipaddr
423   ipfiremake iptstate
424   ipfiremake iputils
425   ipfiremake l7-protocols
426   ipfiremake mISDN
427   ipfiremake capi4k-utils
428   ipfiremake hwdata
429   ipfiremake kudzu
430   ipfiremake logrotate
431   ipfiremake logwatch
432   ipfiremake misc-progs
433   ipfiremake nano
434   ipfiremake nasm
435   ipfiremake URI
436   ipfiremake HTML-Tagset
437   ipfiremake HTML-Parser
438   ipfiremake Compress-Zlib
439   ipfiremake Digest
440   ipfiremake Digest-SHA1
441   ipfiremake Digest-HMAC
442   ipfiremake libwww-perl
443   ipfiremake Net-DNS
444   ipfiremake Net-IPv4Addr
445   ipfiremake Net_SSLeay
446   ipfiremake IO-Stringy
447   ipfiremake Unix-Syslog
448   ipfiremake Mail-Tools
449   ipfiremake MIME-Tools
450   ipfiremake Net-Server
451   ipfiremake Convert-TNEF
452   ipfiremake Convert-UUlib
453   ipfiremake Archive-Tar
454   ipfiremake Archive-Zip
455   ipfiremake Text-Tabs+Wrap
456   ipfiremake Locale-Country
457   ipfiremake XML-Parser
458   ipfiremake glib
459   ipfiremake GeoIP
460   ipfiremake fwhits
461   ipfiremake noip_updater
462   ipfiremake ntp
463   ipfiremake openssh
464   ipfiremake openswan
465   ipfiremake rrdtool
466   ipfiremake setserial
467   ipfiremake setup
468   ipfiremake snort
469   ipfiremake oinkmaster
470   ipfiremake squid
471   ipfiremake squid-graph
472   ipfiremake squidguard
473   ipfiremake calamaris
474   ipfiremake tcpdump
475   ipfiremake traceroute
476   ipfiremake vlan
477   ipfiremake wireless
478   ipfiremake libsafe
479   ipfiremake pakfire
480   ipfiremake java
481   ipfiremake spandsp
482   ipfiremake lzo
483   ipfiremake openvpn
484   ipfiremake pammysql
485   ipfiremake cups
486   ipfiremake ghostscript
487   ipfiremake foomatic
488   ipfiremake hplip
489   ipfiremake samba
490   ipfiremake sudo
491   ipfiremake mc
492   ipfiremake wget
493   ipfiremake bridge-utils
494   ipfiremake screen
495   ipfiremake hddtemp
496   ipfiremake smartmontools
497   ipfiremake htop
498   ipfiremake postfix
499   ipfiremake fetchmail
500   ipfiremake cyrus-imapd
501   ipfiremake openmailadmin
502   ipfiremake clamav
503   ipfiremake spamassassin
504   ipfiremake amavisd
505   ipfiremake alsa
506   ipfiremake mpg123
507   ipfiremake mpfire
508   ipfiremake guardian
509   ipfiremake libid3tag
510   ipfiremake libmad
511   ipfiremake libogg
512   ipfiremake libvorbis
513   ipfiremake lame
514   ipfiremake sox
515   ipfiremake libshout
516   ipfiremake icecast
517   ipfiremake icegenerator
518   ipfiremake mpd
519   ipfiremake mpc
520   ipfiremake xvid
521   ipfiremake libmpeg2
522   ipfiremake cmake
523   ipfiremake libpri
524   ipfiremake asterisk
525   ipfiremake gnump3d
526   ipfiremake libsigc++
527   ipfiremake applejuice
528   ipfiremake ocaml
529   ipfiremake mldonkey
530   ipfiremake libtorrent
531   ipfiremake rtorrent
532   ipfiremake ipfireseeder
533   ipfiremake rsync
534   ipfiremake tcpwrapper
535   ipfiremake portmap
536   ipfiremake nfs
537   ipfiremake nmap
538   ipfiremake mbmon
539   ipfiremake ncftp
540   ipfiremake etherwake
541   ipfiremake bwm-ng
542   ipfiremake tripwire
543   ipfiremake sysstat
544   ipfiremake vsftpd
545   ipfiremake which
546   ipfiremake lsof
547   ipfiremake centerim
548   ipfiremake br2684ctl
549   ipfiremake pcmciautils
550   ipfiremake collectd
551   ipfiremake lcd4linux
552   ipfiremake neon
553   ipfiremake subversion
554   ipfiremake tcptrack
555   ipfiremake teamspeak
556   ipfiremake elinks
557   ipfiremake igmpproxy
558   ipfiremake sdl
559   ipfiremake qemu
560   ipfiremake sane
561   ipfiremake netpbm
562   ipfiremake phpSANE
563   ipfiremake tunctl
564   ipfiremake nagios
565   ipfiremake ebtables
566   ipfiremake arptables
567   ipfiremake fontconfig
568   ipfiremake freefont
569   ipfiremake ffmpeg
570   ipfiremake videolan
571   ipfiremake vdr
572   ipfiremake w_scan
573   ipfiremake git
574   ipfiremake squidclamav
575   ipfiremake bc
576 }
577
578 buildinstaller() {
579   # Run installer scripts one by one
580   LOGFILE="$BASEDIR/log/_build.installer.log"
581   export LOGFILE
582   ipfiremake syslinux
583   ipfiremake as86
584   ipfiremake mbr
585   ipfiremake memtest
586   installmake linux-libc-header
587   installmake binutils
588   ipfiremake uClibc                     PASS=1
589   ipfiremake gcc                        INST=1
590   installmake uClibc                    PASS=2
591   installmake gcc                       INST=2
592   installmake uClibc                    PASS=3
593   installmake busybox
594   installmake udev
595   installmake slang
596   installmake newt
597   installmake gettext
598   installmake kbd
599   installmake popt
600   installmake sysvinit
601   installmake misc-progs
602   installmake libaal
603   installmake reiser4progs
604   installmake reiserfsprogs
605   installmake sysfsutils
606   installmake util-linux
607   installmake pciutils
608   installmake zlib
609   installmake mtd
610   installmake wget
611   installmake hwdata
612   installmake kudzu
613   installmake pcmciautils
614   installmake installer
615   installmake initrd
616 }
617
618 buildpackages() {
619   LOGFILE="$BASEDIR/log/_build.packages.log"
620   export LOGFILE
621   echo "... see detailed log in _build.*.log files" >> $LOGFILE
622
623   installmake strip
624   
625   # Generating list of packages used
626   echo -n "Generating packages list from logs" | tee -a $LOGFILE
627   rm -f $BASEDIR/doc/packages-list
628   for i in `ls -1tr $BASEDIR/log/[^_]*`; do
629         if [ "$i" != "$BASEDIR/log/FILES" -a -n $i ]; then
630                 echo "* `basename $i`" >>$BASEDIR/doc/packages-list
631         fi
632   done
633   echo "== List of softwares used to build $NAME Version: $VERSION ==" > $BASEDIR/doc/packages-list.txt
634   grep -v 'configroot$\|img$\|initrd$\|initscripts$\|installer$\|install$\|setup$\|pakfire$\|stage2$\|smp$\|tools$\|tools1$\|tools2$\|.tgz$\|-config$\|_missing_rootfile$\|install1$\|install2$\|pass1$\|pass2$\|pass3$' \
635         $BASEDIR/doc/packages-list | sort >> $BASEDIR/doc/packages-list.txt
636   rm -f $BASEDIR/doc/packages-list
637   # packages-list.txt is ready to be displayed for wiki page
638   beautify message DONE
639
640   # Create images for install
641         ipfiremake cdrom ED=full
642         
643   # Check if there is a loop device for building in virtual environments
644   if [ -e /dev/loop/0 ] || [ -e /dev/loop0 ]; then
645         ipfiremake usb-stick
646   fi
647   mv $LFS/install/images/{*.iso,*.tgz,*.img.gz} $BASEDIR >> $LOGFILE 2>&1
648
649   ipfirepackages
650
651   # Cleanup
652   stdumount
653   rm -rf $BASEDIR/build/tmp/*
654
655   # Generating total list of files
656   echo -n "Generating files list from logs" | tee -a $LOGFILE
657   rm -f $BASEDIR/log/FILES
658   for i in `ls -1tr $BASEDIR/log/[^_]*`; do
659         if [ "$i" != "$BASEDIR/log/FILES" -a -n $i ]; then
660                 echo "##" >>$BASEDIR/log/FILES
661                 echo "## `basename $i`" >>$BASEDIR/log/FILES
662                 echo "##" >>$BASEDIR/log/FILES
663                 cat $i | sed "s%^\./%#%" | sort >> $BASEDIR/log/FILES
664         fi
665   done
666   beautify message DONE
667
668   cd $PWD
669 }
670
671 ipfirepackages() {
672         ipfiremake core-updates
673         for i in $(ls -1 $BASEDIR/config/rootfiles/packages); do
674                 if [ -e $BASEDIR/lfs/$i ]; then
675                         ipfiredist $i
676                 else
677                         echo -n $i
678                         beautify message SKIP
679                 fi
680         done
681   test -d $BASEDIR/packages || mkdir $BASEDIR/packages
682   mv -f $LFS/install/packages/* $BASEDIR/packages >> $LOGFILE 2>&1
683   rm -rf  $BASEDIR/build/install/packages/*
684 }
685
686 # See what we're supposed to do
687 case "$1" in 
688 build)
689         clear
690         BUILDMACHINE=`uname -m`
691         PACKAGE=`ls -v -r $BASEDIR/cache/toolchains/$SNAME-$VERSION-toolchain-$BUILDMACHINE.tar.gz 2> /dev/null | head -n 1`
692         #only restore on a clean disk
693         if [ ! -f log/cleanup-toolchain-2-tools ]; then
694                 if [ ! -n "$PACKAGE" ]; then
695                         beautify build_stage "Full toolchain compilation - Native GCC: `gcc --version | grep GCC | awk {'print $3'}`"
696                         prepareenv
697                         buildtoolchain
698                 else
699                         PACKAGENAME=${PACKAGE%.tar.gz}
700                         beautify build_stage "Packaged toolchain compilation"
701                         if [ `md5sum $PACKAGE | awk '{print $1}'` == `cat $PACKAGENAME.md5 | awk '{print $1}'` ]; then
702                                 tar zxf $PACKAGE
703                                 prepareenv
704                         else
705                                 exiterror "$PACKAGENAME md5 did not match, check downloaded package"
706                         fi
707                 fi
708         else
709                 echo -n "Using installed toolchain" | tee -a $LOGFILE
710                 beautify message SKIP
711                 prepareenv
712         fi
713
714         beautify build_start
715         beautify build_stage "Building LFS"
716         buildbase
717
718         beautify build_stage "Building IPFire"
719         buildipfire
720
721         beautify build_stage "Building installer"
722         buildinstaller
723
724         beautify build_stage "Building packages"
725         buildpackages
726         beautify build_end
727         ;;
728 shell)
729         # enter a shell inside LFS chroot
730         # may be used to changed kernel settings
731         prepareenv
732         entershell
733         ;;
734 clean)
735         echo -en "${BOLD}Cleaning build directory...${NORMAL}"
736         for i in `mount | grep $BASEDIR | sed 's/^.*loop=\(.*\))/\1/'`; do
737                 $LOSETUP -d $i 2>/dev/null
738         done
739         for i in `mount | grep $BASEDIR | cut -d " " -f 1`; do
740                 umount $i
741         done
742         stdumount
743         for i in `seq 0 7`; do
744             if ( losetup /dev/loop${i} 2>/dev/null | grep -q "/install/images" ); then
745                 umount /dev/loop${i}     2>/dev/null;
746                 losetup -d /dev/loop${i} 2>/dev/null;
747             fi;
748         done
749         rm -rf $BASEDIR/build
750         rm -rf $BASEDIR/cdrom
751         rm -rf $BASEDIR/packages
752         rm -rf $BASEDIR/log
753         if [ -h /tools ]; then
754                 rm -f /tools
755         fi
756         beautify message DONE
757         ;;
758 downloadsrc)
759         if [ ! -d $BASEDIR/cache ]; then
760                 mkdir $BASEDIR/cache
761         fi
762         mkdir -p $BASEDIR/log
763         echo -e "${BOLD}Preload all source files${NORMAL}" | tee -a $LOGFILE
764         FINISHED=0
765         cd $BASEDIR/lfs
766         for c in `seq $MAX_RETRIES`; do
767                 if (( FINISHED==1 )); then 
768                         break
769                 fi
770                 FINISHED=1
771                 cd $BASEDIR/lfs
772                 for i in *; do
773                         if [ -f "$i" -a "$i" != "Config" ]; then
774                                 echo -ne "Loading $i"
775                                 make -s -f $i LFS_BASEDIR=$BASEDIR MESSAGE="$i\t ($c/$MAX_RETRIES)" download >> $LOGFILE 2>&1
776                                 if [ $? -ne 0 ]; then
777                                         beautify message FAIL
778                                         FINISHED=0
779                                 else
780                                         if [ $c -eq 1 ]; then
781                                         beautify message DONE
782                                         fi
783                                 fi
784                         fi
785                 done
786         done
787         echo -e "${BOLD}***Verifying md5sums${NORMAL}"
788         ERROR=0
789         for i in *; do
790                 if [ -f "$i" -a "$i" != "Config" ]; then
791                         make -s -f $i LFS_BASEDIR=$BASEDIR MESSAGE="$i\t " md5 >> $LOGFILE 2>&1
792                         if [ $? -ne 0 ]; then
793                                 echo -ne "MD5 difference in lfs/$i"
794                                 beautify message FAIL
795                                 ERROR=1
796                         fi
797                 fi
798         done
799         if [ $ERROR -eq 0 ]; then
800                 echo -ne "${BOLD}all files md5sum match${NORMAL}"
801                 beautify message DONE
802         else
803                 echo -ne "${BOLD}not all files were correctly download${NORMAL}"
804                 beautify message FAIL
805         fi
806         cd - >/dev/null 2>&1
807         ;;
808 toolchain)
809         clear
810         prepareenv
811         beautify build_stage "Toolchain compilation - Native GCC: `gcc --version | grep GCC | awk {'print $3'}`"
812         buildtoolchain
813         BUILDMACHINE=`uname -m`
814         echo "`date -u '+%b %e %T'`: Create toolchain tar.gz for $BUILDMACHINE" | tee -a $LOGFILE
815         test -d $BASEDIR/cache/toolchains || mkdir $BASEDIR/cache/toolchains
816         cd $BASEDIR && tar -zc --exclude='log/_build.*.log' -f cache/toolchains/$SNAME-$VERSION-toolchain-$BUILDMACHINE.tar.gz \
817                 build/{bin,etc,usr/bin,usr/local} \
818                 build/tools/{bin,etc,*-linux-gnu,include,lib,libexec,sbin,share,var} \
819                 log >> $LOGFILE
820         md5sum cache/toolchains/$SNAME-$VERSION-toolchain-$BUILDMACHINE.tar.gz \
821                 > cache/toolchains/$SNAME-$VERSION-toolchain-$BUILDMACHINE.md5
822         stdumount
823         ;;
824 gettoolchain)
825         BUILDMACHINE=`uname -m`
826         # arbitrary name to be updated in case of new toolchain package upload
827         PACKAGE=$SNAME-$VERSION-toolchain-$BUILDMACHINE
828         if [ ! -f $BASEDIR/cache/toolchains/$PACKAGE.tar.gz ]; then
829                 URL_TOOLCHAIN=`grep URL_TOOLCHAIN lfs/Config | awk '{ print $3 }'`
830                 test -d $BASEDIR/cache/toolchains || mkdir $BASEDIR/cache/toolchains
831                 echo "`date -u '+%b %e %T'`: Load toolchain tar.gz for $BUILDMACHINE" | tee -a $LOGFILE
832                 cd $BASEDIR/cache/toolchains
833                 wget $URL_TOOLCHAIN/$PACKAGE.tar.gz $URL_TOOLCHAIN/$PACKAGE.md5 >& /dev/null
834                 if [ $? -ne 0 ]; then
835                         echo "`date -u '+%b %e %T'`: error downloading toolchain for $BUILDMACHINE machine" | tee -a $LOGFILE
836                 else
837                         if [ "`md5sum $PACKAGE.tar.gz | awk '{print $1}'`" = "`cat $PACKAGE.md5 | awk '{print $1}'`" ]; then
838                                 echo "`date -u '+%b %e %T'`: toolchain md5 ok" | tee -a $LOGFILE
839                         else
840                                 exiterror "$PACKAGE.md5 did not match, check downloaded package"
841                         fi
842                 fi
843         else
844                 echo "Toolchain is already downloaded. Exiting..."
845         fi
846         ;;
847 othersrc)
848         prepareenv
849         echo -ne "`date -u '+%b %e %T'`: Build sources iso for $MACHINE" | tee -a $LOGFILE
850         chroot $LFS /tools/bin/env -i   HOME=/root \
851         TERM=$TERM PS1='\u:\w\$ ' \
852         PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin \
853         VERSION=$VERSION NAME="$NAME" SNAME="$SNAME" MACHINE=$MACHINE \
854         /bin/bash -x -c "cd /usr/src/lfs && make -f sources-iso LFS_BASEDIR=/usr/src install" >>$LOGFILE 2>&1
855         mv $LFS/install/images/ipfire-* $BASEDIR >> $LOGFILE 2>&1
856         if [ $? -eq "0" ]; then
857                 beautify message DONE
858         else
859                 beautify message FAIL
860         fi
861         stdumount
862         ;;
863 git)
864         case "$2" in
865           update|up)
866                         git pull
867                 ;;
868           commit|ci)
869                 shift 2
870                         git commit $*
871                         
872                         [ "$?" -eq "0" ] || exiterror "git commit $* failed."
873                         
874                         echo -e "${BOLD}Do you want to push, too? [y/N]${NORMAL}"
875                         read
876                         [ -z $REPLY ] && exit 0
877                         for i in y Y j J; do
878                                 if [ "$i" == "$REPLY" ]; then
879                                         $0 git push
880                                         exit $?
881                                 fi
882                         done
883                         exiterror "\"$REPLY\" is not a valid answer."
884                 ;;
885           dist)
886                         git archive HEAD | gzip -9 > ${SNAME}-${VERSION}.tar.gz
887                   ;;
888           diff|di)
889                         echo -ne "Make a local diff to last revision"
890                         git diff HEAD > ipfire-diff-$(date +'%Y-%m-%d-%H:%M').diff
891                         evaluate 1
892                         echo "Diff was successfully saved to ipfire-diff-$(date +'%Y-%m-%d-%H:%M').diff"
893                         git diff --stat
894                 ;;
895           push)
896                 [ -z $GIT_USER ] && exiterror "You have to setup GIT_USER first."
897                         GIT_URL="ssh://${GIT_USER}@git.ipfire.org/pub/git/ipfire-2.x"
898                         
899                 git push ${GIT_URL} master
900                 ;;
901         esac
902         ;;
903 uploadsrc)
904         PWD=`pwd`
905         cd $BASEDIR/cache/
906         echo -e "Uploading cache to ftp server:"
907         ncftpls -u $FTP_CACHE_USER -p $FTP_CACHE_PASS ftp://$FTP_CACHE_URL/$FTP_CACHE_PATH/ > /tmp/ftplist
908         for i in *; do
909                 if [ "$(basename $i)" == "toolchains" ]; then continue; fi
910                 grep -q $(basename $i) /tmp/ftplist
911                 if [ "$?" -ne "0" ]; then
912                         echo -ne "$(basename $i)"
913                         ncftpput -u $FTP_CACHE_USER -p $FTP_CACHE_PASS $FTP_CACHE_URL $FTP_CACHE_PATH/ $(basename $i)
914                         if [ "$?" -ne "0" ]; then
915                                 beautify message FAIL
916                         fi
917                 fi
918         done
919         rm -f /tmp/ftplist
920         cd $PWD
921         exit 0
922         ;;
923 upload)
924         FTP_ISO_PORT=`echo "$FTP_ISO_URL" | awk -F: '{ print $2 }'`
925         FTP_ISO_URL=`echo "$FTP_ISO_URL" | awk -F: '{ print $1 }'`
926         if [ -z $FTP_ISO_PORT ]; then
927             FTP_ISO_PORT=21
928         fi
929         cat <<EOF > .ftp-commands
930 mkdir -p $FTP_ISO_PATH$SVN_REVISION
931 mkdir -p $FTP_ISO_PATH$SVN_REVISION/paks
932 quit
933 EOF
934         ncftp -u $FTP_ISO_USER -p $FTP_ISO_PASS -P $FTP_ISO_PORT $FTP_ISO_URL < .ftp-commands
935         rm -f .ftp-commands
936                 
937         case "$2" in
938           iso)
939                 echo -e "Uploading the iso to $FTP_ISO_PATH/$SVN_REVISION."
940
941                 md5sum ipfire-$VERSION.$MACHINE-full.iso > ipfire-$VERSION.$MACHINE-full.iso.md5
942                 for i in svn_status ipfire-source-r$SVN_REVISION.tar.gz ipfire-$VERSION.$MACHINE-full.iso ipfire-$VERSION.$MACHINE-full.iso.md5 ipfire-$VERSION.$MACHINE-devel.iso ipfire-$VERSION.$MACHINE-devel.iso.md5; do
943                                 if [ -e "$i" ]; then
944                             ncftpput -u $FTP_ISO_USER -p $FTP_ISO_PASS -P $FTP_ISO_PORT $FTP_ISO_URL $FTP_ISO_PATH$SVN_REVISION/ $i
945                                         if [ "$?" -eq "0" ]; then
946                                                 echo "The file with name $i was successfully uploaded to $FTP_ISO_URL$FTP_ISO_PATH$SVN_REVISION/."
947                                         else
948                                                 echo "There was an error while uploading the file $i to the ftp server."
949                                                 exit 1
950                                         fi
951                                 fi
952                 done
953                 rm -f ipfire-$VERSION.$MACHINE-full.iso.md5
954                 if [ "$3" = "--with-sources-cd" ]; then
955                         ncftpput -u $FTP_ISO_USER -p $FTP_ISO_PASS -P $FTP_ISO_PORT $FTP_ISO_URL $FTP_ISO_PATH/$SVN_REVISION/ ipfire-sources-cd-$VERSION.$MACHINE.iso
956                 fi
957                 ;;
958           paks)
959                 ncftpput -u $FTP_ISO_USER -p $FTP_ISO_PASS -P $FTP_ISO_PORT $FTP_ISO_URL $FTP_ISO_PATH$SVN_REVISION/paks packages/*
960                 if [ "$?" -eq "0" ]; then
961                         echo -e "The packages were successfully uploaded to $FTP_ISO_URL$FTP_ISO_PATH$SVN_REVISION/."
962                 else
963                         echo -e "There was an error while uploading the packages to the ftp server."
964                         exit 1
965                 fi
966           ;;
967         esac
968         ;;
969 batch)
970         if [ "$2" = "--background" ]; then
971                 batch_script
972                 exit $?
973         fi
974         if [ `screen -ls | grep -q ipfire` ]; then
975                 echo "Build is already running, sorry!"
976                 exit 1
977         else
978                 if [ "$2" = "--rebuild" ]; then
979                         export IPFIRE_REBUILD=1
980                         echo "REBUILD!"
981                 else
982                         export IPFIRE_REBUILD=0
983                 fi
984                 echo -en "${BOLD}***IPFire-Batch-Build is starting...${NORMAL}"
985                 screen -dmS ipfire $0 batch --background
986                 evaluate 1
987                 exit 0
988         fi
989         ;;
990 watch)
991         watch_screen
992         ;;
993 pxe)
994         case "$2" in
995           start)
996                 start_tftpd
997                 ;;
998           stop)
999                 stop_tftpd
1000                 ;;
1001           reload|restart)
1002                 reload_tftpd
1003                 ;;              
1004         esac
1005         exit 0
1006         ;;
1007 lang)
1008         update_langs
1009         ;;
1010 "")
1011         clear
1012         select name in "Exit" "IPFIRE: Downloadsrc" "IPFIRE: Build (silent)" "IPFIRE: Watch Build" "IPFIRE: Batch" "IPFIRE: Clean" "LOG: Tail" "Help"
1013         do
1014         case $name in
1015         "IPFIRE: Downloadsrc")
1016                 $0 downloadsrc
1017                 ;;
1018         "IPFIRE: Build (silent)")
1019                 $0 build-silent
1020                 ;;
1021         "IPFIRE: Watch Build")
1022                 $0 watch
1023                 ;;
1024         "IPFIRE: Batch")
1025                 $0 batch
1026                 ;;
1027         "IPFIRE: Clean")
1028                 $0 clean
1029                 ;;
1030         "Help")
1031                 echo "Usage: $0 {build|changelog|clean|gettoolchain|downloadsrc|shell|sync|toolchain}"
1032                 cat doc/make.sh-usage
1033                 ;;
1034         "LOG: Tail")
1035                 tail -f log/_*
1036                 ;;
1037         "Exit")
1038                 break
1039                 ;;
1040         esac
1041         done
1042         ;;
1043 config)
1044         make_config
1045         ;;
1046 *)
1047         echo "Usage: $0 {build|changelog|clean|gettoolchain|downloadsrc|shell|sync|toolchain}"
1048         cat doc/make.sh-usage
1049         ;;
1050 esac