#!/bin/sh
# Copyright 2003  Slackware Linux, Inc.,  Concord, CA  USA
#    All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

# Slackware 9.1 から流用(盗用 ;-)した，複数 CD 用のインストールスクリ
# プト(cpkgtool に対するラッパー)です．改めて Patrick さん(だろう)のス
# クリプトを眺めたけど，ループの中で series.out を series.in に書き変
# えて処理を進めるあたりとかは名人芸だと思ふ．
#
# Plamo のインストーラ担当として，Patrick さんに最大限の敬意を表しつつ，
# 必要最小限の修正だけで流用させていただきます (_ _)
#
# -- kojima 2004/01/10

# globals

TMP=/var/log/setup/tmp

# functions

updatetagfiles() {
    if [ "$MODE" = "RECOM" ]; then
	mkdir -p $TMP/tagfiles/$series
	cp $SRCPATH/$series/tagfile $TMP/tagfiles/$series/tagfile
    elif [ "$MODE" = "EXPERT" ]; then
	cat $TMP/series/series.in | while read series ; do
            if [ "$LANG" = "ja_JP.UTF-8" ]; then
                if [ -r $SRCPATH/$series/maketag ]; then
	            sh $SRCPATH/$series/maketag
                fi
            else
                if [ -r $SRCPATH/$series/emaketag ]; then
	            sh $SRCPATH/$series/emaketag
                fi
            fi
            #
            # (e)maketag makes SeTnewtag in /tmp. Change it to $TMP ?
            #
	    if [ -r /tmp/SeTnewtag ]; then
		mkdir -p $TMP/tagfiles/$series
		mv /tmp/SeTnewtag $TMP/tagfiles/$series/tagfile
	    fi
	done
    fi
}

errorcode() {
  if [ $1 = 99 ]; then
    # User aborted installation
    exit 1
  else
      if [ "$LANG" = "ja_JP.UTF-8" ]; then
	  dialog --timeout 600 --title "パッケージエラー #$1" --msgbox \
		 "$2 パッケージをインストールする際に致命的なエラーが発生しました。 \
パッケージが壊れている、インストールメディアに不良がある等の問題 \
により、パッケージを正常に読み取ることができませんでした。 \
Enter キーを押すことで先に進めることは可能ですが、システムの \
動作に必須のパッケージの場合、インストールしたシステムが正常に \
動作しないかも知れません。" 11 72
      else
	  dialog --timeout 600 --title "package error #$1" --msgbox \
		 "Fatal error occurred when installing $2 package. \
Cannot read the package correctly because it was broken or error on install media.\
You can ignore this error and proceed with Enter, but if this is the critical software for Linux, installed system wouldn't be able to run properly." 11 72
      fi
  fi
}

installseries() {
  # echo "in installseries $1"
  # echo "SRCPATH:$SRCPATH"
  # sleep 5
  if [ -d $SRCPATH/$1 ]; then
    # First, make sure our tagfiles are in order:
    if [ ! -r $TMP/tagfiles/$1/tagfile -a ! $MODE = full ]; then
      updatetagfiles $1
    fi
    # First, make sure there's at least one package:
    # if ! ls $SRCPATH/$series/*.t[gbxz][zst]? 1> /dev/null 2> /dev/null ; then
    chk=`ls $SRCPATH/$series/*.t[gbx]z $SRCPATH/$series/*.tzst`
    if [ "$chk.x" == ".x" ]; then
      echo "no packages found in $SRCPATH/$series "
      sleep 3
      return 1
    fi
    dialog --infobox "
Installing package series ==>$1<==
" 5 45
    sleep 1
    # Install the package series:
    for package in $SRCPATH/$series/*.{t[gbx]z,tzst} ; do
      if [ "$MODE" = "RECOM"  -o "$MODE" = "EXPERT" ]; then # install the package
        installpkg -root $ROOTDIR -menu -tagfile $TMP/tagfiles/$series/tagfile $package
        ERROR=$?
      fi
      if [ ! $ERROR = 0 ]; then
        errorcode $ERROR $package
      fi
    done
  else # requested, but not on media.  defer until later.
    echo $1 >> $TMP/series/series.out
  fi 
}

remount_disc() {
  umount $DEVICE
  eject $DEVICE
  dialog --title "メディアの交換" --menu "インストールディスクを次のものに交換して \
ENTER キーを押してください" \
10 62 2 \
  "Continue" "次のメディアからインストールを継続する" \
  "Quit" "インストールを終了する" 2> $TMP/reply
  if [ ! $? = 0 ]; then
    REPLY="Quit"
  else
    REPLY="`cat $TMP/reply`"
  fi
  rm -f $TMP/reply
  if [ "$REPLY" = "Quit" ]; then
    errorcode 99
  fi;
  mount $DEVICE $MOUNTPOINT 1> /dev/null 2> /dev/null
}

# /* main */

# Process command line:
if [ $# -gt 0 ]; then # there are arguments to the command
  while [ $# -gt 0 ]; do
   # echo "params: $1"
   case "$1" in
   "--promptmode")
     MODE=`echo $2` ; shift 2 ;;
   "--srcpath")
     SRCPATH=`echo $2` ; shift 2 ;;
   "--mountpoint")
     MOUNTPOINT=`echo $2` ; shift 2 ;;
   "--target")
     ROOTDIR=`echo $2` ; shift 2 ;;
   "--device")
     DEVICE=`echo $2` ; shift 2 ;;
   "--series")
     SERIES=`echo $2` ; shift 2 ;;
   *)
     echo "Unrecognized option $1" ; shift 1 ;; 
   esac
  done
else
  exit 1;
fi
# Empty out temporary directories:
rm -rf $TMP/series $TMP/tagfiles
mkdir -p $TMP/series $TMP/tagfiles
# Create initial list of series to install:
# echo "SERIES: $SERIES"
# sleep 5
for tseries in $SERIES ; do
  echo $tseries | tr [A-Z] [a-z] >> $TMP/series/series.in
done
# Main loop:
while [ -r $TMP/series/series.in ]; do
  cat $TMP/series/series.in | while read series ; do
    series=$series
    echo "installing $series"
    installseries $series;
  done
  rm -f $TMP/series/series.in
  if [ -r $TMP/series/series.out ]; then
    mv $TMP/series/series.out $TMP/series/series.in
    if [ "$DEVICE" = "noremount" ]; then
      # we've done all we can here
      break
    else # see if there's anything we need on the next disc
      remount_disc
    fi
  fi
done

