#!/bin/bash
# -*- coding: utf-8-unix -*-

Usage_C(){
    cat<<EOF
Usage:	$(basename $0) {--version [version]} {--arch [arch]} {--category [categories]} {--dist-upgrade} {--target [target]} {--with-compat32} {--bootstrap-dir [directory]} {--cache-dir [directory]} {--built-rpms-dir [directory]} {clean|build|build-rpm [src.rpm]|install-rpm [arch.rpm|package]|remove-rpm [package]|show-info}

Options:
	--version:		set [version] (default: ${DEFAULT_VERSION})
	--arch:			set [arch] (default: $(uname -i))
	--category:		set [categories] (default: ${CATEGORIES})
	--dist-upgrade:		make VineSeed bootstrap via ${STABLE_VERSION}
	--unionfs:		cover a bootstrap with unionfs
	--target:		build rpms with [target]
	--with-compat32:	build rpms with compat32 on boostrap
	--bootstrap-dir:	set a bootstrap directory (default: ${VBOOTSTRAP_DIR})
	--cache-dir:		set a directory to cache rpms (default: ${CACHE_DIR})
	--built-rpms-dir:	set a directory to store built rpms in chroot (default: ${BUILT_RPMS_DIR})

Actions:
	clean:			clean the boostrap of [version]
	build:			build a boostrap of [version]
	build-rpm:		build [src.rpm] on a boostrap
	install-rpm:		install [arch.rpm|package] on a boostrap
	remove-rpm:		remove [package] on a boostrap
	show-info:		show basic informations and logs in chroot

For example, 
* make a clean/plain build environment on the current archtecture:
$(basename $0) clean build
* build rpms from the specified source rpm:
$(basename $0) build-rpm [src.rpm]
* make a plain build environment for Vine Linux 4.2:
$(basename $0) --version 4.2 clean build
* make a i386 chroot on x86_64:
$(basename $0) --arch i386 clean build
* build a kernel package with target i686:
$(basename $0) --target i686 build-rpm [kernel src.rpm]
* build a compat32 package:
$(basename $0) --arch i386 --with-compat32 build-rpm [src.rpm]
$(/usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*//)
EOF
}

Usage_ja(){
    Usage_C
}

Msg_MissingParameter_C(){
    local para=$1
    cat<<EOF
E: Missing some parameters after ${para}
EOF
}

Msg_MissingParameter_ja(){
    local para=$1
    cat<<EOF
E: ${para} 以後のいくつかの引数に間違いがあります
EOF
}

Msg_NoSuchCategoryExists_C(){
    local para=$1
    cat<<EOF
E: No such category exists: $para
EOF
}

Msg_NoSuchCategoryExists_ja(){
    local para=$1
    cat<<EOF
E: そのようなカテゴリは存在しません: $para
EOF
}

Msg_GiveMoreOptions_C(){
    cat<<EOF
E: You can give no more options after actions
EOF
}

Msg_GiveNoMoreOptions_ja(){
    cat<<EOF
E: 動作の以後にオプションを与えられません
EOF
}

Msg_GiveAtLeastOneAction_C(){
    cat<<EOF
E: You must give at least one action
EOF
}

Msg_GiveAtLeastOneAction_ja(){
    cat<<EOF
E: 少なくとも1つの動作を与えなければなりません
EOF
}

Msg_NoSupportVARCH_C(){
    cat<<EOF
E: arch ${VARCH} is NOT supported on $(uname -i)
EOF
}

Msg_NoSupportVARCH_ja(){
    cat<<EOF
E: ${VARCH} アーキテクチャは $(uname -i) 上で非サポートです
EOF
}

Msg_NoSupportVERSION_C(){
    cat<<EOF
E: ${VERSION} is NOT supported
EOF
}

Msg_NoSupportVERSION_ja(){
    cat<<EOF
E: バージョン ${VERSION} は非サポートです
EOF
}

Msg_NoSupportDistUpgradeVERSION_C(){
    cat<<EOF
E: version ${VERSION} does not support --dist-upgrade option
EOF
}

Msg_NoSupportDistUpgradeVERSION_ja(){
    cat<<EOF
E: バージョン ${VERSION} では --dist-upgrade オプションはサポートされていません
EOF
}

Msg_NoSupportTARGET_C(){
    cat<<EOF
E: rpm build target ${TARGET} is NOT supported
EOF
}

Msg_NoSupportTARGET_ja(){
    cat<<EOF
E: rpm ビルドターゲット ${TARGET} はサポートされていません
EOF
}

Msg_NotPackageName_C(){
    local RPM_PKG=$1
    cat<<EOF
E: $RPM_PKG is not a package name
EOF
}

Msg_NotPackageName_ja(){
    local RPM_PKG=$1
    cat<<EOF
E: $RPM_PKG はパッケージ名でありません
EOF
}

Msg_NotSourceRPM_C(){
    local RPM_PKG=$1
    cat<<EOF
E: $RPM_PKG is not a source RPM package
EOF
}

Msg_NotSourceRPM_ja(){
    local RPM_PKG=$1
    cat<<EOF
E: $RPM_PKG はソース RPM パッケージでありません
EOF
}

##############################################################################

check-parameter(){
    [ -z "$*" ] && Usage_$LOCALE && return 1

    while [ ! -z "$*" ]; do
	case $1 in
	    --help|help)
		Usage_$LOCALE
		return 1
		;;
	    --version|--arch|--category|--target|--bootstrap-dir|--cache-dir|--built-rpms-dir)
		[ $with_actions -eq 1 ] && \
		    Msg_GiveNoMoreOptions_$LOCALE && return 1
		shift
		check-next-parameter $1 || return 1
		;;
	    --dist-upgrade|--unionfs|--with-compat32)
		[ $with_actions -eq 1 ] && \
		    Msg_GiveNoMoreOptions_$LOCALE && return 1
		;;
	    --build-rpm|build-rpm|--install-rpm|install-rpm|--remove-rpm|remove-rpm)
		with_actions=1
		shift
		check-next-parameter $1 || return 1
		;;
	    --build|build|--clean|clean|--show-info|show-info)
		with_actions=1
		;;
	    *)
		Msg_MissingParameter_$LOCALE $1
		return 1
		;;
	esac
	shift
    done

    [ $with_actions -eq 0 ] && Msg_GiveAtLeastOneAction_$LOCALE && return 1

    return 0
}

