#! /bin/sh -e
#
# Pass the information needed by debian-edu-install into
# /target/etc/debian-edu/config.

. /usr/share/debconf/confmodule

log() {
    logger -t 07debian-edu-install "$@"
}

if db_get mirror/suite && [ "$RET" ] ; then
    SUITE=$RET
fi

if db_get mirror/distribution && [ "$RET" ] ; then
    DISTRIBUTION=$RET
fi

db_get debian-edu-install/profile
PROFILE=$RET

db_get debian-installer/language
LANGCODE=$RET

db_get debian-installer/locale
LOCALE=$RET

# install debian-edu-install in chroot if a network install
if grep -q "^deb file:///cdrom" /target/etc/apt/sources.list ; then 
  log "Doing nothing, cd-based install should install debian-edu-config automaticly"
elif grep -qe "^deb.*local" /target/etc/apt/sources.list ; then 
  log "local apt-source is already defined, debian-edu-config should be present"
else
  # Check if there is a local source availible from the same location as the main source is
  MAINSOURCE=$(sed -ne 's:^deb \(.*\) \(.*\) main.*:\1:p' /target/etc/apt/sources.list | head -1)
  if wget -qO - $MAINSOURCE/dists/$DISTRIBUTION/Release | grep "local/binary" ; then 
    log "Adding local apt-source from the same location as main"
    sed -ne 's:^\(deb.*\)main.*:\1local:p' /target/etc/apt/sources.list >> /target/etc/apt/sources.list
  fi
  log "Updating Package list"
  chroot /target apt-get update
  log "Fetching debian-edu-install"
  if chroot /target apt-get -qy install debian-edu-install ; then 
    log "installation of debian-edu-install was ok"
  else
    log "installation of debian-edu-install failed, continuing anyway"
  fi
fi

