# we have to have the tarball before we can proceed

download_module glibc &&

LOCALEDATA_DIR="/tmp/localedata"
ORIGINAL_SUPPORTED="$LOCALEDATA_DIR/SUPPORTED"
COPIED_SUPPORTED="/tmp/supported-locales"
FILELIST_FILE="$LOCALEDATA_DIR-files"

unpack_localedata() {
  # Write the path of the localedata archive path into a file
  echo "${SOURCE/.tar.bz2/}/localedata" > $FILELIST_FILE &&

  message "Extracting locale data .. this might take a second .."

  # Extract the localedata directory from the source tarball
  tar -C /tmp               \
      -T $FILELIST_FILE     \
      --strip-components=1  \
      -xjf $SOURCE_CACHE/$SOURCE
}

remove_temporary_files() {
  rm -rf $LOCALEDATA_DIR $FILELIST_FILE $COPIED_SUPPORTED
}

make_locales_checklist() {
  # Copy and modify the supported locales file so we can treat it like 
  # a normal bash script
  cp $ORIGINAL_SUPPORTED $COPIED_SUPPORTED &&
  sed -i -e 's/SUPPORTED-LOCALES=/SUPPORTED_LOCALES="/g' $COPIED_SUPPORTED &&
  echo '"' >> $COPIED_SUPPORTED &&
  
  # Execute the modified supported locales file to get a list of 
  # supported variables in the $SUPPORTED_LOCALES variable
  . $COPIED_SUPPORTED &&
  
  # For each locale, extract name, title and charmap information
  for locale in $SUPPORTED_LOCALES; do
    locale_base=$(echo $locale | sed -e 's:\/.*$::g;s:\..*$::g')

    # This is a bit tricky and slow but it does the job
    if [[ -e $LOCALEDATA_DIR/locales/$locale_base ]]; then
      title=$(cat $LOCALEDATA_DIR/locales/$locale_base | grep ^title)
      title=$(echo $title | awk -F"\"" '{ $1 = ""; print }')
      title=$(echo $title | sed -e 's/^[ \t]*//;s/[ \t]*$//')
    else
      title=""
    fi

    # Echo locale/charmap, title and status to be passed to the 
    # dialog checklist
    echo "\"$locale\" \"$title\" \"off\" "
  done
}

select_locales() {
  BACKTITLE="Glibc Configuration"
      TITLE="Locale Selection"
       HELP="Translated messages are automatically installed, but the locale database that controls other behavior is not.  Please select desired locale or locales.  If none are selected then all will be installed."
  make_locales_checklist | xargs  \
  dialog --backtitle "$BACKTITLE" \
         --title     "$TITLE"     \
         --stdout                 \
         --separate-output        \
         --checklist "$HELP"      \
         0 0 0
}

if ! grep -q CONFIGURED $MODULE_CONFIG; then
  if grep -q "GLIBC_LOCALES=" $MODULE_CONFIG; then
    message "Selecting locales is optional"
  fi

  # Don't change default to 'y', it complicate ISO builds and leave users without any locales if left unattended
  if query "Would you like to select locales? (if not selected, all locales will be installed)" n; then
    unpack_localedata

    GLIBC_LOCALES=`select_locales`
    TEMP=`grep -v "GLIBC_LOCALES="             $MODULE_CONFIG`
    echo "$TEMP"                             > $MODULE_CONFIG
    echo "GLIBC_LOCALES=\"$GLIBC_LOCALES\"" >> $MODULE_CONFIG

    remove_temporary_files
  fi

  echo "OPTS=\"\$OPTS $OPTS\"" >> $MODULE_CONFIG
  echo "CONFIGURED=y"          >> $MODULE_CONFIG
fi