check-next-parameter(){
    local arg=$1
    [ -z "${arg}" ] && Msg_MissingParameter_$LOCALE ${arg} && return 1

    [ $(echo ${arg} | grep '^-') ] && \
	Msg_MissingParameter_$LOCALE ${arg} && return 1

    return 0
}

setup-vbuilder(){
    ## load default settings
    VBUILDER_CONF=/etc/vbootstrap/vbuilder.conf
    if [ -r $VBUILDER_CONF ]; then
	. $VBUILDER_CONF
    fi
    [ -z "${DEFAULT_VERSION}" ] && \
	DEFAULT_VERSION=@@VBUILDER_DEFAULT_VERSION@@
    [ -z "${CATEGORIES}" ] && \
	CATEGORIES=@@VBUILDER_CATEGORIES@@
    [ -z "${VBOOTSTRAP_DIR}" ] && \
	VBOOTSTRAP_DIR=@@VBUILDER_VBOOTSTRAP_DIR@@
    [ -z "${CACHE_DIR}" ] && \
	CACHE_DIR=@@VBUILDER_CACHE_DIR@@
    [ -z "${BUILT_RPMS_DIR}" ] && \
	BUILT_RPMS_DIR=@@VBUILDER_BUILT_RPMS_DIR@@

    ## set default version for vbootstrap
    VERSION=$DEFAULT_VERSION

    ## set current stable relase version
    STABLE_VERSION=@@VBUILDER_STABLE_VERSION@@

    ## set locale
    case $LANG in
	ja*)  LOCALE=ja ;;
	*)    LOCALE=C ;;
    esac

    ## set boolian
    with_setup_vbootstrap=0
    with_unionfs=0
    with_dist_upgrade=0
    with_actions=0
    with_category_main=0
    with_category_plus=0
    with_category_nonfree=0
    with_category_test=0
    with_category_proposed_updates=0
    with_category_security=0
}

