#!/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

# preparations, resolve the LANG alias
if [ -n "$LANG" ] ; then
   if grep "^$LANG" /etc/locale.alias >/dev/null 2>&1; then
      export LANG=$(grep "^$LANG" /etc/locale.alias| sed -e 's/.*\(.._.*\).*/\1/')
   fi
fi

source_eval () {
  sed1=${sed:-sed}
  if [ -z "$LANG" ] && [ -z "$LC_MESSAGES" ]; then
    . $ModuleHelpDir/eval_C
  else
    if [ -n "$LC_MESSAGES" ]; then
       LINGUA=$(echo $LC_MESSAGES | $sed1 -e 's/_.*//')
       if (echo $LC_MESSAGES | grep -i '\.utf.8' > /dev/null) ; then
         LINGUA=$LINGUA.utf-8
       fi
    else
       LINGUA=$(echo $LANG | $sed1 -e 's/_.*//')
       if (echo $LANG | grep -i '\.utf.8' > /dev/null) ; then
         LINGUA=$LINGUA.utf-8
       fi
    fi
    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)
      ModuleHelpDir=$2
      shift
      ;;
    --contentdir)
      ModuleContentDir=$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
      local reply
      echo ""
      echo "$msg_Invalid_option"
      echo ""
      echo "$msg_Something_went_wrong"
      read reply
    esac
    shift
  done
}

interactive_main () {
  local text="$msg_Select_Category_l"
  local directory
  local tdir

  machine=`uname -m`
  
  echo 'menu "$text" "$msg_Select_Category" "@DEFAULTITEM@" \' > $TempFile
  echo '"$msg_Exit" "$msg_Finished_these" \'  >> $TempFile
  if [ "$machine" = "s390" ]; then
    echo '" " " " \' >> $TempFile
    echo '"$msg_Edit" "$msg_Edit_chandev" \'  >> $TempFile
  fi
  echo '" " " " \' >> $TempFile

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

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

    local help=""
    local summary=""
    tdir=`echo $dir | tr / _`

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

    summary=$(echo $summary | sed -e 's/^-\(.*\)/\1/')
    echo "\"$dir\"" \\ >> $TempFile
    echo "\"$summary."\" \\ >> $TempFile
  done
  echo "" >> $TempFile
  
  while true; do
    local directory status defaultitem

    if [ -n "$One_Restricted_Section" ]; then
      directory=$One_Restricted_Section
    else
      sed -e "s#@DEFAULTITEM@#$defaultitem#" $TempFile > $TempFile6
      directory="`. $TempFile6`"
      if [ $? -ne 0 ]; then return 1; fi
    fi

    case "$directory" in
    " ")
      ;;
    "$msg_Exit")
      return 0;;
    "$msg_Edit")
      if [ -z "$EDITOR" ]; then
               EDITOR=/usr/bin/editor
      fi
      $EDITOR $MODUTILSDIR/0chandev.chandev
      ;;
    *)
      defaultitem="$directory"
      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 $*

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

if [ -d "$Target/$ModuleHelpDir" ]; then
    ModuleHelpDir="$Target/$ModuleHelpDir"
fi

. "$ModuleHelpDir/params"
. "$ModuleHelpDir/util"
. "$ModuleHelpDir/dialog"

trap "rm -f $TempFile $TempFile1 $TempFile2 $TempFile3 $TempFile4 $TempFile5 $TempFile6" 0 9 15

source_eval

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

modul_suffix
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 $(get_modules_dirs $Dir_Prefix); do
    if [ -d $Dir_Prefix/$i -a \
      -f  $(first $Dir_Prefix/$i/$modend) ]; 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

exit 0
