#!/bin/sh
# Please make sure this file can be executed by both "bash" and "ash".
# Also, the external functions used has to be restricted to the ones available
#   via busybox if it is to be used on the boot floppies

# (c) 2000 Software in the Public Interest
# Released under the General Public License, version 2 or above
# Please contact modconf@packages.debian.org for questions about this package

umask 022

source_eval () {
  if [ -z "$LANG" ]; then
    . $ModuleHelpDir/eval_C
  else
    LINGUA=$(echo $LANG | $sed -e 's/_.*//')
    if [ -f $ModuleHelpDir/eval_$LINGUA ]; then
       . $ModuleHelpDir/eval_$LINGUA
    else
       . $ModuleHelpDir/eval_C
    fi
  fi
}

parse_cmdline () {
  while [ $# -ne 0 ]; do
  case "$1" in
    --source)
      Source=$2
      shift
      ;;
    --target)
      Target=$2
      shift
      ;;
    --libdir)
      ModuleContentsDir=$2
      shift
      ;;
    --helpdir)
      ModuleHelpDir=$2
      shift
      ;;
    --restrict-section)
      Restrict_Section="$Restrict_Section $2"
      shift
      ;;
    --exclude-section)
      Exclude_Section="$Exclude_Section $2"
      shift
      ;;
    --load-after)
      Load_After="$Load_After $2"
      shift
      ;;
    --load-before)
      Load_Before="$Load_Before $2"
      shift
      ;;
    --load-only)
      Load_Only="$Load_Only $2"
      shift
      ;;
    --run-shell)
      Run_Shell="$Run_Shell $2"
      shift
      ;;
    --tty)
      ttydev=$2
      shift
      ;;
    --test)
      SIMULATE=echo
      ;;
    *)
      ModuleHelpDir=${ModuleHelpDir:-$Target/usr/share/modconf}
      source_eval
      write_it_down "$msg_Invalid_option"
    esac
    shift
  done
}

interactive_main () {
  local text="$msg_Select_Category_l"
  local directory
  
  echo 'menu "$text" "$msg_Select_Category" \' > $TempFile
  echo '"$msg_Exit" "$msg_Finished_these" \'  >> $TempFile
  echo '" " " " \' >> $TempFile

  for directory in $dir_descrs; do
    if [ ! -d $Dir_Prefix/$directory -a $Source = mounted ]; then 
      continue;
    fi

    if [ -n "$Restrict_Section" ]; then
      if in_list $directory $Restrict_Section; then
        true
      else
        continue
      fi
    fi

    local help=""
    local summary=""

    eval "summary=$`echo dir_descr_$directory`"

    echo "\"$directory\"" \\ >> $TempFile
    echo "\"$summary."\" \\ >> $TempFile
  done
  echo "" >> $TempFile

  while true; do
    local directory status
    if [ -n "$One_Restricted_Section" ]; then
      directory=$One_Restricted_Section
    else
      directory="`. $TempFile`"
      if [ $? -ne 0 ]; then return 1; fi
    fi

    case "$directory" in
    " ")
      ;;
    "$msg_Exit")
      return 0;;
    *)
      module_directory_menu "$directory" \
       "$msg_Select_modules_l" "`eval echo \\\"$msg_Select_modules\\\"`"
      if [ -n "$One_Restricted_Section" ]; then
        return 0;
      fi
      ;;
    esac
  done
  rm -f $TempFile
}

## ********* Main  ************
Source=mounted
parse_cmdline $*

if [ -d $Target/usr/share/modconf ]; then
    libdir=$Target/usr/share/modconf
else
    libdir=/usr/share/modconf
fi

. $libdir/params
. $libdir/util
. $libdir/dialog

# default if not set from cmdline
ModuleHelpDir=${ModuleHelpDir:-$Target/usr/share/modconf}

source_eval

if [ -z "$KernelVersion" ]; then
  msgBox "$msg_kernel_version_problem" "$msg_Problem"
  return 1
fi

check_dirs
check_modules
check_modules_conf

if [ "$Source" = "floppy" ]; then
  if [ -f $ModuleContentDir/modcont ]; then
    . $ModuleContentDir/modcont
  else
    echo "$msg_Cannot_open $ModuleContentDir/modcont"
    exit 1
  fi
else
  dir_descrs=""
  for i in $(cd $Dir_Prefix; echo *); do
    if [ -d $Dir_Prefix/$i -a \
      -f $(first $Dir_Prefix/$i/*.o) ]; then
      if [ -n "$Exclude_Section" ] && 
                            in_list $i $Exclude_Section ; then
        true
                  else
        dir_descrs="$dir_descrs $i"
      fi
    fi
  done
fi

s=$(second $Restrict_Section)
if [ -n "$Restrict_Section" -a -z "$s" ]; then
    One_Restricted_Section=$(first $Restrict_Section)
fi

for i in $Load_Before; do
  get_and_insmod $i;
done

if [ -z "$Load_Only" ]; then
  interactive_main
else
  Load_After=$Load_Only
fi

for i in $Load_After; do
  get_and_insmod $i
done

rm -f $TempFile $TempFile.1 $TempFile.2 $TempFile.3 $TempFile.4

exit 0