setup-vbootstrap(){
    if [ ${with_setup_vbootstrap} -eq 0 ]; then
	with_setup_vbootstrap=1

	## check some directories
	## Note: create $BUILT_RPMS_DIR in RPM_Build()
	[ -d $VBOOTSTRAP_DIR ] || mkdir -p $VBOOTSTRAP_DIR
	[ -d $CACHE_DIR ] || mkdir -p $CACHE_DIR

	## check a chroot archtecture
	if [ -z ${VARCH} ]; then
	    VARCH=$(uname -i)
	else
	    case "${VARCH}" in
		i386|i686|x86_64)
		    [ "$(uname -i)" = "ppc" ] && \
			Msg_NoSupportVARCH_$LOCALE && exit 1
		    ;;
		ppc)
		    [ "$(uname -i)" = "i386" -o "$(uname -i)" = "i686" -o "$(uname -i)" = "x86_64" ] && \
			Msg_NoSupportVARCH_$LOCALE && exit 1
		    ;;
	    esac
	fi

        ##!! 4.2 is NO support on VARCH=x86_64
	if [ "${VERSION}" = "4.2" -a "${VARCH}" = "x86_64" ]; then
	    Msg_NoSupportVERSION_${LOCALE} && exit 1
	fi

	## support i386 chroot on x86_64 below: 
	[ "${VARCH}" != "$(uname -i)" ] && VERSION=${VERSION}_${VARCH}

        ## check support ${VERSION}
	if [ -z "$(/usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*// | grep -m 1 ${VERSION})" ]; then
	    Msg_NoSupportVERSION_$LOCALE && exit 1
	fi

	## check ${VERSION} equals VineSeed*, when with_dist_upgrade=1
	if [ $with_dist_upgrade -eq 1 ]; then
	    if [ "$(echo ${VERSION} | sed -e "s/\(VineSeed\).*/\1/")" != "VineSeed" ]; then 
		Msg_NoSupportDistUpgradeVERSION_$LOCALE && exit 1
	    fi
	fi

	## set ${MAJOR_VERSION}
	MAJOR_VERSION=$(echo ${VERSION} | sed -e "s/_i[0-9]86//")

	## check apt categories
	## "main" category is unconditionally permited
	with_category_main=1
	for cat in $(echo ${CATEGORIES} | sed -e "s/,/ /"g); do
	    case $cat in
		main)
		    with_category_main=1
		    ;;
		plus)
		    with_category_plus=1
		    ;;
		nonfree)
		    with_category_nonfree=1
		    ;;
		test)
                    ## "test" category only exists in VineSeed
		    [ "${MAJOR_VERSION}" = "VineSeed" ] || \
			Msg_NoSuchCategoryExists_$LOCALE ${cat} && exit 1
		    with_category_test=1
		    ;;
		proposed-updates)
                    ##!! "proposed-updates" category does not exist in 4.2
		    [ "${MAJOR_VERSION}" = "4.2" ] && \
			Msg_NoSuchCategoryExists_$LOCALE ${cat} && exit 1

		    with_category_proposed_updates=1
		    ;;
		security)
                    ## "security" category does not exist in VineSeed
		    [ "${MAJOR_VERSION}" = "VineSeed" ] && \
			Msg_NoSuchCategoryExists_$LOCALE ${cat} && exit 1
		    with_category_security=1
		    ;;
		*)
		    Msg_NoSuchCategoryExists_$LOCALE ${cat} && exit 1
		    ;;
	    esac
	done

	## check build target option ${TARGET}
	if [ ! -z "${TARGET}" ]; then
	    RPM_TARGET_LIST="$(cat /usr/lib/rpm/rpmrc | grep arch_canon: | sed -e "s/arch_canon:[[:blank:]]*\(.*\):.*/\1/")"
	    if [ -z "$(echo $RPM_TARGET_LIST | grep $TARGET)" ]; then
		Msg_NoSupportTARGET_$LOCALE && exit 1
	    fi
	fi

    fi

    BUILD_ROOT=${VBOOTSTRAP_DIR}/${VERSION}
    BUILD_USER=vbuilder
    BUILD_DIR=/home/${BUILD_USER}/rpm
    UNIONFS_DIR=${VBOOTSTRAP_DIR}/unionfs/${VERSION}
    ARCHIVES_DIR=${BUILD_ROOT}/var/cache/apt/archives
    EXTERNAL_ARCHIVES_DIR=${CACHE_DIR}/${VERSION}/apt/archives
    VBUILDER_LOG=${BUILD_ROOT}/var/log/vbuilder.log

    __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c"

    mkdir -p $VBOOTSTRAP_DIR
}

