#!/bin/bash
#
#  JaM Builder 0.01
#
#      (C) 2004 Yasushi kinneko Date   kinneko@po.incl.ne.jp
#
#  "JaM Builder" is the script which reconstructs Japanese-izing 
#  'MEPIS' CD Linux Distribution.
#  This script based on "YAK Builder 0.02".
#  (C) 2003 Yutaro Ebihara  ebihara@si-linux.com
#  http://www.si-linux.co.jp/pub/yakbuilder/
#
# the JaM Builder is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundatin; either version 2 of the
# License, or (at youroption) any later version.
#
# the JaM Builder is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
#   Jan 02 2003  1st  relaease  v.0.01
#
# edit here
VERSION=0.01
CDROM_TITLE="JaMEPIS"
XTERM="/usr/bin/X11/xterm -title jam_chroot -bg lightpink"

# don't edit following 2 line
SOURCE_DIR=./source
MASTER_DIR=./master
CDROM=/mnt/auto/cdrom

# -------------------------------- functions -----------------------------------------

function cdromcopy () {
    check_root
    if  ! [ -d $SOURCE_DIR ]; then
	mkdir -p $SOURCE_DIR
    fi
    if  ! [ -d $MASTER_DIR ]; then
	mkdir -p $MASTER_DIR
    fi

    echo "copying from $CDROM to $MASTER_DIR directory....."
    rsync -avu --delete --progress $CDROM/* $MASTER_DIR

    # get cloop.o from boot.img
    if ! [ -d /mnt/image ]; then
    	mkdir -p /mnt/image
    fi
    mount -o loop $CDROM/boot/boot.img /mnt/image
    cp /mnt/image/initrd.gz ./
    umount /mnt/image
    gzip -d ./initrd.gz
    mount -o loop ./initrd /mnt/image
    cp /mnt/image/modules/cloop.o ./
    umount /mnt/image
    rm ./initrd
    rm -r /mnt/image
    if ! [ -d /dev/cloop ]; then
    	mknod /dev/cloop b 240 0
    fi

    if ! [ -d /mnt/cloop ]; then
    	mkdir /mnt/cloop
    fi
    insmod ./cloop.o file=$MASTER_DIR/linux/linux
    mount -o ro /dev/cloop /mnt/cloop
    echo ""
    echo "copying from MEPIS cloop image to $SOURCE_DIR directory....."
    rsync -avu --delete /mnt/cloop/* $SOURCE_DIR
    umount /mnt/cloop
    rmmod cloop
    rm $MASTER_DIR/linux/linux

    echo ""
    echo "okay finish. type 'jambuilder edit' and add or delete packages."
}

function edit (){
    check_root
    echo "Add or delete packages."
    echo "type 'exit' when you want to exit."
    mount -t proc proc $SOURCE_DIR/proc
    mv $SOURCE_DIR/etc/resolv.conf $SOURCE_DIR/etc/resolv.conf-jam
    cp /etc/resolv.conf $SOURCE_DIR/etc/resolv.conf
    if [ $DISPLAY ]; then
	$XTERM -e chroot $SOURCE_DIR
    else
        chroot $SOURCE_DIR
    fi
    rm $SOURCE_DIR/etc/resolv.conf
    mv $SOURCE_DIR/etc/resolv.conf-jam $SOURCE_DIR/etc/resolv.conf
    umount $SOURCE_DIR/proc
}

function mkcloop () {
    check_root
    if ! [ -d $SOURCE_DIR ]; then
	echo "ERROR! there is no $SOURCE_DIR dir"
	exit 1
    fi
    if ! [ -d $MASTER_DIR ]; then
	echo "ERROR! there is no $MASTER_DIR dir"
	exit 1
    fi

    #
    # set log files to zero byte
    #
    echo "clean log files"
    cleanup_logs

    # build JaMEPIS cloop image
    mkisofs -R -l -V "JaMEPIS iso9660 filesystem" -hide-rr-moved -v \
           $SOURCE_DIR | create_compressed_fs - 65536 > $MASTER_DIR/linux/linux

    echo "Finish!"
    echo "Next, type 'jambuilser mkiso'."
}

function cleanup_logs () {
    check_root
    find $SOURCE_DIR/var/log  -exec cp /dev/null {} \;
}

function mkiso () {
    check_root
    if ! [ -d $MASTER_DIR ]; then
	echo "ERROR! there is no $MASTER_DIR dir"
	exit 1
    fi

    mkisofs -r -J -l -V $CDROM_TITLE -hide-rr-moved -v \
           -b boot/boot.img \
           -c boot/boot.cat \
           -o JaMEPIS-cd.iso \
           $MASTER_DIR/

    echo "Finish!"
    echo "Next, burn CD-ROM for Utilities(like 'K3b') and type 'jambuilder cleanall'."
}

function cleanall () {
    check_root
    rm -rf $SOURCE_DIR $MASTER_DIR
    rm -rf /mnt/cloop
    rm /dev/cloop
    rm ./cloop.o
}

function check_root () {
    if [ $UID != 0 ]; then
	echo "ERROR! you have to run as a root, or use sudo and try again."
	exit 1
    fi
}


# ============================
#
# main routine start from here
#
# ============================

echo "JaM Builder version $VERSION"
echo "(C) 2004  Yasushi kinneko Date  <kinneko@po.incl.ne.jp>"
echo ""

case "$1" in
    cdcopy)
        ls /mnt/auto/cdrom > /dev/null        # autofs dummy access
	if grep -q /dev/cdrom /proc/mounts; then
		echo "[cdcopy] copy from $CDROM directory"
		cdromcopy
	else
		echo "ERROR: cdrom is not mount on /dev/cdrom"
		echo "mount the cdrom and try again"
	fi
    ;;
    edit)
	echo "[edit] edit chroot directory"
	edit
    ;;
    mkcloop)
	echo "[mkcloop] build cloop image to $MASTER_DIR/linux/linux"
	mkcloop
    ;;
    mkiso)
	echo "[mkiso] build iso image"
	mkiso
    ;;
    cleanall)
	echo "[clean] remove and cleanup $SOURCE_DIR , $MASTER_DIR directory"
	echo -n "Sure? type 'yes' to cleanup ->"
	read yesno
	if [ $yesno == 'yes' ]; then
	    cleanall
	fi
    ;;
    *)
	echo "Usage: jambuilder {cdcopy|edit|mkcloop|mkiso|cleanall}"
	echo ""
	echo "cdcopy:     copy files from $CDROM to ./master and ./source"
	echo "            setup editting environment"
	echo "edit:       chroot into the JaMEPIS Userland, install and remove packages"
	echo "mkcloop:    build cloop JaMEPIS Userland image"
	echo "mkiso:      build bootable iso image file"
	echo "cleanall:   cleanup $SOURCE_DIR , $MASTER_DIR directory"
	echo ""
	echo "kinneko <kinneko@po.incl.ne.jp>"
	exit 1
    ;;
esac

exit 0

