#!/bin/sh
#
# Tazdev - SliTaz developers and build host tool. System wide config
# file is located at: /etc/slitaz/tazdev.conf. Keep the code clear and
# well commented please, also: configuration variables are $UPPERCASE
# and variables initialized by tazdev itself are $lowerspace
#
# (c) 2011 SliTaz GNU/Linux - GNU gpl v3
#
# AUTHORS
#     Christophe Lincoln <pankso@slitaz.org>
#     Pascal Bellard <bellard@slitaz.org>
#

VERSION=1.9

. /lib/libtaz.sh

[ -f /etc/slitaz/tazdev.conf ] && . /etc/slitaz/tazdev.conf
[ -f tazdev.conf ] && . ./tazdev.conf

if [ ! "$SLITAZ_HOME" ]; then
	echo -e "\nNo config file found\n" && exit 1
fi

usage() {
	cat << EOT

$(boldify "Usage:") $(basename $0) [command] [vers|user|tool] [vers|--opts]

SliTaz developers and build host tool v$VERSION

$(boldify "Commands:")
  usage               Print this short usage and command list
  help                Give help on a SliTaz tool or library.
  stats|-s           Display statistics about your projects
  chroot|-c           Mount virtual fs and chroot into the build env
  gen-chroot|-gc      Generate a chroot using packages from config file
  umount-chroot|-uc   Unmount chroot specified on cmdline
  clean-chroot|-cc    Clean a chroot environment (skip root/ and home/)
  !purge              Remove obsolete packages and obsolete source tarballs
  !dry-purge          Show obsolete packages and obsolete source tarballs
  push|-p             Upload new packages to the main mirror
  dry-push|-dp        Show what will be uploaded to the mirror. Does nothing
  pull                Download new packages from the main mirror
  dry-pull            Show what will be downloaded from the mirror. Does nothing
  relpkg|-rp          Archive and upload new package/project version

$(boldify "Options:")
  --rootfs=           Path to the chroot to generate or clean
  --arch=             Specify the architecture type for cross-chroot, push/pull
  --clean             Clean chroot before generation a new one

$(boldify "Options:")
  $(basename $0) gen-chroot undigest --clean
  $(basename $0) chroot stable
  $(basename $0) -c --arch=arm

EOT
}

get_version() {
	# Stable, undigest or custom.
	if [ "$2" ] && [ ! $(echo $2 | grep "\--*") ]; then
		version=$2
		slitaz=$SLITAZ_HOME/$2
	else
		version=cooking
		slitaz=$SLITAZ_HOME/cooking
	fi
	if [ "$arch" ]; then
		rootfs=$slitaz/$arch/chroot
	else
		rootfs=$slitaz/chroot
	fi
}

check_mirror() {
	# ping -c 1 $MIRROR
	if [ -n "$2" ]; then
		user=$2
	else
		user=$user
	fi
	if [ "$2" = "stable" ] || [ "$3" = "stable" ]; then
		remote=$MIRROR_PKGS/stable/
		local=$SLITAZ_HOME/stable/packages/
	elif [ "$2" = "undigest" ] || [ "$3" = "undigest" ]; then
		remote=$MIRROR_PKGS/undigest/
		local=$SLITAZ_HOME/undigest/packages/
	else
		remote=$MIRROR_PKGS/cooking/
		local=$SLITAZ_HOME/cooking/packages/
	fi
	if [ "$arch" ]; then
		remote=${remote}$arch/
		local=${local}$arch/
	fi
}

# Bind a directory into the chroot
bind_chroot_dir()
{
	mkdir -p $1 $2
	mount -o bind $1 $2
}

# Mount virtual Kernel file systems and chroot but check that nobody
# else has done mounts
mount_chroot()
{
	if [ ! -d $rootfs/proc/1 ]; then
		echo -ne "\nMounting virtual filesystems..."
		mount -t proc proc $rootfs/proc
		mount -t sysfs sysfs $rootfs/sys
		mount -t devpts devpts $rootfs/dev/pts
		mount -t tmpfs shm $rootfs/dev/shm
		status
	fi
	# Mount source so they can be shared between cooking/stable/undigest.
	# But do it only if it's a slitaz developement chroot.
	fs=$rootfs/home/slitaz
	if [ -d "$slitaz" ]; then
		bind_chroot_dir $SLITAZ_HOME/src $fs/src
		# Now mount package dir so they are in /home/slitaz/$version
		# We may not mount cache wok or others it has no point and if
		# one wants to use a shared wok he can bind it manually.
		if [ "$arch" ]; then
			slitaz=$slitaz/$arch
		fi
		[ -d "$fs/packages" ] || mkdir -p $fs/packages
		[ -d "$slitaz/packages" ] || mkdir -p $fs/packages
		bind_chroot_dir $slitaz/packages $fs/packages
	fi
}