setup-vbootstrap-rpm(){
    setup-vbootstrap

    ## check ${BUILD_ROOT}
    [ -d ${BUILD_ROOT} ] || Build

    DIST_RELEASE=$(cat ${BUILD_ROOT}/etc/vine-release | cut -f3 -d" " | cut -f1 -d.)

    if [ -f $RPM_PKG ]; then
	BASE_RPM_PKG=$(basename $RPM_PKG)
	cp -f $RPM_PKG $BUILD_ROOT${BUILD_DIR}
    else
	BASE_RPM_PKG=$RPM_PKG	
    fi
}

## recover apt-get data on host/chroot
apt-get-update(){
    case $1 in
	--host)
	    echo -n "apt-get update on host ... "
	    apt-get -qq update > /dev/null 2>&1
	    echo "done."
	    ;;
	--chroot)
	    echo -n "apt-get update on chroot ... "
	    $__chroot_sh 'apt-get -qq update' > /dev/null 2>&1
	    echo "done."
	    ;;
	*)
	    echo apt-get-update: unknown option $1
	    exit 1
	    ;;
    esac
}

## mount-chroot {|--umount} [file system|name]
## support file systems: /home /tmp /sys /proc /dev/shm /dev/pts /dev
## support name: vfs archives_dir
## NOTE: /tmp needs for applications which use X
##       vfs is virtual file systems
##       archives_dir uses to mount ${EXTERNAL_ARCHIVES_DIR} to ${ARCHIVES_DIR}
##       unionfs_dir covers ${BUILD_ROOT} with unionfs
mount-chroot(){
    if [ "$1" = "--umount" ]; then
	mount-chroot-umount $2 || return 1
    else
	mount-chroot-mount $1 || return 1
    fi
    return 0
}

## mount-chroot-umount [file system|name]
mount-chroot-umount(){
    local fs=$1
    case $fs in
	/home|/tmp|/sys|/proc|/dev/shm|/dev/pts|/dev)
	    [ -d ${BUILD_ROOT}${fs} ] || return 1
	    [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] || \
    		umount ${BUILD_ROOT}${fs}
	    return 0
	    ;;
	vfs)
	    # for dir in /sys /proc /dev/shm /dev/pts /dev; do
	    # 	mount-chroot-umount ${dir} || return 1
	    # done
	    [ -d ${BUILD_ROOT}/proc ] || return 1
	    [ -z "$(mount | grep ${BUILD_ROOT}/proc)" ] || \
		umount ${BUILD_ROOT}/proc
	    return 0
	    ;;
	archives_dir)
	    [ -d ${ARCHIVES_DIR} ] || return 1
	    [ -z "$(mount | grep ${ARCHIVES_DIR})" ] || \
    		umount ${ARCHIVES_DIR}
	    return 0
	    ;;
	unionfs_dir)
	    [ -d ${BUILD_ROOT} ] || return 1
	    [ -z "$(mount | grep ${BUILD_ROOT} | grep unionfs)" ] || \
		umount ${BUILD_ROOT}
	    if [ ! -z "$(mount | grep ${BUILD_ROOT} | grep unionfs)" ]; then
		echo "Retry lazy unmount ... "
		umount -l ${BUILD_ROOT}
		echo "done."
	    fi
	    return 0
	    ;;
	*)
	    echo mount-chroot-umount: unknown file system $fs
	    exit 1
	    ;;
    esac
}

