#!/bin/bash
#fetch_ebuilds.sh
#Writed by v12aml ( v12aml@gmail.com )
VERSION="0.3"

source /etc/make.conf

do_lock()
{
if [ -f /tmp/fetch_ebuilds.kill ]
then
    logger "download_portage_group.sh: exit by lock_file.kill"
    exit 0
elif [ -f /tmp/fetch_ebuilds.pause ]
then
    echo -e "\n\nPAUSED\n\n"
    while [ -f /tmp/fetch_ebuilds.pause ]
    do
        sleep 60
    done
fi
}


LIST1=`find ${PORTDIR-/usr/portage} -type f -name '*.ebuild'`
LIST2=`find ${PORTDIR_OVERLAY-/usr/local/portage} -type f -name '*.ebuild'`
LIST=`echo $LIST1 $LIST2`
N=`echo $LIST | wc -w`
COUNT=1
echo "$N to go"

for loop in $LIST
do
    do_lock
    APP=`echo $loop | cut -d/ -f4,6`
    echo -e "\n\033[40;32mWorking   \033[40;34m($COUNT/$N)		\033[40;36m[ \033[40;33m${APP%%.ebuild}\033[40;32m \033[40;36m]\n\033[40;37m"
    ebuild $loop fetch
    echo -e "\n\033[40;32mFinished  \033[40;34m(~$P%)			\033[40;36m[ \033[40;33m${APP%%.ebuild} \033[40;36m]\033[40;32\n\033[40;37m"
    P=`expr $COUNT / \( $N / 100 \)`
    COUNT=`expr $COUNT + 1`    
done

#EOF