# Unmount virtual Kernel file systems.
umount_chroot() {
	[ "$1" ] && rootfs=$1
	fs=$rootfs/home/slitaz
	echo -ne "\nUnmounting virtual filesystems..."
	umount $rootfs/dev/shm
	umount $rootfs/dev/pts
	umount $rootfs/sys
	umount $rootfs/proc
	if mount | fgrep -q $fs/src; then
		umount $fs/src
		umount $fs/packages
	fi
	status && echo ""
}

# Get the last cooking base rootfs, extract and configure.
gen_chroot() {
	echo -e "\nGenerating new chroot in: $rootfs"
	separator
	mkdir -p $rootfs
	# We my gen cooking chroot from a stable version or invers
	case "$version" in
		cooking|undigest) url="http://$MIRROR/packages/cooking/" ;;
		stable|4.0) url="http://$MIRROR/packages/stable/" ;;
	esac
	# --mirror=
	[ "$mirror" ] && url="$mirror"
	mpath=/var/lib/tazpkg/mirror
	cp $mpath ${mpath}.tazdev
	echo "$url" > $mpath
	echo -n "Mirror URL: ${url#http://}"
	tazpkg recharge 2>/dev/null 1>/dev/null
	status
	for pkg in $CHROOT_PKGS
	do
		echo -n "Installing: $pkg $vers"
		tazpkg -gi $pkg --root=$rootfs 2>/dev/null 1>/dev/null
		status
	done
	echo -n "Creating resolv.conf..."
	cat /etc/resolv.conf > $rootfs/etc/resolv.conf
	status
	echo -n "Restoring host packages list..."
	mv -f ${mpath}.tazdev $mpath
	tazpkg recharge 2>/dev/null 1>/dev/null >/dev/null
	status
	separator
	case "$version" in
		cooking) version="" ;;
	esac
	[ "$arch" ] && version="$version --arch=$arch"
	echo -n "Ready to chroot with:"; colorize 34 " tazdev -c $version"
	newline
}

# Remove obsolate slitaz packages --> in cooker ???
purge_packages() {
	arg=$1
	tmp=/tmp/tazdev.$$
	ls $wok| while read pkg; do
		[ -f $wok/$pkg/taz/*/receipt ] || continue
		unset VERSION EXTRAVERSION
		. $wok/$pkg/taz/*/receipt
		echo $pkg-${VERSION}$EXTRAVERSION.tazpkg
	done > $tmp
	ls $slitaz/chroot/home/slitaz/packages | while read pkg; do
		case "$pkg" in
			*.tazpkg)
				grep -q ^$pkg$ $tmp && continue
				echo "Removing pkg: $pkg"
				[ "$arg" == "purge" ] &&
				rm -f $slitaz/chroot/home/slitaz/packages/$pkg ;;
		esac
	done
	rm -f $tmp
}

# Remove obsolete source tarballs --> in cooker ??? but we share $src
# between cooking and stable to we must scan all versions, all arch
purge_sources() {
	arg=$1
	wok=$slitaz/chroot/home/slitaz/wok
	tmp=/tmp/tazdev.$$
	ls $wok| while read pkg; do
		[ -f $wok/$pkg/receipt ] || continue
		TARBALL=""
		. $wok/$pkg/receipt
		[ -n "$TARBALL" ] && echo $TARBALL
		grep SOURCES_REPOSITORY/ $wok/$pkg/receipt | sed \
		-e 's|.*SOURCES_REPOSITORY/\([^ ]*\)\( .*\)$|\1|' \
		-e 's|.*SOURCES_REPOSITORY/\([^ ]*\)$|\1|' | sort | uniq | \
		sed "s|['\"/]||g" | while read file ; do
			eval echo $file 2> /dev/null
		done
	done > $tmp
	ls $slitaz/chroot/home/slitaz/src | while read pkg; do
		grep -q ^$pkg$ $tmp && continue
		echo "Removing src: $pkg"
		[ "$arg" == "purge" ] &&
			rm -f $slitaz/chroot/home/slitaz/src/$pkg
	done
	rm -f $tmp
}