## mount-chroot-mount [file system|name]
mount-chroot-mount(){
    local fs=$1
    local mnt_opts=""

    case $fs in
	/home)
	    mnt_opts="-o rbind"
	    ;;
	*)
	    mnt_opts="--bind"
	    ;;
    esac

    case $fs in
	/home|/tmp|/sys|/proc|/dev/shm|/dev/pts|/dev)
	    [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
	    [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
		mount ${mnt_opts} ${fs} ${BUILD_ROOT}${fs}
	    return 0
	    ;;
	vfs)
	    # for dir in /dev /dev/pts /dev/shm /proc /sys; do
	    # 	mount-chroot-mount ${dir} || return 1
	    # done
	    mount-chroot-mount /proc || return 1
	    return 0
	    ;;
	archives_dir)
	    [ -d ${EXTERNAL_ARCHIVES_DIR} ] || mkdir -p ${EXTERNAL_ARCHIVES_DIR}
	    [ -d ${ARCHIVES_DIR} ] || mkdir -p ${ARCHIVES_DIR}
	    [ -z "$(mount | grep ${ARCHIVES_DIR})" ] && \
		mount ${mnt_opts} ${EXTERNAL_ARCHIVES_DIR} ${ARCHIVES_DIR}
	    [ -d ${ARCHIVES_DIR}/partial ] || mkdir -p ${ARCHIVES_DIR}/partial
	    return 0
	    ;;
	unionfs_dir)
	    if [ $with_unionfs -eq 1 ]; then
		[ -d ${UNIONFS_DIR} ] || mkdir -p ${UNIONFS_DIR}
		[ -z "$(mount | grep ${BUILD_ROOT})" ] && \
		    mount -t unionfs -o dirs=${UNIONFS_DIR}=rw:${BUILD_ROOT}=ro unionfs ${BUILD_ROOT}
		unionctl ${BUILD_ROOT} --list
	    fi
	    return 0
	    ;;
	*)
	    echo mount-chroot-mount: unknown file system $fs
	    exit 1
	    ;;
    esac
}

write-vbuilder-log(){
    HRULE="======================================================================"

    [ -d ${BUILD_ROOT} ] || return 1

    if [ ! -f $VBUILDER_LOG ]; then
	cat<<EOF > $VBUILDER_LOG
${HRULE}
VBUILDER REPORT
DATE:		$(LANG=C date)
HOSTNAME:	$(hostname)
OS:		$(echo $($__chroot_sh "cat /etc/vine-release"))
%_arch:		$(echo $($__chroot_sh "rpm --eval %_arch"))

--version: ${VERSION}
$(echo $([ -z "${VARCH}" ] || echo "--arch: ${VARCH}"))
$(echo $([ -z "${CATEGORIES}" ] || echo "--category: ${CATEGORIES}"))
$(echo $([ $with_dist_upgrade -eq 1 ] && echo "--dist-upgrade"))
$(echo $([ $with_unionfs -eq 1 ] && echo "--unionfs"))
$(echo $([ -z "${TARGET}" ] || echo "--target: ${TARGET}"))
--bootstrap-dir: ${VBOOTSTRAP_DIR}
--cache-dir: ${CACHE_DIR}
--built-rpms-dir: ${BUILT_RPMS_DIR}
${HRULE}

[$VBUILDER_CONF]
$(cat $VBUILDER_CONF)

[History]
EOF
    else
	cat<<EOF >> $VBUILDER_LOG
$*
EOF
    fi
}

