#!/bin/bash
#
cd /build_tools/larch8
#./build_all
#exit 0

OLD=`pwd`
cd /build_tools
. /etc/makepkg.conf
clear
cd larch8 || exit 1
declare -a menuarray
menuarray=( `ls   larch0/profiles/ |grep  $CARCH`) 
echo ${array1[@]}

element_count=${#menuarray[*]}
element_count=`expr $element_count - 1`





if [  x$1 = x ]
then
	for i in `seq 0 $element_count`
	do
	 echo "$i ) ${menuarray[$i]}"
	 echo
	done
	echo "Select the profile to use:"
	read a
	profile=${menuarray[$a]}

else
	if [ x$1 = xmv ]
	then
		profile=MV-local
	else
		profile=$1
	fi
fi
echo $profile
#if  [ x$a = x ]
#then
#	profile=${menuarray[3]}
#fi

echo "Running larch for $profile"
#larch7
#time ./larch-archin -f -p larch0/profiles/$profile install && ./larch-larchify -p larch0/profiles/$profile && ./larch-live_iso -p larch0/profiles/$profile
#larch8
time ./larch-archin -f -p larch0/profiles/$profile install && ./mount4chroot && ./larch-larchify -p larch0/profiles/$profile && ./larch-medium -o /tmp/test2.iso -p larch0/profiles/$profile && ./umount4chroot
status=$?
if [ $status = 0 ]
then
	if [ -f /usr/bin/isohybrid ]
	then
		echo "Running isohybrid..."
		/usr/bin/isohybrid /tmp/test2.iso 
	else
		echo "Syslinux needs to be installed to run isohybrid"
	fi
	
	echo "*****************************************************"
	echo "your $profile iso is complete (/tmp/test2.iso)"
	echo "*****************************************************"
fi
#./larch-larchify -p larch0/profiles/$profile && ./larch-medium -o /tmp/test3.iso -p larch0/profiles/$profile
#time  ./larch-medium -o /tmp/test3.iso -p larch0/profiles/$profile


#if [ $profile = "MV-local" ]
#then
#	echo "your $profile iso has been moved to /tmp/MV-2.iso"
#	mv /home/larchroot/.larch/mylivecd.iso /tmp/MV-2.iso
#else
#	echo "your $profile iso has been moved to /tmp/testcd-2.iso"
#	if [ -e linhes.iso ]
#	then
#	     echo "moving linhes.iso to /tmp/test2.iso"
#	     mv linhes.iso /tmp/test2.iso
#	     ls -lah /tmp/test2.iso
#        fi

#fi
date
cd $OLD