cookutils view cook @ rev 368
Handle CROSS_BUGS (will move the few CROSS var to it so all cross configs are prefixed with CROSS_ in cook)
| author | Christophe Lincoln <pankso@slitaz.org> | 
|---|---|
| date | Thu May 10 04:37:24 2012 +0200 (2012-05-10) | 
| parents | c29544de5093 | 
| children | ae512633e579 | 
 line source
     1 #!/bin/sh
     2 #
     3 # Cook - A tool to cook and generate SliTaz packages. Read the README
     4 # before adding or modifing any code in cook!
     5 #
     6 # Copyright (C) SliTaz GNU/Linux - GNU gpl v3
     7 # Author: Christophe Lincoln <pankso@slitaz.org>
     8 #
     9 . /lib/libtaz.sh
    11 [ -f "/etc/slitaz/cook.conf" ] && . /etc/slitaz/cook.conf
    12 [ -f "cook.conf" ] && . ./cook.conf
    14 # Share DB and status with the Cooker.
    15 activity="$CACHE/activity"
    16 command="$CACHE/command"
    17 broken="$CACHE/broken"
    18 blocked="$CACHE/blocked"
    20 # Old style compatibility
    21 SOURCES_REPOSITORY=$SRC
    23 #
    24 # Functions
    25 #
    27 usage() {
    28 	cat << EOT
    30 $(echo -e "\033[1m$(gettext "Usage:")\033[0m") cook [package|command] [list|--option]
    32 $(echo -e "\033[1m$(gettext "Commands:")\033[0m")
    33   usage|help         $(gettext "Display this short usage.")
    34   setup              $(gettext "Setup your build environment.")
    35   arm-setup          $(gettext "Create packages DB lists and flavors.")
    36   test               $(gettext "Test environment and cook a package.")
    37   list-wok           $(gettext "List packages in the wok.")
    38   search             $(gettext "Simple packages search function.")
    39   new                $(gettext "Create a new package with a receipt".)
    40   list               $(gettext "Cook a list of packages.")
    41   clean-wok          $(gettext "Clean-up all packages files.")
    42   clean-src          $(gettext "Clean-up all packages sources.")
    43   pkgdb              $(gettext "Create packages DB lists and flavors.")
    45 $(echo -e "\033[1m$(gettext "Options:")\033[0m")
    46   --clean|-c         Cook : $(gettext "clean the package in the wok.")
    47   --install|-i       Cook : $(gettext "cook and install the package.")
    48   --getsrc|-gs       Cook : $(gettext "get the package source tarball.")
    49   --block|-b         Cook : $(gettext "Block a package so cook will skip it.")
    50   --unblock|-ub      Cook : $(gettext "Unblock a blocked package.")
    51   --interactive|-x   New  : $(gettext "create a receipt interactively.")
    52   --wok|-w           Setup: $(gettext "clone the cooking wok from Hg repo.")
    53   --stable           Setup: $(gettext "clone the stable wok from Hg repo.")
    54   --undigest         Setup: $(gettext "clone the undigest wok from Hg repo.")
    55   --flavors          Pkgdb: $(gettext "create up-to-date flavors files.")
    57 EOT
    58 	exit 0
    59 }
    61 # Log activities, we want first letter capitalized.
    62 log() {
    63 	grep ^[A-Z] | \
    64 		sed s"#^[A-Z]\([^']*\)#$(date '+%Y-%m-%d %H:%M') : \0#" >> $activity
    65 }
    67 # We don't want these escapes in web interface.
    68 clean_log() {
    69 	sed -i -e s'|\[70G\[ \[1;32m| |' \
    70 		-e s'|\[0;39m \]||' $LOGS/$pkg.log
    71 }
    73 # Log broken packages.
    74 broken() {
    75 	if ! grep -q "^$pkg$" $broken; then
    76 		echo "$pkg" >> $broken
    77 	fi
    78 }
    80 # Be sure package exists in wok.
    81 check_pkg_in_wok() {
    82 	if [ ! -d "$WOK/$pkg" ]; then
    83 		gettext -e "\nUnable to find package in the wok:"
    84 		echo -e " $pkg\n" && exit 1
    85 	fi
    86 }
    88 if_empty_value() {
    89 	if [ -z "$value" ]; then
    90 		gettext "QA: empty variable:"; echo -e " ${var}=\"\"\n"
    91 		exit 1
    92 	fi
    93 }
    95 # Initialize files used in $CACHE
    96 init_db_files() {
    97 	gettext "Creating directories structure in:"; echo " $SLITAZ"
    98 	mkdir -p $WOK $PKGS $SRC $CACHE $LOGS
    99 	gettext "Creating DB files in:"; echo " $CACHE"
   100 	for f in $activity $command $broken $blocked
   101 	do
   102 		touch $f
   103 	done
   104 }
   106 # QA: check a receipt consistency before building.
   107 receipt_quality() {
   108 	gettext -e "QA: checking package receipt...\n"
   109 	unset online
   110 	if ifconfig | grep -q -A 1 "^[a-z]*[0-9]" | fgrep 'addr:'; then
   111 		online="online"
   112 	fi
   113 	for var in PACKAGE VERSION CATEGORY SHORT_DESC MAINTAINER WEB_SITE
   114 	do
   115 		unset value
   116 		value="$(. $receipt ; eval echo \$$var)"
   117 		case "$var" in
   118 			PACKAGE|VERSION|SHORT_DESC)
   119 				if_empty_value ;;
   120 			CATEGORY)
   121 				[ -z "$value" ] && value="empty"
   122 				valid="base-system x-window utilities network graphics \
   123 					multimedia office development system-tools security games \
   124 					misc meta non-free"
   125 				if ! echo "$valid" | grep -q -w "$value"; then
   126 					gettext "QA: unknown category:"; echo -e " $value\n"
   127 					exit 1
   128 				fi ;;
   129 			WEB_SITE)
   130 				# We don't check WGET_URL since if dl is needed it will fail.
   131 				# Break also if we're not online. Here error is not fatal.
   132 				if_empty_value
   133 				[ -z "$online" ] || break
   134 				if ! busybox wget -T 12 -s $value 2>/dev/null; then
   135 					gettext "QA: Unable to reach:"; echo -e " $value"
   136 				fi ;;
   137 		esac
   138 	done
   139 }
   141 # Executed before sourcing a receipt.
   142 unset_receipt() {
   143 	unset DEPENDS BUILD_DEPENDS WANTED EXTRAVERSION WGET_URL PROVIDE TARBALL
   144 	unset HOST_ARCH CROSS_BUGS
   145 }
   147 # Paths used in receipt and by cook itself.
   148 set_paths() {
   149 	pkgdir=$WOK/$PACKAGE
   150 	src=$pkgdir/source/$PACKAGE-$VERSION
   151 	taz=$pkgdir/taz
   152 	pack=$taz/$PACKAGE-${VERSION}${EXTRAVERSION}
   153 	fs=$pack/fs
   154 	stuff=$pkgdir/stuff
   155 	install=$pkgdir/install
   156 	if [ "$WANTED" ]; then
   157 		wanted=${WANTED%% *}
   158 		src=$WOK/$wanted/source/$wanted-$VERSION
   159 		install=$WOK/$wanted/install
   160 		wanted_stuff=$WOK/$wanted/stuff
   161 	fi
   162 	# Kernel version is set from linux-api-headers since it is part of toolchain.
   163 	if [ -f "$INSTALLED/linux-api-headers/receipt" ]; then
   164 		kvers=$(grep ^VERSION= $INSTALLED/linux-api-headers/receipt | cut -d '"' -f 2)
   165 	fi
   166 	# Old way compatibility.
   167 	_pkg=$install
   168 }
   170 # Create source tarball when URL is a SCM.
   171 create_tarball() {
   172 	gettext "Creating tarball: "; echo "$tarball"
   173 	if [ "$LZMA_SRC" ]; then
   174 		tar -c $pkgsrc | lzma e $SRC/$tarball -si || exit 1
   175 	else
   176 		tar cjf $tarball $pkgsrc || exit 1
   177 		mv $tarball $SRC && rm -rf $pkgsrc
   178 	fi
   179 }
   181 # Get package source. For SCM we are in cache so clone here and create a
   182 # tarball here.
   183 get_source() {
   184 	pwd=$(pwd)
   185 	pkgsrc=${SOURCE:-$PACKAGE}-$VERSION
   186 	tarball=$pkgsrc.tar.bz2
   187 	[ "$LZMA_SRC" ] && tarball=$pkgsrc.tar.lzma
   188 	case "$WGET_URL" in
   189 		http://*|ftp://*)
   190 			# Busybox Wget is better!
   191 			busybox wget -T 60 -c -O $SRC/$TARBALL $WGET_URL || \
   192 				(echo -e "ERROR: wget $WGET_URL" && exit 1) ;;
   193 		https://*)
   194 			wget -c --no-check-certificate -O $SRC/$TARBALL $WGET_URL || \
   195 				(echo -e "ERROR: wget $WGET_URL" && exit 1) ;;
   196 		hg*|mercurial*)
   197 			if $(echo "$WGET_URL" | fgrep -q "hg|"); then
   198 				url=${WGET_URL#hg|}
   199 			else
   200 				url=${WGET_URL#mercurial|}
   201 			fi
   202 			gettext -e "Getting source from Hg...\n"
   203 			echo "URL: $url"
   204 			gettext "Cloning to: "; echo "$pwd/$pkgsrc"
   205 			if [ "$BRANCH" ]; then
   206 				echo "Hg branch: $BRANCH"
   207 				hg clone $url --rev $BRANCH $pkgsrc || \
   208 					(echo "ERROR: hg clone $url --rev $BRANCH" && exit 1)
   209 			else
   210 				hg clone $url $pkgsrc || (echo "ERROR: hg clone $url" && exit 1)
   211 			fi
   212 			rm -rf $pkgsrc/.hg
   213 			create_tarball ;;
   214 		git*)
   215 			url=${WGET_URL#git|}
   216 			gettext -e "Getting source from Git...\n"
   217 			echo "URL: $url"
   218 			git clone $url $pkgsrc || (echo "ERROR: git clone $url" && exit 1)
   219 			if [ "$BRANCH" ]; then
   220 				echo "Git branch: $BRANCH"
   221 				cd $pkgsrc && git checkout $BRANCH && cd ..
   222 			fi
   223 			create_tarball ;;
   224 		cvs*)
   225 			url=${WGET_URL#cvs|}
   226 			mod=$PACKAGE
   227 			[ "$CVS_MODULE" ] && mod=$CVS_MODULE
   228 			gettext -e "Getting source from CVS...\n"
   229 			echo "URL: $url"
   230 			[ "$CVS_MODULE" ] && echo "CVS module: $mod"
   231 			gettext "Cloning to: "; echo "$pwd/$mod"
   232 			cvs -d:$url co $mod && mv $mod $pkgsrc
   233 			create_tarball ;;
   234 		svn*|subversion*)
   235 			if $(echo "$WGET_URL" | fgrep -q "svn|"); then
   236 				url=${WGET_URL#svn|}
   237 			else
   238 				url=${WGET_URL#subversion|}
   239 			fi
   240 			gettext -e "Getting source from SVN...\n"
   241 			echo "URL: $url"
   242 			if [ "$BRANCH" ]; then
   243 				echo t | svn co $url -r $BRANCH $pkgsrc
   244 			else
   245 				echo t | svn co $url $pkgsrc
   246 			fi
   247 			create_tarball ;;
   248 		*)
   249 			gettext -e "\nERROR: Unable to handle:"; echo -e " $WGET_URL \n" | \
   250 				tee -a $LOGS/$PACKAGE.log
   251 			exit 1 ;;
   252 	esac
   253 }
   255 # Extract source package.
   256 extract_source() {
   257 	if [ ! -s "$SRC/$TARBALL" ]; then
   258 		local url
   259 		url="http://mirror.slitaz.org/sources/packages"
   260 		url=$url/${TARBALL:0:1}/$TARBALL
   261 		gettext "Getting source from mirror:"; echo " $url"
   262 		busybox wget -c -P $SRC $url || echo -e "ERROR: wget $url"
   263 	fi
   264 	gettext "Extracting:"; echo " $TARBALL"
   265 	case "$TARBALL" in
   266 		*.tar.gz|*.tgz) tar xzf $SRC/$TARBALL 2>/dev/null ;;
   267 		*.tar.bz2|*.tbz|*.tbz2) tar xjf $SRC/$TARBALL 2>/dev/null ;;
   268 		*.tar.lzma) tar xaf $SRC/$TARBALL ;;
   269 		*.tar) tar xf $SRC/$TARBALL ;;
   270 		*.zip|*.xpi) unzip -o $SRC/$TARBALL ;;
   271 		*.xz) unxz -c $SRC/$TARBALL | tar xf - ;;
   272 		*.Z) uncompress -c $SRC/$TARBALL | tar xf - ;;
   273 		*.rpm) rpm2cpio $SRC/$TARBALL | cpio -idm --quiet ;;
   274 		*.run) /bin/sh $SRC/$TARBALL $RUN_OPTS ;;
   275 		*) cp $SRC/$TARBALL $(pwd) ;;
   276 	esac
   277 }
   279 # Display cooked package summary.
   280 summary() {
   281 	cd $WOK/$pkg
   282 	[ -d install ] && prod=$(du -sh install | awk '{print $1}' 2>/dev/null)
   283 	fs=$(du -sh taz/* | awk '{print $1}')
   284 	size=$(du -sh $PKGS/$pkg-${VERSION}*.tazpkg | awk '{print $1}')
   285 	files=$(cat taz/$pkg-*/files.list | wc -l)
   286 	cookdate=$(date "+%Y-%m-%d %H:%M")
   287 	sec=$time
   288 	div=$(( ($time + 30) / 60))
   289 	[ "$div" != 0 ] && min="~ ${div}m"
   290 	gettext "Summary for:"; echo " $PACKAGE $VERSION"
   291 	separator
   292 	[ "$prod" ] && echo "Produced   : $prod"
   293 	cat << EOT
   294 Packed     : $fs
   295 Compressed : $size
   296 Files      : $files
   297 Cook time  : ${sec}s $min
   298 Cook date  : $cookdate
   299 Arch type  : $ARCH
   300 $(separator)
   301 EOT
   302 }
   304 # Display debugging error info.
   305 debug_info() {
   306 	echo -e "\nDebug information"
   307 	separator
   308 	echo "Cook date: $(date '+%Y-%m-%d %H:%M')"
   309 	for error in \
   310 		ERROR "No package" "cp: can't" "can't open" "can't cd" \
   311 		"error:" "fatal error:"
   312 	do
   313 		fgrep "$error" $LOGS/$pkg.log
   314 	done
   315 	separator && echo ""
   316 }
   318 # Copy all generic files (locale, pixmaps, .desktop). We use standard paths,
   319 # so some packages need to copy these files with the receipt and genpkg_rules.
   320 copy_generic_files()
   321 {
   322 	# $LOCALE is set in cook.conf
   323 	if [ "$LOCALE" ]; then
   324 		if [ -d "$install/usr/share/locale" ]; then
   325 			mkdir -p $fs/usr/share/locale
   326 			for i in $LOCALE
   327 			do
   328 				if [ -d "$install/usr/share/locale/$i" ]; then
   329 					cp -a $install/usr/share/locale/$i $fs/usr/share/locale
   330 				fi
   331 			done
   332 		fi
   333 	fi
   335 	# Generic pixmaps copy can be disabled with GENERIC_PIXMAPS="no"
   336 	if [ "$GENERIC_PIXMAPS" != "no" ]; then
   337 		if [ -d "$install/usr/share/pixmaps" ]; then
   338 			mkdir -p $fs/usr/share/pixmaps
   339 			cp -a $install/usr/share/pixmaps/$PACKAGE.png \
   340 				$fs/usr/share/pixmaps 2>/dev/null || continue
   341 			cp -a $install/usr/share/pixmaps/$PACKAGE.xpm \
   342 				$fs/usr/share/pixmaps 2>/dev/null || continue
   343 		fi
   345 		# Custom or homemade PNG pixmap can be in stuff.
   346 		if [ -f "$stuff/$PACKAGE.png" ]; then
   347 			mkdir -p $fs/usr/share/pixmaps
   348 			cp -a $stuff/$PACKAGE.png $fs/usr/share/pixmaps
   349 		fi
   350 	fi
   352 	# Desktop entry (.desktop).
   353 	# Generic desktop entry copy can be disabled with GENERIC_MENUS="no"
   354 	if [ "$GENERIC_MENUS" != "no" ]; then
   355 		if [ -d "$install/usr/share/applications" ] && [ "$WANTED" == "" ]; then
   356 			cp -a $install/usr/share/applications $fs/usr/share
   357 		fi
   358 	fi
   360 	# Homemade desktop file(s) can be in stuff.
   361 	if [ -d "$stuff/applications" ]; then
   362 		mkdir -p $fs/usr/share
   363 		cp -a $stuff/applications $fs/usr/share
   364 	fi
   365 	if [ -f "$stuff/$PACKAGE.desktop" ]; then
   366 		mkdir -p $fs/usr/share/applications
   367 		cp -a $stuff/$PACKAGE.desktop $fs/usr/share/applications
   368 	fi
   369 }
   371 # Find and strip : --strip-all (-s) or --strip-debug on static libs as well
   372 # as removing uneeded files like in Python packages.
   373 strip_package()
   374 {
   375 	gettext "Executing strip on all files..."
   376 	for dir in $fs/bin $fs/sbin $fs/usr/bin $fs/usr/sbin $fs/usr/games
   377 	do
   378 		if [ -d "$dir" ]; then
   379 			find $dir -type f -exec strip -s '{}' 2>/dev/null \;
   380 		fi
   381 	done
   382 	find $fs -name "*.so*" -exec strip -s '{}' 2>/dev/null \;
   383 	find $fs -name "*.a" -exec strip --strip-debug '{}' 2>/dev/null \;
   384 	status
   386 	# Remove Python .pyc and .pyo from packages.
   387 	if echo "$PACKAGE $DEPENDS" | fgrep -q "python"; then
   388 		gettext "Removing Python compiled files..."
   389 		find $fs -type f -name "*.pyc" -delete 2>/dev/null
   390 		find $fs -type f -name "*.pyo" -delete 2>/dev/null
   391 		status
   392 	fi
   394 	# Remove Perl perllocal.pod and .packlist from packages.
   395 	if echo "$DEPENDS" | fgrep -q "perl"; then
   396 		gettext "Removing Perl compiled files..."
   397 		find $fs -type f -name "perllocal.pod" -delete 2>/dev/null
   398 		find $fs -type f -name ".packlist" -delete 2>/dev/null
   399 		status
   400 	fi
   401 }
   403 # Remove installed deps.
   404 remove_deps() {
   405 	# Now remove installed build deps.
   406 	diff="$CACHE/installed.cook.diff"
   407 	if [ -s "$CACHE/installed.cook.diff" ]; then
   408 		deps=$(cat $diff | grep ^+[a-zA-Z0-9] | sed s/^+//)
   409 		nb=$(cat $diff | grep ^+[a-zA-Z0-9] | wc -l)
   410 		gettext "Build dependencies to remove:"; echo " $nb"
   411 		gettext "Removing:"
   412 		for dep in $deps
   413 		do
   414 			echo -n " $dep"
   415 			echo 'y' | tazpkg remove $dep >/dev/null
   416 		done
   417 		echo -e "\n"
   418 		# Keep the last diff for debug and info.
   419 		mv -f $CACHE/installed.cook.diff $CACHE/installed.diff
   420 	fi
   421 }
   423 # The main cook function.
   424 cookit() {
   425 	echo "Cook: $PACKAGE $VERSION"
   426 	separator
   427 	set_paths
   429 	# Handle cross compilation.
   430 	if [ "$HOST_ARCH" ] && [ ! $(echo "$HOST_ARCH" | fgrep $ARCH) ]; then
   431 		echo "ERROR: This package dont cook or is not include in: $ARCH"
   432 		[ "$CROSS_BUGS" ] && echo "Bug: $CROSS_BUGS"
   433 		exit 1
   434 	fi
   435 	case "$ARCH" in
   436 		arm|x86_64)
   437 			echo "cook: adding /usr/cross/$ARCH/bin to PATH"
   438 			export PATH=$PATH:/usr/cross/$ARCH/bin
   439 			echo "cook: using C compiler $HOST_SYSTEM-gcc"
   440 			export CC=${HOST_SYSTEM}-gcc
   441 			export CXX=${HOST_SYSTEM}-g++ ;;
   442 	esac
   444 	[ "$QA" ] && receipt_quality
   445 	cd $pkgdir
   446 	rm -rf install taz source
   448 	# Disable -pipe if less than 512Mb free RAM.
   449 	free=$(free | fgrep '/+ buffers' | tr -s ' ' | cut -f 4 -d ' ')
   450 	if [ "$free" -lt 524288 ] && [ "$CFLAGS" != "${CFLAGS/-pipe}" ]; then
   451 			gettext -e "Disabling -pipe compile flag: $free RAM\n"
   452 			CFLAGS="${CFLAGS/-pipe}" && CFLAGS=$(echo "$CFLAGS" | tr -s ' ')
   453 			CXXFLAGS="${CXXFLAGS/-pipe}" && \
   454 				CXXFLAGS=$(echo "$CXXFLAGS" | tr -s ' ')
   455 	fi
   456 	unset free
   458 	# Export flags and path to be used by make and receipt.
   459 	DESTDIR=$pkgdir/install
   460 	export DESTDIR MAKEFLAGS CFLAGS CXXFLAGS CONFIG_SITE LC_ALL=C LANG=C
   461 	#LDFLAGS
   463 	# Check for build deps and handle implicit depends of *-dev packages
   464 	# (ex: libusb-dev :: libusb).
   465 	rm -f $CACHE/installed.local $CACHE/installed.web $CACHE/missing.dep
   466 	touch $CACHE/installed.local $CACHE/installed.web
   467 	[ "$BUILD_DEPENDS" ] && gettext -e "Checking build dependencies...\n"
   468 	for dep in $BUILD_DEPENDS
   469 	do
   470 		implicit=${dep%-dev}
   471 		for i in $dep $implicit
   472 		do
   473 			if [ ! -f "$INSTALLED/$i/receipt" ]; then
   474 				# Try local package first. In some cases implicit doesn't exist, ex:
   475 				# libboost-dev exists but not libboost, so check if we got vers.
   476 				unset vers
   477 				vers=$(. $WOK/$i/receipt 2>/dev/null ; echo $VERSION)
   478 				if [ -f "$PKGS/$i-$vers.tazpkg" ]; then
   479 					echo $i-$vers.tazpkg >> $CACHE/installed.local
   480 				else
   481 					# Priority to package version in wok (maybe more up-to-date)
   482 					# than the mirrored one.
   483 					if [ "$vers" ]; then
   484 						if fgrep -q $i-$vers $DB/packages.list; then
   485 							echo $i >> $CACHE/installed.web
   486 						else
   487 							# So package exists in wok but not available.
   488 							gettext "Missing dep (wok/pkg):"; echo " $i $vers"
   489 							echo $i >> $CACHE/missing.dep
   490 						fi
   491 					else
   492 						# Package is not in wok but may be in repo.
   493 						if fgrep -q $i-$vers $DB/packages.list; then
   494 							echo $i >> $CACHE/installed.web
   495 						else
   496 							echo "ERROR: unknown dep $i" && exit 1
   497 						fi
   498 					fi
   499 				fi
   500 			fi
   501 		done
   502 	done
   504 	# Get the list of installed packages
   505 	cd $INSTALLED && ls -1 > $CACHE/installed.list
   507 	# Have we a missing build dep to cook ?
   508 	if [ -s "$CACHE/missing.dep" ] && [ "$AUTO_COOK" ]; then
   509 		gettext -e "Auto cook config is set   : AUTO_COOK\n"
   510 		cp -f $LOGS/$PACKAGE.log $LOGS/$PACKAGE.log.$$
   511 		for i in $(uniq $CACHE/missing.dep)
   512 		do
   513 			(gettext "Building dep (wok/pkg)    :"; echo " $i $vers") | \
   514 				tee -a $LOGS/$PACKAGE.log.$$
   515 			cook $i || (echo -e "ERROR: can't cook dep '$i'\n" && \
   516 				fgrep "remove: " $LOGS/$i.log && \
   517 				fgrep "Removing: " $LOGS/$i.log && echo "") | \
   518 				tee -a $LOGS/$PACKAGE.log.$$ && break
   519 		done
   520 		rm -f $CACHE/missing.dep
   521 		mv  $LOGS/$PACKAGE.log.$$ $LOGS/$PACKAGE.log
   522 	fi
   524 	# QA: Exit on missing dep errors. We exit in both cases, if AUTO_COOK
   525 	# is enabled and cook fails we have ERROR in log, if no auto cook we have
   526 	# missing dep in cached file.
   527 	if fgrep -q "ERROR:" $LOGS/$pkg.log || [ -s "$CACHE/missing.dep" ]; then
   528 		[ -s "$CACHE/missing.dep" ] && nb=$(cat $CACHE/missing.dep | wc -l)
   529 		echo "ERROR: missing dep $nb" && exit 1
   530 	fi
   532 	# Install local packages.
   533 	cd $PKGS
   534 	for i in $(uniq $CACHE/installed.local)
   535 	do
   536 		gettext "Installing dep (pkg/local):"; echo " $i"
   537 		tazpkg install $i >/dev/null
   538 	done
   540 	# Install web or cached packages (if mirror is set to $PKGS we only
   541 	# use local packages).
   542 	for i in $(uniq $CACHE/installed.web)
   543 	do
   544 		gettext "Installing dep (web/cache):"; echo " $i"
   545 		tazpkg get-install $i >/dev/null
   546 	done
   548 	# If a cook failed deps are removed.
   549 	cd $INSTALLED && ls -1 > $CACHE/installed.cook && cd $CACHE
   550 	[ ! -s "installed.cook.diff" ] && \
   551 		busybox diff installed.list installed.cook > installed.cook.diff
   552 	deps=$(cat installed.cook.diff | grep ^+[a-zA-Z0-9] | wc -l)
   554 	# Get source tarball and make sure we have source dir named:
   555 	# $PACKAGE-$VERSION to be standard in receipts. Here we use tar.lzma
   556 	# tarball if it exists.
   557 	if [ "$WGET_URL" ] && [ ! -f "$SRC/$TARBALL" ]; then
   558 		if [ -f "$SRC/${SOURCE:-$PACKAGE}-$VERSION.tar.lzma" ]; then
   559 			TARBALL=${SOURCE:-$PACKAGE}-$VERSION.tar.lzma
   560 			LZMA_SRC=""
   561 		else
   562 			get_source || exit 1
   563 		fi
   564 	fi
   565 	if [ ! "$WANTED" ] && [ "$TARBALL" ] && [ ! -d "$src" ]; then
   566 		mkdir -p $pkgdir/source/tmp && cd $pkgdir/source/tmp
   567 		if ! extract_source ; then
   568 			get_source
   569 			extract_source || exit 1
   570 		fi
   571 		if [ "$LZMA_SRC" ]; then
   572 			cd $pkgdir/source
   573 			if [ "$(ls -A tmp | wc -l)" -gl 1 ] || [ -f "$(echo tmp/*)" ]; then
   574 				mv tmp tmp-1 && mkdir tmp
   575 				mv tmp-1 tmp/${SOURCE:-$PACKAGE}-$VERSION
   576 			fi
   577 			if [ -d "tmp/${SOURCE:-$PACKAGE}-$VERSION" ]; then
   578 				cd tmp && tar -c * | lzma e $SRC/$TARBALL -si
   579 			fi
   580 		fi
   581 		cd $pkgdir/source/tmp
   582 		# Some archives are not well done and don't extract to one dir (ex lzma).
   583 		files=$(ls | wc -l)
   584 		[ "$files" == 1 ] && [ -d "$(ls)" ] && mv * ../$PACKAGE-$VERSION
   585 		[ "$files" == 1 ] && [ -f "$(ls)" ] && mkdir -p ../$PACKAGE-$VERSION && \
   586 			mv * ../$PACKAGE-$VERSION/$TARBALL
   587 		[ "$files" -gt 1 ]  && mkdir -p ../$PACKAGE-$VERSION && \
   588 			mv * ../$PACKAGE-$VERSION
   589 		cd .. && rm -rf tmp
   590 	fi
   592 	# Execute receipt rules.
   593 	if grep -q ^compile_rules $receipt; then
   594 		echo "Executing: compile_rules"
   595 		echo "CFLAGS   : $CFLAGS"
   596 		#echo "LDFLAGS  : $LDFLAGS"
   597 		[ -d "$src" ] && cd $src
   598 		compile_rules $@ || exit 1
   599 		# Stay compatible with _pkg
   600 		[ -d "$src/_pkg" ] && mv $src/_pkg $install
   601 		# QA: compile_rules success so valid.
   602 		mkdir -p $install
   603 	else
   604 		# QA: No compile_rules so no error, valid.
   605 		mkdir -p $install
   606 	fi
   607 	separator && echo ""
   609 	# Execute testsuite.
   610 	if grep -q ^testsuite $receipt; then
   611 		echo "Running testsuite"
   612 		separator
   613 		testsuite $@ || exit 1
   614 		separator && echo ""
   615 	fi
   616 }
   618 # Cook quality assurance.
   619 cookit_quality() {
   620 	if [ ! -d "$WOK/$pkg/install" ] && [ ! "$WANTED" ]; then
   621 		echo -e "ERROR: cook failed" | tee -a $LOGS/$pkg.log
   622 	fi
   623 	# ERROR can be echoed any time in cookit()
   624 	if fgrep -q ERROR: $LOGS/$pkg.log; then
   625 		debug_info | tee -a $LOGS/$pkg.log
   626 		rm -f $command && exit 1
   627 	fi
   628 }
   630 # Create the package. Wanted to use Tazpkg to create a tazpkg package at first,
   631 # but it doesn't handle EXTRAVERSION.
   632 packit() {
   633 	set_paths
   635 	# Handle cross compilation
   636 	case "$ARCH" in
   637 		arm|x86_64) VERSION="$VERSION-$ARCH" ;;
   638 	esac
   640 	echo "Pack: $PACKAGE $VERSION"
   641 	separator
   643 	if grep -q ^genpkg_rules $receipt; then
   644 		gettext -e "Executing: genpkg_rules\n"
   645 		set -e && cd $pkgdir && mkdir -p $fs
   646 		genpkg_rules || echo -e "\nERROR: genpkg_rules failed\n" >> \
   647 			$LOGS/$pkg.log
   648 	else
   649 		gettext "No packages rules: meta package"; echo
   650 		mkdir -p $fs
   651 	fi
   653 	# First QA check to stop now if genpkg_rules failed.
   654 	if fgrep -q ERROR: $LOGS/$pkg.log; then
   655 		exit 1
   656 	fi
   658 	cd $taz
   659 	for file in receipt description.txt
   660 	do
   661 		[ ! -f "../$file" ] && continue
   662 		gettext "Copying"; echo -n " $file..."
   663 		cp -f ../$file $pack && chown 0.0 $pack/$file && status
   664 	done
   665 	copy_generic_files
   667 	# Create files.list with redirecting find output.
   668 	gettext "Creating the list of files..." && cd $fs
   669 	find . -type f -print > ../files.list
   670 	find . -type l -print >> ../files.list
   671 	cd .. && sed -i s/'^.'/''/ files.list
   672 	status
   674 	# Strip and stuff files.
   675 	strip_package
   677 	# Md5sum of files.
   678 	gettext "Creating md5sum of files..."
   679 	while read file; do
   680 		[ -L "fs$file" ] && continue
   681 		[ -f "fs$file" ] || continue
   682 		case "$file" in
   683 			/lib/modules/*/modules.*|*.pyc) continue ;;
   684 		esac
   685 		md5sum "fs$file" | sed 's/  fs/  /'
   686 	done < files.list > md5sum
   687 	status
   688 	UNPACKED_SIZE=$(du -chs fs receipt files.list md5sum \
   689 		description.txt 2> /dev/null | awk \
   690 		'{ sz=$1 } END { print sz }')
   692 	# Build cpio archives.
   693 	gettext "Compressing the fs... "
   694 	find fs | cpio -o -H newc --quiet | lzma e fs.cpio.lzma -si
   695 	rm -rf fs
   696 	status
   697 	PACKED_SIZE=$(du -chs fs.cpio.lzma receipt files.list \
   698 		md5sum description.txt 2> /dev/null | awk \
   699 		'{ sz=$1 } END { print sz }')
   700 	gettext "Updating receipt sizes..."
   701 	sed -i s/^PACKED_SIZE.*$// receipt
   702 	sed -i s/^UNPACKED_SIZE.*$// receipt
   703 	sed -i "s/^PACKAGE=/PACKED_SIZE=\"$PACKED_SIZE\"\nUNPACKED_SIZE=\"$UNPACKED_SIZE\"\nPACKAGE=/" receipt
   704 	status
   706 	# Set extra version.
   707 	if [ "$EXTRAVERSION" ]; then
   708 		gettext "Updating receipt EXTRAVERSION: "; echo -n "$EXTRAVERSION"
   709 		sed -i s/^EXTRAVERSION.*$// receipt
   710 		sed -i "s/^VERSION=/EXTRAVERSION=\"$EXTRAVERSION\"\nVERSION=/" receipt
   711 		status
   712 	fi
   714 	# Compress.
   715 	gettext "Creating full cpio archive... "
   716 	find . -print | cpio -o -H newc --quiet > \
   717 		../$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg
   718 	status
   719 	gettext "Restoring original package tree... "
   720 	unlzma -c fs.cpio.lzma | cpio -idm --quiet
   721 	status
   722 	rm fs.cpio.lzma && cd ..
   724 	# QA and give info.
   725 	tazpkg=$(ls *.tazpkg)
   726 	packit_quality
   727 	separator && gettext "Package:"; echo -e " $tazpkg\n"
   728 }
   730 # Verify package quality and consistency.
   731 packit_quality() {
   732 	#gettext "QA: Checking for broken link..."
   733 	#link=$(find $fs/usr -type l -follow)
   734 	#[ "$link" ] && echo -e "\nERROR: broken link in filesystem"
   735 	#status
   737 	# Exit if any error found in log file.
   738 	if fgrep -q ERROR: $LOGS/$pkg.log; then
   739 		rm -f $command && exit 1
   740 	fi
   742 	gettext "QA: Checking for empty package..."
   743 	files=$(cat $WOK/$pkg/taz/$pkg-*/files.list | wc -l)
   744 	if [ "$files" == 0 ] && [ "$CATEGORY" != "meta" ]; then
   745 		echo -e "\nERROR: empty package"
   746 		rm -f $command && exit 1
   747 	else
   748 		# Ls sort by name so the first file is the one we want.
   749 		old=$(ls $PKGS/$pkg-*.tazpkg 2>/dev/null | head -n 1)
   750 		status
   751 		if [ -f "$old" ]; then
   752 			gettext "Removing old: $(basename $old)"
   753 			rm -f $old && status
   754 		fi
   755 		mv -f $pkgdir/taz/$pkg-*.tazpkg $PKGS
   756 		sed -i /^${pkg}$/d $broken
   757 		#gettext "Removing source tree..."
   758 		#rm -f $WOK/$pkg/source && status
   759 	fi
   760 }
   762 mkinstall_list() {
   763 	local pkg
   764 	for pkg in $@ ; do
   765 		case " $INSTALL_LIST " in
   766 		*\ $pkg\ *) continue ;;
   767 		*)	if [ -s /home/slitaz/wok/$pkg/receipt ]; then
   768 				unset DEPENDS
   769 				. /home/slitaz/wok/$pkg/receipt
   770 				INSTALL_LIST="$INSTALL_LIST $pkg"
   771 				[ -n "$DEPENDS" ] && mkinstall_list $DEPENDS
   772 			fi
   773 			echo $pkg
   774 		esac
   775 	done
   776 }
   778 tac()
   779 {
   780 	sed '1!G;h;$!d' $1
   781 }
   783 # Launch the cook command into a chroot jail protected by aufs.
   784 # The current filesystems are used read-only and updates are
   785 # stored in a separate branch.
   786 try_aufs_chroot() {
   788 	base=/dev/shm/aufsmnt$$
   790 	# Can we setup the chroot ? Is it already done ?
   791 	grep -q ^AUFS_NOT_SUPPORTED $receipt && return
   792 	[ -n "$AUFS_MOUNTS" -a ! -f /aufs-umount.sh ] || return
   793 	lsmod | grep -q aufs || modprobe aufs 2> /dev/null || return
   794 	mkdir ${base}root ${base}rw || return
   796 	echo "Setup aufs chroot..."
   798 	# Sanity check
   799 	for i in / /proc /sys /dev/shm /home ; do
   800 		case " $AUFS_MOUNTS " in
   801 		*\ $i\ *) ;;
   802 		*)	AUFS_MOUNTS="$AUFS_MOUNTS $i" ;;
   803 		esac
   804 	done
   805 	for mnt in $(echo $AUFS_MOUNTS | sort | uniq); do
   806 		mount --bind $mnt ${base}root$mnt
   807 		if [ $mnt == / ] && ! mount -t aufs -o br=${base}rw:/ none ${base}root; then
   808 	    		echo "Aufs mountage failure"
   809 	    		umount ${base}root
   810 	    		rmdir ${base}*
   811 	    		return
   812 		fi
   813 		echo "umount ${base}root$mnt" >> ${base}rw/aufs-umount.sh
   814 	done
   816 	chroot ${base}root $(cd $(dirname $0); pwd)/$(basename $0) "$@"
   817 	status=$?
   819 	echo "Leaving aufs chroot..."
   820 	tac ${base}rw/aufs-umount.sh | sh
   821 	rm -rf ${base}rw
   822 	umount ${base}root
   823 	rmdir $base*
   825 	# Install package if requested
   826 	if [ "$inst" ]; then
   827 		if [ -f "$PKGS/$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg" ]; then
   828 			cd $PKGS && tazpkg install \
   829 				$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg --forced
   830 		else
   831 			gettext -e "Unable to install package, build has failed.\n\n"
   832 			exit 1
   833 		fi
   834 	fi
   836 	# Install package if part of the chroot to keep env up-to-date.
   837 	if [ -d "$INSTALLED/$pkg" ]; then
   838 		. /etc/slitaz/cook.conf
   839 		. $WOK/$pkg/taz/$pkg-*/receipt
   840 		echo "Updating chroot environment..."
   841 		echo "Updating chroot: $pkg (${VERSION}${EXTRAVERSION})" | log
   842 		cd $PKGS && tazpkg install \
   843 			$pkg-${VERSION}${EXTRAVERSION}.tazpkg --forced
   844 	fi
   845 	exit $status
   846 }
   848 # Create a XML feed for freshly builded package.
   849 gen_rss() {
   850 	pubdate=$(date "+%a, %d %b %Y %X")
   851 	cat > $FEEDS/$pkg.xml << EOT
   852 	<item>
   853 		<title>$PACKAGE $VERSION${EXTRAVERSION}</title>
   854 		<link>${COOKER_URL}?pkg=$PACKAGE</link>
   855 		<guid>$PACKAGE-$VERSION${EXTRAVERSION}</guid>
   856 		<pubDate>$pubdate</pubDate>
   857 		<description>$SHORT_DESC</description>
   858 	</item>
   859 EOT
   860 }
   862 #
   863 # Commands
   864 #
   866 case "$1" in
   867 	usage|help|-u|-h)
   868 		 usage ;;
   869 	list-wok)
   870 		gettext -e "\nList of packages in:"; echo " $WOK"
   871 		separator
   872 		cd $WOK && ls -1
   873 		separator
   874 		echo -n "Packages: " && ls | wc -l
   875 		echo "" ;;
   876 	search)
   877 		# Just a simple search function, we dont need more actually.
   878 		query="$2"
   879 		gettext -e "\nSearch results for:"; echo " $query"
   880 		separator
   881 		cd $WOK && ls -1 | grep "$query"
   882 		separator && echo "" ;;
   883 	setup)
   884 		# Setup a build environment
   885 		check_root
   886 		echo "Cook: setting up the environment" | log
   887 		gettext -e "\nSetting up your environment\n"
   888 		separator && cd $SLITAZ
   889 		init_db_files
   890 		gettext -e "Checking for packages to install...\n"
   891 		case " $@ " in
   892 		*\ --reinstall\ *)
   893 			INSTALL_LIST=""
   894 			for pkg in $(mkinstall_list $SETUP_PKGS); do
   895 				tazpkg get-install $pkg --forced
   896 			done ;;
   897 		*)
   898 			for pkg in $SETUP_PKGS
   899 			do
   900 				[ ! -f "$INSTALLED/$pkg/receipt" ] &&
   901 					tazpkg get-install $pkg
   902 			done ;;
   903 		esac
   905 		# Handle --options
   906 		case "$2" in
   907 			--wok|-w)
   908 				hg clone $WOK_URL wok || exit 1 ;;
   909 			--stable)
   910 				hg clone $WOK_URL-stable wok || exit 1 ;;
   911 			--undigest)
   912 				hg clone $WOK_URL-undigest wok || exit 1 ;;
   913 		esac
   915 		# SliTaz group and permissions
   916 		if ! grep -q ^slitaz /etc/group; then
   917 			gettext -e "Adding group: slitaz\n"
   918 			addgroup slitaz
   919 		fi
   920 		gettext -e "Setting permissions for slitaz group...\n"
   921 		find $SLITAZ -maxdepth 2 -exec chown root.slitaz {} \;
   922 		find $SLITAZ -maxdepth 2 -exec chmod g+w {} \;
   923 		separator
   924 		gettext -e "All done, ready to cook packages :-)\n\n" ;;
   925 	arm-setup)
   926 		# Setup fro cross ARM compiling.
   927 		sed -i \
   928 			-e s'/ARCH=.*/ARCH="arm"/' \
   929 			-e s'/CFLAGS=.*/CFLAGS=""/' \
   930 			-e s'/BUILD_SYSTEM=.*/BUILD_SYSTEM=i486-slitaz-linux/' \
   931 			-e s'/HOST_SYSTEM=.*/HOST_SYSTEM=$ARCH-slitaz-linux-gnueabi/' \
   932 				/etc/slitaz/cook.conf
   933 		. /etc/slitaz/cook.conf
   934 		echo "Target arch   : $ARCH"
   935 		echo "Configure args: $CONFIGURE_ARGS"
   936 		if [ -x /usr/cross/$ARCH/bin/${HOST_SYSTEM}-gcc ]; then
   937 			echo "Cross compiler: ${HOST_SYSTEM}-gcc"
   938 		else
   939 			echo "ERROR: C compiler is missing: ${HOST_SYSTEM}-gcc"
   940 			echo "Run 'cross compile' for cook a toolchain"
   941 		fi ;;
   942 	test)
   943 		# Test a cook environment.
   944 		echo "Cook test: testing the cook environment" | log
   945 		[ ! -d "$WOK" ] && exit 1
   946 		[ ! -d "$WOK/cooktest" ] && cp -r $DATA/cooktest $WOK
   947 		cook cooktest ;;
   948 	new)
   949 		# Create the package folder and an empty receipt.
   950 		pkg="$2"
   951 		[ "$pkg" ] || usage
   952 		echo ""
   953 		if [ -d "$WOK/$pkg" ]; then
   954 			echo -n  "$pkg " && gettext "package already exists."
   955 			echo -e "\n" && exit 1
   956 		fi
   957 		gettext "Creating"; echo -n " $WOK/$pkg"
   958 		mkdir $WOK/$pkg && cd $WOK/$pkg && status
   959 		gettext "Preparing the package receipt..."
   960 		cp $DATA/receipt .
   961 		sed -i s"/^PACKAGE=.*/PACKAGE=\"$pkg\"/" receipt
   962 		status && echo ""
   964 		# Interactive mode, asking and seding.
   965 		case "$3" in
   966 			--interactive|-x)
   967 				gettext -e "Entering interactive mode...\n"
   968 				separator
   969 				echo "Package       : $pkg"
   970 				# Version.
   971 				echo -n "Version       : " ; read anser
   972 				sed -i s/'VERSION=\"\"'/"VERSION=\"$anser\""/ receipt
   973 				# Category.
   974 				echo -n "Category      : " ; read anser
   975 				sed -i s/'CATEGORY=\"\"'/"CATEGORY=\"$anser\""/ receipt
   976 				# Short description.
   977 				echo -n "Short desc    : " ; read anser
   978 				sed -i s/'SHORT_DESC=\"\"'/"SHORT_DESC=\"$anser\""/ receipt
   979 				# Maintainer.
   980 				echo -n "Maintainer    : " ; read anser
   981 				sed -i s/'MAINTAINER=\"\"'/"MAINTAINER=\"$anser\""/ receipt
   982 				# Web site.
   983 				echo -n "Web site      : " ; read anser
   984 				sed -i s#'WEB_SITE=\"\"'#"WEB_SITE=\"$anser\""# receipt
   985 				echo ""
   986 				# Wget URL.
   987 				echo "Wget URL to download source tarball."
   988 				echo "Example  : \$GNU_MIRROR/\$PACKAGE/\$TARBALL"
   989 				echo -n "Wget url : " ; read anser
   990 				sed -i s#'WGET_URL=\"$TARBALL\"'#"WGET_URL=\"$anser\""# receipt
   991 				# Ask for a stuff dir.
   992 				echo -n "Do you need a stuff directory ? (y/N) : " ; read anser
   993 				if [ "$anser" = "y" ]; then
   994 					echo -n "Creating the stuff directory..."
   995 					mkdir $WOK/$pkg/stuff && status
   996 				fi
   997 				# Ask for a description file.
   998 				echo -n "Are you going to write a description ? (y/N) : " ; read anser
   999 				if [ "$anser" = "y" ]; then
  1000 					echo -n "Creating the description.txt file..."
  1001 					echo "" > $WOK/$pkg/description.txt && status
  1002 				fi
  1003 				separator
  1004 				gettext -e "Receipt is ready to use.\n"
  1005 				echo "" ;;
  1006 		esac ;;
  1007 	list)
  1008 		# Cook a list of packages (better use the Cooker since it will order
  1009 		# packages before executing cook).
  1010 		check_root
  1011 		[ -z "$2" ] && gettext -e "\nNo list in argument.\n\n" && exit 1
  1012 		[ ! -f "$2" ] && gettext -e "\nNo list found:" && \
  1013 			echo -e " $2\n" && exit 1
  1014 		echo "Cook list starting: $2" | log
  1015 		for pkg in $(cat $2)
  1016 		do
  1017 			cook $pkg || broken
  1018 		done ;;
  1019 	clean-wok)
  1020 		check_root
  1021 		gettext -e "\nCleaning all packages files..."
  1022 		rm -rf $WOK/*/taz $WOK/*/install $WOK/*/source
  1023 		status && echo "" ;;
  1024 	clean-src)
  1025 		check_root
  1026 		gettext -e "\nCleaning all packages sources..."
  1027 		rm -rf $WOK/*/source
  1028 		status && echo "" ;;
  1029 	pkgdb)
  1030 		# Create suitable packages list for TazPKG and only for built packages
  1031 		# as well as flavors files for TazLiTo. We dont need logs since we do it
  1032 		# manually to ensure everything is fine before syncing the mirror.
  1033 		case "$2" in
  1034 			--flavors)
  1035 				continue ;;
  1036 			*)
  1037 				[ "$2" ] && PKGS="$2"
  1038 				[ ! -d "$PKGS" ] && \
  1039 					gettext -e "\nPackages directory doesn't exist\n\n" && exit 1 ;;
  1040 		esac
  1041 		time=$(date +%s)
  1042 		flavors=$SLITAZ/flavors
  1043 		live=$SLITAZ/live
  1044 		echo "cook:pkgdb" > $command
  1045 		echo "Cook pkgdb: Creating all packages lists" | log
  1046 		echo ""
  1047 		gettext "Creating lists for: "; echo "$PKGS"
  1048 		separator
  1049 		gettext "Cook pkgdb started: "; date "+%Y-%m-%d %H:%M"
  1050 		cd $PKGS
  1051 		rm -f packages.*
  1052 		gettext -e "Creating: packages.list\n"
  1053 		ls -1 *.tazpkg | sed s'/.tazpkg//' > $PKGS/packages.list
  1054 		gettext -e "Creating: packages.md5\n"
  1055 		md5sum *.tazpkg > $PKGS/packages.md5
  1056 		md5sum packages.md5 | cut -f1 -d' ' > ID
  1057 		gettext -e "Creating lists from: "; echo "$WOK"
  1058 		cd $WOK
  1059 		for pkg in *
  1060 		do
  1061 			unset_receipt
  1062 			. $pkg/receipt
  1063 			if [ -f "$PKGS/$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg" ]; then
  1064 				# PACKED_SIZE and UNPACKED_SIZE are only in built receipt
  1065 				if [ -s $pkg/taz/*/receipt ]; then
  1066 					. $pkg/taz/*/receipt
  1067 				fi
  1068 				# packages.desc lets us search easily in DB
  1069 				cat >> $PKGS/packages.desc << EOT
  1070 $PACKAGE | ${VERSION}$EXTRAVERSION | $SHORT_DESC | $CATEGORY | $WEB_SITE
  1071 EOT
  1072 				# packages.txt used by tazpkg and tazpkg-web also to provide
  1073 				# a human readable package list with version and description.
  1074 				cat >> $PKGS/packages.txt << EOT
  1075 $PACKAGE
  1076 ${VERSION}$EXTRAVERSION
  1077 $SHORT_DESC
  1078 $PACKED_SIZE ($UNPACKED_SIZE installed)
  1080 EOT
  1081 				# packages.equiv is used by tazpkg install to check depends.
  1082 				for i in $PROVIDE; do
  1083 					DEST=""
  1084 					echo $i | fgrep -q : && DEST="${i#*:}:"
  1085 					if grep -qs ^${i%:*}= $PKGS/packages.equiv; then
  1086 						sed -i "s/^${i%:*}=/${i%:*}=$DEST$PACKAGE /" \
  1087 							$PKGS/packages.equiv
  1088 					else
  1089 						echo "${i%:*}=$DEST$PACKAGE" >> $PKGS/packages.equiv
  1090 					fi
  1091 				done
  1092 				# files.list provides a list of all packages files.
  1093 				cat $pkg/taz/*/files.list | sed s/^/"$pkg: \0"/ >> \
  1094 					$PKGS/files.list
  1095 			fi
  1096 		done
  1098 		# Display list size.
  1099 		gettext -e "Done: packages.desc\n"
  1100 		gettext -e "Done: packages.txt\n"
  1101 		gettext -e "Done: packages.equiv\n"
  1103 		# files.list.lzma
  1104 		gettext -e "Creating: files.list.lzma\n"
  1105 		cd $PKGS && lzma e files.list files.list.lzma
  1106 		rm -f files.list
  1108 		# Display some info.
  1109 		separator
  1110 		nb=$(ls $PKGS/*.tazpkg | wc -l)
  1111 		time=$(($(date +%s) - $time))
  1112 		echo -e "Packages: $nb - Time: ${time}s\n"
  1114 		# Create all flavors files at once. Do we really need code to monitor
  1115 		# flavors changes ? Lets just build them with packages lists before
  1116 		# syncing the mirror.
  1117 		[ "$2" == "--flavors" ] || exit 1
  1118 		[ ! -d "$flavors" ] && echo -e "Missing flavors: $flavors\n" && exit 1
  1119 		[ -d "$live" ] || mkdir -p $live
  1120 		gettext "Creating flavors files in:"; echo " $live"
  1121 		echo "Cook pkgdb: Creating all flavors" | log
  1122 		separator
  1123 		gettext -e "Recharging lists to use latest packages...\n"
  1124 		tazpkg recharge >/dev/null 2>/dev/null
  1126 		# We need a custom tazlito config to set working dir to /home/slitaz.
  1127 		if [ ! -f "$live/tazlito.conf" ]; then
  1128 			echo "Creating configuration file: tazlito.conf"
  1129 			cp /etc/tazlito/tazlito.conf $live
  1130 			sed -i s@WORK_DIR=.*@WORK_DIR=\"/home/slitaz\"@ \
  1131 				$live/tazlito.conf
  1132 		fi
  1134 		# Update Hg flavors repo and pack.
  1135 		[ -d "$flavors/.hg" ] && cd $flavors && hg pull -u
  1137 		cd $live
  1138 		echo "Starting to generate flavors..."
  1139 		rm -f flavors.list *.flavor
  1140 		for i in $flavors/*
  1141 		do
  1142 			fl=$(basename $i)
  1143 			echo "Packing flavor: $(basename $i)"
  1144 			tazlito pack-flavor $fl >/dev/null || exit 1
  1145 			tazlito show-flavor $fl --brief --noheader 2> \
  1146 				/dev/null >> flavors.list
  1147 		done
  1148 		cp -f $live/*.flavor $live/flavors.list $PKGS
  1149 		separator && gettext "Flavors size: "; du -sh $live | awk '{print $1}'
  1150 		echo "" && rm -f $command ;;
  1151 	*)
  1152 		# Just cook and generate a package.
  1153 		check_root
  1154 		time=$(date +%s)
  1155 		pkg="$1"
  1156 		[ -z "$pkg" ] && usage
  1157 		receipt="$WOK/$pkg/receipt"
  1158 		check_pkg_in_wok && echo ""
  1160 		# Skip blocked, 3 lines also for the Cooker.
  1161 		if grep -q "^$pkg$" $blocked && [ "$2" != "--unblock" ]; then
  1162 			gettext -e "Blocked package:"; echo -e " $pkg\n" && exit 0
  1163 		fi
  1165 		try_aufs_chroot "$@"
  1167 		# Log and source receipt.
  1168 		echo "Cook started for: <a href='cooker.cgi?pkg=$pkg'>$pkg</a>" | log
  1169 		echo "cook:$pkg" > $command
  1171 		# Display and log info if cook process stopped.
  1172 		trap 'gettext -e "\n\nCook stopped: control-C\n\n" | \
  1173 			tee -a $LOGS/$pkg.log' INT
  1175 		unset inst
  1176 		unset_receipt
  1177 		. $receipt
  1179 		# Handle --options
  1180 		case "$2" in
  1181 			--clean|-c)
  1182 				gettext -e "Cleaning:"; echo -n " $pkg"
  1183 				cd $WOK/$pkg && rm -rf install taz source
  1184 				status && echo "" && exit 0 ;;
  1185 			--install|-i)
  1186 				inst='yes' ;;
  1187 			--getsrc|-gs)
  1188 				gettext "Getting source for:"; echo " $pkg"
  1189 				separator && get_source
  1190 				echo -e "Tarball: $SRC/$TARBALL\n" && exit 0 ;;
  1191 			--block|-b)
  1192 				gettext "Blocking:"; echo -n " $pkg"
  1193 				[ $(grep "^$pkg$" $blocked) ] || echo "$pkg" >> $blocked
  1194 				status && echo "" && exit 0 ;;
  1195 			--unblock|-ub)
  1196 				gettext "Unblocking:"; echo -n " $pkg"
  1197 				sed -i "/^${pkg}$/"d $blocked
  1198 				status && echo "" && exit 0 ;;
  1200 		esac
  1202 		# Check if wanted is built now so we have separate log files.
  1203 		for wanted in $WANTED ; do
  1204 			if grep -q "^$wanted$" $blocked; then
  1205 				echo "WANTED package is blocked: $wanted" | tee $LOGS/$pkg.log
  1206 				echo "" && rm -f $command && exit 1
  1207 			fi
  1208 			if grep -q "^$wanted$" $broken; then
  1209 				echo "WANTED package is broken: $wanted" | tee $LOGS/$pkg.log
  1210 				echo "" && rm -f $command && exit 1
  1211 			fi
  1212 			if [ ! -d "$WOK/$wanted/install" ]; then
  1213 				cook "$wanted" || exit 1
  1214 			fi
  1215 		done
  1217 		# Cook and pack or exit on error and log everything.
  1218 		cookit $@ 2>&1 | tee $LOGS/$pkg.log
  1219 		remove_deps | tee -a $LOGS/$pkg.log
  1220 		cookit_quality
  1221 		packit 2>&1 | tee -a $LOGS/$pkg.log
  1222 		clean_log
  1224 		# Exit if any error in packing.
  1225 		if grep -q ^ERROR $LOGS/$pkg.log; then
  1226 			debug_info | tee -a $LOGS/$pkg.log
  1227 			rm -f $command && exit 1
  1228 		fi
  1230 		# Create an XML feed
  1231 		gen_rss
  1233 		# Time and summary
  1234 		time=$(($(date +%s) - $time))
  1235 		summary | tee -a $LOGS/$pkg.log
  1236 		echo ""
  1238 		# Install package if requested
  1239 		if [ "$inst" ]; then
  1240 			if [ -f "$PKGS/$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg" ]; then
  1241 				cd $PKGS && tazpkg install \
  1242 					$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg --forced
  1243 			else
  1244 				gettext -e "Unable to install package, build has failed.\n\n"
  1245 				exit 1
  1246 			fi
  1247 		fi
  1249 		# Install package if part of the chroot to keep env up-to-date.
  1250 		if [ -d "$INSTALLED/$PACKAGE" ] && [ -z "$AUFS_MOUNTS" ]; then
  1251 			echo "Updating chroot environment..."
  1252 			echo "Updating chroot: $PACKAGE (${VERSION}${EXTRAVERSION})" | log
  1253 			cd $PKGS && tazpkg install \
  1254 				$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg --forced
  1255 		fi
  1257 		# Finally we DONT WANT to build the *-dev or packages with WANTED="$pkg"
  1258 		# You want automation: use the Cooker Build Bot.
  1259 		#[ -d "$WOK/$pkg-dev" ] && cook $pkg-dev
  1260 		rm -f $command ;;
  1261 esac
  1263 exit 0