##############################################################################

Clean(){
    setup-vbootstrap

    # # output end-of-line in $VBUILDER_LOG
    # [ -f $VBUILDER_LOG ] && write-vbuilder-log ${HRULE}
    # Show-Info

    # mount-chroot --umount /home
    # mount-chroot --umount /tmp
    mount-chroot --umount vfs
    mount-chroot --umount archives_dir
    mount-chroot --umount unionfs_dir
    apt-get-update --host

    if [ $with_unionfs -eq 1 ]; then
	if [ -d ${UNIONFS_DIR} ]; then
	    echo -n "Cleaning build root \"${UNIONFS_DIR}\" via unionfs ... "
	    rm -rf ${UNIONFS_DIR}
	    echo "done."
	fi
    else
	if [ -d ${BUILD_ROOT} ]; then
	    echo -n "Cleaning build root \"${BUILD_ROOT}\" ... "
	    rm -rf ${BUILD_ROOT}
	    echo "done."
	fi
    fi

    echo "Cleanup a build farm for ${VERSION} done."
}

Build(){
    setup-vbootstrap

    if [ $with_dist_upgrade -eq 1 ]; then
	## make bootstrap of ${STABLE_VERSION}
	/usr/sbin/vbootstrap \
	    $(echo ${VERSION} | sed -e "s/VineSeed/${STABLE_VERSION}/") \
	    ${BUILD_ROOT}

	## aim apt-line to VineSeed
	sed -i "s/apt ${STABLE_VERSION}/apt VineSeed/g" \
	    ${BUILD_ROOT}/etc/apt/sources.list.d/main.list
    else
	/usr/sbin/vbootstrap ${VERSION} ${BUILD_ROOT}
    fi

    mount-chroot archives_dir
    mount-chroot vfs
    # mount-chroot /tmp
    # mount-chroot /home

    $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'

    ##!! 4.2 has no apt-sourceslist-{plus,nonfree,proposed-updates} packages
    case ${MAJOR_VERSION} in
	4.2)
	    $__chroot_sh "sed -i -e 's/main plus updates nonfree *$/$(echo ${CATEGORIES} | sed -e "s/,/ /"g) updates/g' /etc/apt/sources.list"
	    # [ $with_category_security -eq 1 ] && \
	    # 	echo 
	    ;;
	@@VBUILDER_STABLE_VERSION@@)
	    [ $with_category_plus -eq 1 ] && \
		$__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-plus'
	    [ $with_category_nonfree -eq 1 ] && \
		$__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-nonfree'
	    [ $with_category_proposed_updates -eq 1 ] && \
		$__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-proposed-updates'
	    # [ $with_category_security -eq 1 ] && \
	    # 	echo 
	    ;;
	VineSeed)
	    [ $with_category_plus -eq 1 ] && \
		$__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-plus'
	    [ $with_category_nonfree -eq 1 ] && \
		$__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-nonfree'
	    [ $with_category_test -eq 1 ] && \
		$__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-test'
	    ;;
    esac

    [ $with_dist_upgrade -eq 1 ] && \
	$__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'

    $__chroot_sh 'apt-get -qq -y install build-essential'

    [ $with_category_nonfree -eq 1 ] && \
	$__chroot_sh 'apt-get -qq -y install self-build-setup'

    $__chroot_sh 'apt-get -qq -y install etcskel shadow-utils'

    $__chroot_sh 'cd /dev && /sbin/MAKEDEV console'
    $__chroot_sh 'cd /dev && /sbin/MAKEDEV null'
    $__chroot_sh 'cd /dev && /sbin/MAKEDEV zero'
    $__chroot_sh 'cd /dev && /sbin/MAKEDEV random'
    $__chroot_sh 'cd /dev && /sbin/MAKEDEV urandom'

    $__chroot_sh '/usr/sbin/pwconv'
    $__chroot_sh "/usr/sbin/useradd ${BUILD_USER}"

    ##!! for rpm-4.8.0 or higher
    ##!! (See http://trac.vinelinux.org/wiki/Vine6/AboutUpdateToolchain)
    if [ "$(echo ${VERSION} | sed -e "s/\(VineSeed\).*/\1/")" = "VineSeed" ]; then
	$__chroot_sh "sed -i -e 's/^%_topdir/#%_topdir/' /home/${BUILD_USER}/.rpmmacros"
    fi

    ## output basic informations in chroot
    write-vbuilder-log
    write-vbuilder-log "build"

    # mount-chroot --umount /home
    # mount-chroot --umount /tmp
    mount-chroot --umount vfs
    mount-chroot --umount archives_dir
    apt-get-update --host

    echo "Making a build farm for ${VERSION} done."
}

