#!/bin/ash

# This file is part of PLD batch-installer
# Copyright (C) 2001 CYBER Service
# 
# This program 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# Author: Rafa Kleger-Rudomin
# Contributors:


# this script is responsible for installing rpm packages


################# utility functions ####################

message () { log warn "`nls "Installing RPM packages"`"; }

##########################################
################# main ###################
##########################################

PATH=$PATH:. . installer-functions

find_config_paths $@

# oh well, just in case...
load_sys_config
hostname $net_hostname
if [ -z "$usb_controller" -o "$usb_controller" = "auto" ]; then
  if test -f /lib/modules/`uname -r`/usbcore.o ; then
    : ok
  else
    load_package usb-mod
  fi
fi

load_config
find_source_dir

set -e 
#set -x

rpms_dir="PLD/RPMS"
source_mountpoint=/src

is_dest_mounted

# bug in poldek REMOVE THIS AFTER IT IS FIXED
ln -sfn /dest/installer /installer

# now we can load rpm (notice that this is loaded into dest!)
load_package rpm

# Even for statically linked binary (like poldek) there is need
# for libresolv.so and nssswitch.conf file... :< 
load_package libc

# make sure we have bootloader
if test "$boot_loader" -a "$boot_loader" != none ; then
    add_rpm $boot_loader
    loader=$boot_loader
fi

# if we have pcmcia_core loaded changes are big, that we'll need
# pcmcia-cs on installed box
if lsmod | grep '^pcmcia_core ' ; then
    add_rpm pcmcia-cs
fi

# if we were installing using dhcp or user asked for dhcp ipaddr in
# installer.sysconf, better add dhcp client
if test "$net_ipaddr" = dhcp || \
   ( load_sys_config; test "$net_ipaddr" = dhcp ); then
  if has_rpm pump || has_rpm dhcp-client ; then
    : skip
  else
    add_rpm dhcpcd
  fi
fi

if is_smp ; then
  if is_mini_iso ; then
    : # skip, no kernel-smp on mini-iso
  elif has_rpm kernel ; then
    grep -v '^kernel$' $pkgsfile > $pkgsfile.tmp
    cat $pkgsfile.tmp > $pkgsfile
    rm -f $pkgsfile.tmp
    add_rpm kernel-smp
  fi
fi

export PATH="/dest/installer/bin:$PATH"

case "$source" in
    net)
	dir="$source_device/$source_dir/$rpms_dir"
	echo $dir | grep -q ':/' || dir="ftp://$dir" || :
	rm -rf /dest/installer/tmp/*
	;;
    *)
	dir="${source_mountpoint}${source_dir}/$rpms_dir"
	;;
esac

test -d /etc/rpm || mkdir /etc/rpm
echo -n > /etc/rpm/macros

if [ "$pkgs_install_docs" = no ] ; then
  echo "# Don't install documentation along with packages:" >> /etc/rpm/macros
  echo '%_excludedocs 1' >> /etc/rpm/macros
else
  echo "# Do install documentation along with packages:" >> /etc/rpm/macros
  echo '%_excludedocs 0' >> /etc/rpm/macros
fi

if [ "$pkgs_install_langs" = "" ] ; then
  pkgs_install_langs=all
fi

echo "# Set of langauges in which resources are to be installed:" >> /etc/rpm/macros
echo "%_install_langs $pkgs_install_langs" >> /etc/rpm/macros

log info "langs = $pkgs_install_langs, docs = $pkgs_install_docs"

case "$pkgs_installer" in
    poldek)
	load_package poldek

	# nasty workaround for grow_inode: allocation failed on low-memory
	# systems, with 2.2 kernels.
	if [ $KERNEL_VERCODE -lt 4 ] ; then
	    log info "Enabling workaround for 'grow_inodes: allocation failed'"
	    count=1
	    mkdir /dest/installer/tmp/workaround
	    while test $count -le 2000; do
		count=$(($count+1))
		mknod /dest/installer/tmp/workaround/$count c 1 3 || break
	    done
	    rm -fr /dest/installer/tmp/workaround
	fi

	export TMPDIR=/installer/tmp
	poldek_log=/dest/installer/poldek.log
	echo -n > $poldek_log
	poldek_opt="--mercy --mkdir --cachedir=/dest/installer/tmp"
	poldek_opt="$poldek_opt --log=$poldek_log"
	poldek_opt="$poldek_opt --install-dist=/dest -v --pset $pkgsfile"

	case "$source" in 
	    cdrom)
	    	if test -r ${source_mountpoint}${source_dir}/.pkgsplit.conf ; then
		  cp ${source_mountpoint}${source_dir}/.poldekrc /tmp/poldekrc
		  cp ${source_mountpoint}${source_dir}/.pkgsplit.conf /tmp/pkgsplit.conf
		  poldek_opt="$poldek_opt --conf=/tmp/poldekrc --split-conf=/tmp/pkgsplit.conf"
		else
		  poldek_opt="$poldek_opt --noconf -s $dir/"
		fi
		;;
	    *)
		poldek_opt="$poldek_opt -s $dir/"
		case "$net_proxy" in
		  "" | none )
		    : nothing
		    ;;
		  * )
		    export SNARF_PROXY="$net_proxy"
		    echo "ftp_get       = /bin/snarf %P %D" > /tmp/poldekrc
		    echo "http_get      = /bin/snarf %P %D" >> /tmp/poldekrc
		    poldek_opt="$poldek_opt --conf=/tmp/poldekrc"
		    ;;
		esac
		;;
	esac
	message
	if LANG=C LC_ALL=C poldek $poldek_opt ; then
	  cat $poldek_log >> /var/log/installer
	else
	  log error "$(nls "PACKAGES INSTALLATION FAILED!!!")"
	  # filter out important information
	  grep error: $poldek_log
	  log error "$(nls "You might want to try installing just basic packages,")"
	  log error "$(nls "and install rest after reboot.")"
	  cat $poldek_log >> /var/log/installer
	  exit 1
	fi
	;;
    *)
	die "Unknown RPM installer $pkgs_installer"
	;;
esac

# create /etc/mtab
# workaround for buggy e2fsprogs-1.20, to be deleted when it is corrected
echo -n >/dest/etc/mtab

# copy configs into /dest
cp -f /etc/installer.conf /dest/etc && chmod 600 /dest/etc/installer.conf
cp -f /etc/installer.sysconf /dest/etc && chmod 600 /dest/etc/installer.sysconf
cp -f $pkgsfile /dest/etc && chmod 600 /dest/etc/installer.pkgs

case $source in
  net )
    if test -f /dest/etc/poldek.conf ; then
      cp /dest/etc/poldek.conf /tmp
      ( 
        echo "# Installer changed ftp://ftp.pld.org.pl/ to $source_device in this file"
	sed -e "s|ftp://ftp.pld.org.pl|$source_device|g" /tmp/poldek.conf
      ) > /dest/etc/poldek.conf
    fi
    ;;
esac

# remove garbage
rm -fr /dest/installer

case "$source" in
  cdrom | disk )
    if grep -q '/src ' /proc/mounts ; then
      umount /src || :
    fi
    ;;
esac

# vim:ft=sh
