#!/bin/sh
#
# cook-setup - SliTaz cook: bootstrap a build chroot, or set up a
# cross-compile environment for arm / armv6hf / armv7 / x86_64.
#
# Usage:
#   cook-setup                    # native chroot bootstrap
#   cook-setup --wok              # bootstrap + hg clone the cooking wok
#   cook-setup --stable           # bootstrap + clone the stable wok
#   cook-setup --undigest         # bootstrap + clone the undigest wok
#   cook-setup --tiny             # bootstrap + clone the tiny wok
#   cook-setup --forced           # re-install SETUP_PKGS even if present
#   cook-setup <arch>             # cross-compile setup (arm|armv6hf|armv7|x86_64)
#   cook-setup <arch> --forced    # cross + force re-install of CROSS_SETUP
#
# Copyright (C) SliTaz GNU/Linux - GNU GPL v3
#

. /usr/lib/slitaz/libcook.sh


usage() {
	cat <<EOT

$(boldify "$(_ 'Usage:')") cook-setup [arch] [options]

$(boldify "$(_ 'Commands:')")
  usage|help|--help|-h $(_ 'Display this short usage.')
  (no arg)             $(_ 'Bootstrap the native build chroot.')
  arm|armv6hf|armv7|x86_64
                       $(_ 'Set up a cross-compile environment.')

$(boldify "$(_ 'Options:')")
  --forced             $(_ 'Force re-install of setup packages.')
  --wok                $(_ 'Clone the cooking wok from Hg.')
  --stable             $(_ 'Clone the stable wok.')
  --undigest           $(_ 'Clone the undigest wok.')
  --tiny               $(_ 'Clone the tiny wok.')

$(boldify "$(_ 'Examples:')")
  cook-setup                   $(_ 'fresh chroot, no wok cloned')
  cook-setup --wok             $(_ 'chroot + clone the cooking wok')
  cook-setup --forced --wok    $(_ 'force reinstall + clone')
  cook-setup arm               $(_ 'cross-compile setup for ARM')
  cook-setup x86_64            $(_ 'cross-compile setup for x86_64')

EOT
	exit 0
}


# Create $SLITAZ subdirs and DB files that cook expects to find.

init_db_files() {
	_ 'Creating directories structure in "%s"' "$SLITAZ"
	mkdir -p $WOK $PKGS $SRC $CACHE $LOGS $FEEDS
	_ 'Creating DB files in "%s"' "$CACHE"
	touch $activity $command $broken $blocked $CACHE/webstat
	chown www:www $cache/webstat
}


#
# Parse arguments
#

arch=''
forced=''
wok_opt=''

for arg in "$@"; do
	case "$arg" in
		usage|help|--help|-h) usage ;;
		--forced)             forced='yes' ;;
		--wok|--stable|--undigest|--tiny) wok_opt="$arg" ;;
		arm|armv6hf|armv7|x86_64)         arch="$arg" ;;
		*) _ 'Unknown argument: %s' "$arg" >&2; usage ;;
	esac
done


#
# Cross-compile setup
#