Show-Info(){
    setup-vbootstrap

    [ -f $VBUILDER_LOG ] && cat $VBUILDER_LOG
}

RPM_Remove(){
    setup-vbootstrap-rpm
    mount-chroot unionfs_dir
    mount-chroot archives_dir
    mount-chroot vfs
    apt-get-update --chroot

    [ -f $RPM_PKG ] && Msg_NotPackageName_$LOCALE $RPM_PKG && exit 1
    $__chroot_sh "apt-get -y remove $BASE_RPM_PKG"

    write-vbuilder-log "remove-rpm $RPM_PKG"

    mount-chroot --umount vfs
    mount-chroot --umount archives_dir
    mount-chroot --umount unionfs_dir
    apt-get-update --host
}

RPM_Install(){
    setup-vbootstrap-rpm
    mount-chroot unionfs_dir
    mount-chroot archives_dir
    mount-chroot vfs
    apt-get-update --chroot

    $__chroot_sh "cd ${BUILD_DIR} && apt-get -y install $BASE_RPM_PKG"

    write-vbuilder-log "install-rpm $RPM_PKG"

    mount-chroot --umount vfs
    mount-chroot --umount archives_dir
    mount-chroot --umount unionfs_dir
    apt-get-update --host
}

RPM_Build(){
    setup-vbootstrap-rpm
    mount-chroot unionfs_dir
    mount-chroot archives_dir
    mount-chroot vfs
    apt-get-update --chroot

    [ ! -f $RPM_PKG ] && Msg_NotSourceRPM_$LOCALE $RPM_PKG && exit 1
    
    RPM_PKG_USER=$(stat -c %U $RPM_PKG)
    RPM_PKG_GROUP=$(stat -c %G $RPM_PKG)
    [ ! -z "${SUDO_UID}" ] && RPM_PKG_USER=${SUDO_UID}
    [ ! -z "${SUDO_GID}" ] && RPM_PKG_GROUP=${SUDO_GID}
    local __install="install -p -v -o ${RPM_PKG_USER} -g ${RPM_PKG_GROUP}"
    RPM_PKG_ARCH_LIST="RPMS/i386 RPMS/i686 RPMS/x86_64 RPMS/ppc RPMS/noarch SRPMS"
    [ -z "${TARGET}" ] || \
	RPM_PKG_ARCH_LIST="RPMS/${TARGET} ${RPM_PKG_ARCH_LIST}"

    ## make src.rpm for $VERSION
    $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpm -ivh $BASE_RPM_PKG'"
    $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpmbuild -bs --nodeps --clean --rmsource --rmspec ${BUILD_DIR}/SPECS/*.spec'"


    ## change ${DIST_RELEASE}
    BASE_RPM_PKG=$(echo $BASE_RPM_PKG | sed -e "s/vl\([0-9]*\)\./vl${DIST_RELEASE}\./")

    ## rebuild $BASE_RPM_PKG on ${DIST_RELEASE}
    $__chroot_sh "cd ${BUILD_DIR}/SRPMS && apt-get -o APT::Install::Virtual=true -y build-dep $BASE_RPM_PKG"
    $__chroot_sh "cd ${BUILD_DIR}/SRPMS && su ${BUILD_USER} -c 'rpmbuild --rebuild $RPM_OPTS $BASE_RPM_PKG'"
    $__chroot_sh "cd ${BUILD_DIR} && apt-get -y install $(find $BUILD_ROOT${BUILD_DIR}/RPMS -type f -regex '.*\.rpm' | sed -e s@${BUILD_ROOT}@@g -e 's|.*\/compat32-.*||g' -e 's|.*\/.*\.src\.rpm||g' -e 's/$/ \\/g')"

    ## copy built rpms to ${HOME}/rpm/ for each archtectures
    echo "Copying built rpms to ${BUILT_RPMS_DIR} for each archtectures ... "
    for i in $RPM_PKG_ARCH_LIST; do \
	if [ -d $BUILD_ROOT${BUILD_DIR}/${i} ]; then
	    if [ ! -d ${BUILT_RPMS_DIR}/${MAJOR_VERSION}/${i} ]; then
		$__install -d ${BUILT_RPMS_DIR}/${MAJOR_VERSION}/${i}/
		chown -R ${RPM_PKG_USER}:${RPM_PKG_GROUP} ${BUILT_RPMS_DIR}
	    fi
	    find $BUILD_ROOT${BUILD_DIR}/${i} -type f -regex '.*\.rpm' \
		-exec $__install -m0644 {} ${BUILT_RPMS_DIR}/${MAJOR_VERSION}/${i}/ \;
	fi
    done

    write-vbuilder-log "build-rpm $RPM_PKG"

    mount-chroot --umount vfs
    mount-chroot --umount archives_dir
    mount-chroot --umount unionfs_dir
    apt-get-update --host

    echo "done."
}


##############################################################################

setup-vbuilder

check-parameter $* || exit 1

while [ $# -gt 0 ]; do
    tmpARG=$1
    case $tmpARG in
	--version|--arch|--category|--target|--bootstrap-dir|--cache-dir|--built-rpms-dir)
	    shift
	    ;;
	--dist-upgrade|--unionfs|--with-compat32)
	    ;;
	--build-rpm|build-rpm|--install-rpm|install-rpm|--remove-rpm|remove-rpm)
	    shift
	    ;;
	--build|build|--clean|clean|--show-info|show-info)
	    ;;
	*)
	    echo unknown option $1
	    exit 1
	    ;;
    esac

    case $tmpARG in
	--version)
	    VERSION=$1
	    ;;
	--arch)
	    VARCH=$1
	    ;;
	--category)
	    CATEGORIES=$1
	    ;;
	--dist-upgrade)
	    with_dist_upgrade=1
	    ;;
	--unionfs)
	    with_unionfs=1
	    ;;
	--target)
	    TARGET=$1
	    RPM_OPTS="${RPM_OPTS} --target $TARGET"
	    ;;
	--with-compat32)
	    RPM_OPTS="${RPM_OPTS} --with compat32"
	    ;;
	--bootstrap-dir)
	    VBOOTSTRAP_DIR=$1
	    ;;
	--cache-dir)
	    CACHE_DIR=$1
	    ;;
	--built-rpms-dir)
	    BUILT_RPMS_DIR=$1
	    ;;
	--build-rpm|build-rpm)
	    RPM_PKG=$1
	    RPM_Build || exit 1
	    ;;
	--install-rpm|install-rpm)
	    RPM_PKG=$1
	    RPM_Install || exit 1
	    ;;
	--remove-rpm|remove-rpm)
	    RPM_PKG=$1
	    RPM_Remove || exit 1
	    ;;
	--show-info|show-info)
	    Show-Info
	    ;;
	--build|build)
	    Build
	    ;;
	--clean|clean)
	    Clean
	    ;;
    esac
    shift
done

exit