case "$1" in
	stats|-s)
		echo -e "\nStatistics for: $PROJECTS\n"
		echo -n "Project" && echo -ne "\033[24G Size" && echo -ne "\033[38G Revision"
		echo -ne "\033[48G Version" && echo -e "\033[64G Files"
		separator
		cd $PROJECTS
		for proj in *
		do
			rev=""
			echo -n "$proj"
			size=`du -sh $proj | awk '{ print $1 }'`
			echo -ne "\033[24G $size"
			if [ -d $proj/.hg ]; then
				cd $proj
				rev=`hg head --template '{rev}\n'`
				vers=`hg tags | head -n 2 | tail -n 1 | cut -d " " -f 1`
				echo -ne "\033[38G $rev"
				echo -ne "\033[48G $vers" && cd ..
			fi
			files=`find $proj -type f | wc -l`
			echo -e "\033[64G $files"
		done
		separator
		echo "" ;;
	chroot|-c)
		# Chroot into a build env. Default to cooking configured in tazdev.conf
		check_root
		get_version $@
		[ -d "$2" ] && rootfs=$2
		mount_chroot
		echo -e "Chrooting to: $rootfs\n"
		chroot $rootfs /bin/sh --login
		umount_chroot ;;
	umount-chroot|-uc)
		check_root
		get_version $@
		[ -d "$2" ] && rootfs=$2
		umount_chroot $rootfs ;;
	gen-chroot|-gc)
		check_root
		# We can use: --rootfs=/path/to/chroot
		if [ ! "$rootfs" ]; then
			get_version $@
		fi
		if [ "$clean" ] || [ "$forced" ]; then
			$0 -cc --rootfs=$rootfs --noline
		fi
		# Dont break another env.
		if [ -d $rootfs/bin ]; then
			echo -e "\nA chroot environment already exists in : $rootfs\n"
			exit 1
		fi
		gen_chroot ;;
	clean-chroot|-cc)
		check_root
		# We can use: --rootfs=/path/to/chroot
		if [ ! "$rootfs" ]; then
			get_version $@
		fi
		if [ ! -d "$rootfs" ]; then
			echo -e "\nChroot doesn't exist: $rootfs\n"
			exit 1
		fi
		if [ -d $rootfs/proc/1 ]; then
			echo -e "\nWARNING: $rootfs/proc mounted!\n"
			exit 1
		fi
		cd $rootfs || exit 1
		echo ""
		boldify "Cleaning chroot: $rootfs"
		separator
		# Keep root/ and /home they may have a build wok, custom scripts, etc.
		for i in boot bin dev etc lib media mnt proc sbin sys tmp usr var run
		do
			if [ -d "$i" ]; then
				echo -n "Removing: $i ($(du -sh $i | awk '{ print $1 }'))... "
				rm -rf $i && status
			fi
		done
		rm -f init
		separator
		[ "$noline" ] || newline ;;
	push|-p)
		check_mirror $@
		rsync -r -t -O -l -v -z --delete \
			$local -e ssh $user@$MIRROR:$remote ;;
	dry-push|-dp)
		check_mirror $@
		rsync -r -t -O -l -v -z --delete --dry-run \
			$local -e ssh $user@$MIRROR:$remote ;;
	pull)
		check_mirror $@
		rsync -r -t -l -v -z --delete \
			-e ssh $user@$MIRROR:$remote $local ;;
	dry-pull)
		check_mirror $@
		rsync -r -t -l -v -z --delete --dry-run \
			-e ssh $user@$MIRROR:$remote $local ;;
	purge|dry-purge)
		check_root
		get_version $@
		purge_packages $1
		purge_sources $1 ;;
	relpkg|-rp)
		# Release a slitaz sub-project and upload tarball to mirror
		[ -z "$MIRROR_SOURCES" ] && MIRROR_SOURCES="/var/www/slitaz/mirror/sources"
		if [ -z $2 ]; then
			echo -e "\nUsage: $0 relpkg package [version]\n"
			exit 0
		fi
		pkg=$2
		
		# We can get the last found version in .hgtags
		if [ -z $3 ]; then
			version=$(tail -n 1 $PROJECTS/$pkg/.hgtags | awk '{print $2'})
		else
			version=$3
		fi

		echo "" && cd $PROJECTS/$pkg

		# Sanity check
		if ! grep -q $version$ .hgtags; then
			echo "Missing Hg tag for version: $version"
			echo -e "You may want to: hg tag $version && hg push\n"
			exit 0
		fi

		# Archive
		echo -n "Creating tarball and md5sum for: $pkg-$version... "
		hg archive -t tgz $SOURCE/$pkg-$version.tar.gz
		( cd $SOURCE; md5sum $pkg-$version.tar.gz > $pkg-$version.md5 )
		echo "Done"

		# Upload
		echo -n "Do you wish to upload tarball to the mirror [N/y] ? "
		read upload
		if [ "$upload" = "y" ]; then
			echo "Uploading to: $MIRROR/sources/${pkg#slitaz-}"
			scp $pkg-$version.tar.gz $pkg-$version.md5 \
				$MIRROR:$MIRROR_SOURCES/${pkg#slitaz-}
		fi

		# Update pkg in wok
		echo -n "Do you wish to update $pkg in wok [N/y] ? "
		read update
		if [ "$update" = "y" ]; then
			echo -n "Updating $pkg: $version"
			cd $PROJECTS/wok
			sed -i s"/VERSION=.*/VERSION=\"$version\"/" $pkg*/receipt
			status
		fi ;;
	help)
		[ ! "$2" ] && echo "Missing tool/library name" && exit 0
		echo ""
		boldify "Help for: $2"
		separator
		if [ -f "/usr/share/doc/slitaz/$2.txt" ]; then
			less -M /usr/share/doc/slitaz/$2.txt
		else
			echo "No help found"
		fi
		separator && echo "" ;;
	usage|*)
		usage ;;
esac

exit 0
