#!/bin/bash

# Copyright (C) 2004-2010 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# This file is part of le-base
#
# le-base 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 Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.


#------------------------------------------------------------------------------
DIR=$(readlink -f "$0")
. "`dirname "$(echo "$DIR")"`/common-functions.sh"

checkUser || exit 1
checkMountedFS || exit 2
loadConf "$PREFIX/conf/$1/create-iso.conf" $1
initChroot

# Configures ubuntu repositories
#cp -f /etc/apt/sources.list /etc/apt/sources.list.BKP
sed -i 's/# deb/deb/g' "$CHROOTDIR/etc/apt/sources.list"

# C3SL repositories
#sed -i 's@//security@//br.archive@g' /etc/apt/sources.list
#sed -i 's@//archive@//br.archive@g' /etc/apt/sources.list
#sed -i 's@//security.ubuntu.com@//ubuntu-archive.locaweb.com.br@g' \
sed -i 's@//security.ubuntu.com@//ubuntu.c3sl.ufpr.br@g' \
    "$CHROOTDIR/etc/apt/sources.list"
#sed -i 's@//archive.ubuntu.com@//ubuntu-archive.locaweb.com.br@g' \
sed -i 's@//archive.ubuntu.com@//ubuntu.c3sl.ufpr.br@g' \
    "$CHROOTDIR/etc/apt/sources.list"
#grep "^deb" /etc/apt/sources.list.BKP >> /etc/apt/sources.list
# Configures LE repositories
rm -f "$CHROOTDIR/etc/apt/sources.list.d/le-repos.list"
rm -f "$CHROOTDIR/etc/apt/sources.list.d/java-repos.list"
echo "deb http://$LEHOST educ main" \
        >> "$CHROOTDIR/etc/apt/sources.list.d/le-repos.list"
echo "deb http://$LEHOST $LEMIRROR main" \
        >> "$CHROOTDIR/etc/apt/sources.list.d/le-repos.list"
echo "deb http://archive.canonical.com/ lucid partner" \
        >> "$CHROOTDIR/etc/apt/sources.list.d/java-repos.list"

chroot "$CHROOTDIR" wget http://windu.c3sl.ufpr.br/le/public.key -O /dev/stdout \
    > "$TMP/windu.key"
chroot "$CHROOTDIR" apt-key add - < "$TMP/windu.key"

# Videolan repositories
echo 'deb http://download.videolan.org/pub/debian/stable/ /
deb-src http://download.videolan.org/pub/debian/stable/ /' \
		>> "$CHROOTDIR/etc/apt/sources.list.d/videolan-repos.list"

chroot "$CHROOTDIR" wget http://download.videolan.org/pub/debian/videolan-apt.asc -O /dev/stdout \
    > "$TMP/videolan.asc"
chroot "$CHROOTDIR" apt-key add - < "$TMP/videolan.asc"

# Changes distro
chroot "$CHROOTDIR" apt-get -y --force-yes update

# Avoid grub-probe errors
cp -f "$CHROOTDIR"/usr/sbin/update-grub "$CHROOTDIR"/usr/sbin/update-grub.tmp
echo "#!/bin/bash" > "$CHROOTDIR"/usr/sbin/update-grub
echo "exit 0" >> "$CHROOTDIR"/usr/sbin/update-grub
chmod a+x "$CHROOTDIR"/usr/sbin/update-grub

closeChroot

exit 0