if [ -n "$arch" ]; then
	check_root
	. /etc/slitaz/cook.conf
	for pkg in $CROSS_SETUP; do
		if [ -n "$forced" ]; then
			tazpkg -gi $pkg --forced
		else
			[ ! -d "$INSTALLED/$pkg" ] && tazpkg -gi $pkg
		fi
	done

	_ 'Cook: setup %s cross environment' "$arch" | log
	title 'Setting up your %s cross environment' "$arch"
	init_db_files
	sed -i \
		-e "s|ARCH=.*|ARCH=\"$arch\"|" \
		-e "s|CROSS_TREE=.*|CROSS_TREE=\"/cross/$arch\"|" \
		-e 's|BUILD_SYSTEM=.*|BUILD_SYSTEM=i486-slitaz-linux|' \
		/etc/slitaz/cook.conf
	case "$arch" in
		arm)
			flags='-O2 -march=armv6'
			host="$ARCH-slitaz-linux-gnueabi" ;;
		armv6hf)
			flags='-O2 -march=armv6j -mfpu=vfp -mfloat-abi=hard'
			host="$ARCH-slitaz-linux-gnueabi" ;;
		armv7)
			flags='-Os -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp -pipe'
			host="$ARCH-slitaz-linux-gnueabi" ;;
		x86_64)
			flags='-O2 -mtune=generic -pipe'
			host="$ARCH-slitaz-linux" ;;
	esac
	sed -i \
		-e "s|CFLAGS=.*|CFLAGS=\"$flags\"|" \
		-e "s|HOST_SYSTEM=.*|HOST_SYSTEM=$host|" /etc/slitaz/cook.conf
	. /etc/slitaz/cook.conf
	sysroot="$CROSS_TREE/sysroot"
	tools="/cross/$arch/tools"
	root="$sysroot"
	# L10n: keep the same width of translations to get a consistent view
	_ 'Target arch     : %s' "$ARCH"
	_ 'Configure args  : %s' "$CONFIGURE_ARGS"
	_ 'Build flags     : %s' "$flags"
	_ 'Arch sysroot    : %s' "$sysroot"
	_ 'Tools prefix    : %s' "$tools/bin"
	# Tell the packages manager where to find packages.
	_ 'Packages DB     : %s' "$root$DB"
	mkdir -p $root$INSTALLED
	cd $root$DB; rm -f *.bak
	for list in packages.list packages.desc packages.equiv packages.md5; do
		rm -f $list
		ln -s $SLITAZ/packages/$list $list
	done
	# We must have the cross compiled glibc-base installed or default
	# i486 package will be used as dep by tazpkg and then break the
	# cross environment
	if [ ! -f "$root$INSTALLED/glibc-base/receipt" ]; then
		colorize 36 $(_ 'WARNING: %s is not installed in sysroot' '(e)glibc-base')
	fi
	# Show GCC version or warn if not yet compiled.
	if [ -x "$tools/bin/$HOST_SYSTEM-gcc" ]; then
		_ 'Cross compiler  : %s' "$HOST_SYSTEM-gcc"
	else
		colorize 36 $(_ 'C compiler "%s" is missing' "$HOST_SYSTEM-gcc")
		_ 'Run "%s" to cook a toolchain' 'cross compile'
	fi
	footer
	exit 0
fi


#
# Native chroot bootstrap
#

check_root
_ 'Cook: setup environment' | log
title 'Setting up your environment'
[ -d $SLITAZ ] || mkdir -p $SLITAZ
cd $SLITAZ
init_db_files
_ 'Checking for packages to install...'
# Use setup pkgs from cross.conf (if present) to override SETUP_PKGS
# for cross-compile chroots. Skipped silently on a vanilla chroot.
case "$ARCH" in
	arm*|x86_64)
		if [ -f /etc/slitaz/cross.conf ]; then
			_ 'Using config file: %s' '/etc/slitaz/cross.conf'
			. /etc/slitaz/cross.conf
		fi
		;;
esac
for pkg in $SETUP_PKGS; do
	if [ -n "$forced" ]; then
		tazpkg -gi $pkg --forced
	else
		[ ! -d "$INSTALLED/$pkg" ] && tazpkg get-install $pkg
	fi
done

# Handle --wok/--stable/--undigest/--tiny
case "$wok_opt" in
	--wok)      hg clone $WOK_URL          wok || exit 1 ;;
	--stable)   hg clone $WOK_URL-stable   wok || exit 1 ;;
	--undigest) hg clone $WOK_URL-undigest wok || exit 1 ;;
	--tiny)     hg clone $WOK_URL-tiny     wok || exit 1 ;;
esac

# SliTaz group and permissions
if ! grep -q ^slitaz /etc/group; then
	_ 'Adding group "%s"' 'slitaz'
	addgroup slitaz
fi
_ 'Setting permissions for group "%s"...' 'slitaz'
find $SLITAZ -maxdepth 2 -exec chown root:slitaz {} \;
find $SLITAZ -maxdepth 2 -exec chmod g+w {} \;
footer "$(_ 'All done, ready to cook packages :-)')"
