#!/bin/sh
##########################################################################
##                                                                      ##
## detects  current  ip  addresses  for  the  debian  mirrors  and      ##
## updates _system/_cd/etc/apt/sources.list  accordingly.  Its main     ##
## use is in the other  pkg  scripts  to ensure that only valid ip      ##
## addresses are placed into sources.list, but it can also be called    ##
## independently.                                                       ##
##                                                                      ##
## Important:  This  script  should  only  be  called  out  of  the     ##
## utils directory.                                                     ##
##                                                                      ##
## copylefted by Timo Benk <t_benk@users.sourceforge.net>               ##
##                                                                      ##
##########################################################################

. ../config_build
. ../"$SCRIPTS"/misc/lib/lib_fail

# first we need some ip addresses
# the following perl oneliner is contributed by Volker Apelt <volker_apelt@yahoo.de>
echo -n "detecting ip for $DEBIAN_MIRROR: "
MIRROR=$(host $DEBIAN_MIRROR | perl -n -e 'm/((\d{1,3}\.){3}\d{1,3})/; print "$1\n";' | grep -v '^[[:space:]]*$' | head -n1)
echo $MIRROR

if [ "$MIRROR" ]; then
    echo "updating ../$ROOTFS/etc/apt/sources.list"
    cat <<EOF > ../$ROOTFS/etc/apt/sources.list
# See sources.list(5) for more information, especialy
# Remember that you can only use http, ftp or file URIs
# CDROMs are managed through the apt-cdrom tool.

# $DEBIAN_DISTRIB 
deb ftp://$MIRROR/debian $DEBIAN_DISTRIB $DEBIAN_COMPONENTS 
deb ftp://$MIRROR/debian-non-US $DEBIAN_DISTRIB/non-US $DEBIAN_COMPONENTS 

EOF
else
    exit 1
fi
