#!/bin/bash

. /usr/share/debconf/confmodule

db_get debian-installer/locale || exit
LANG="$RET"

LOCALE=`echo $LANG | tr "[:upper:]" "[:lower:]" | sed "s/\.utf-8//g"`
LANGUAGE=`echo $LOCALE | sed "s/_.*//g"`
COUNTRY=`echo $LOCALE | sed "s/.*_//g"`

# Get the right search URL to use for this locale.
case "$LOCALE" in
 'de_de')
   SEARCHURL='http:\/\/de.search.yahoo.com\/search';
   ;;
 'en_gb')
   SEARCHURL='http:\/\/uk.search.yahoo.com\/search';
   ;;
 'es_ar')
# default spanish-speaking url
   SEARCHURL='http:\/\/espanol.search.yahoo.com\/search';
   ;;
 'es_bo')
# default spanish-speaking url
   SEARCHURL='http:\/\/espanol.search.yahoo.com\/search';
   ;;
 'es_cr')
# default spanish-speaking url
   SEARCHURL='http:\/\/espanol.search.yahoo.com\/search';
   ;;
 'es_do')
# default spanish-speaking url
   SEARCHURL='http:\/\/espanol.search.yahoo.com\/search';
   ;;
 'es_ec')
# default spanish-speaking url
   SEARCHURL='http:\/\/espanol.search.yahoo.com\/search';
   ;;
 'es_sv')
# default spanish-speaking url
   SEARCHURL='http:\/\/espanol.search.yahoo.com\/search';
   ;;
 'es_gt')
# default spanish-speaking url
   SEARCHURL='http:\/\/espanol.search.yahoo.com\/search';
   ;;
 'es_hn')
# default spanish-speaking url
   SEARCHURL='http:\/\/espanol.search.yahoo.com\/search';
   ;;
 'es_ni')
# default spanish-speaking url
   SEARCHURL='http:\/\/espanol.search.yahoo.com\/search';
   ;;
 'es_pa')
# default spanish-speaking url
   SEARCHURL='http:\/\/espanol.search.yahoo.com\/search';
   ;;
 'es_py')
# default spanish-speaking url
   SEARCHURL='http:\/\/espanol.search.yahoo.com\/search';
   ;;
 'es_pr')
# default spanish-speaking url
   SEARCHURL='http:\/\/espanol.search.yahoo.com\/search';
   ;;
 'es_uy')
# default spanish-speaking url
   SEARCHURL='http:\/\/espanol.search.yahoo.com\/search';
   ;;
 'es_es')
   SEARCHURL='http:\/\/es.search.yahoo.com\/search';
   ;;
 'es_cl')
   SEARCHURL='http:\/\/cl.search.yahoo.com\/search';
   ;;
 'es_co')
   SEARCHURL='http:\/\/co.search.yahoo.com\/search';
   ;;
 'es_mx')
   SEARCHURL='http:\/\/mx.search.yahoo.com\/search';
   ;;
 'es_pe')
   SEARCHURL='http:\/\/pe.search.yahoo.com\/search';
   ;;
 'es_ve')
   SEARCHURL='http:\/\/ve.search.yahoo.com\/search';
   ;;
 'fr_fr')
   SEARCHURL='http:\/\/fr.search.yahoo.com\/search';
   ;;
 'fr_ca')
   SEARCHURL='http:\/\/ca.search.yahoo.com\/search';
   ;;
 'ja_jp')
   SEARCHURL='http:\/\/search.yahoo.co.jp\/search';
   ;;
 'ko_ko')
   SEARCHURL='http:\/\/kr.search.yahoo.com\/search';
   ;;
 'ko_kp')
   SEARCHURL='http:\/\/kr.search.yahoo.com\/search';
   ;;
 'ko_kr')
   SEARCHURL='http:\/\/kr.search.yahoo.com\/search';
   ;;
 'it')
   SEARCHURL='http:\/\/it.search.yahoo.com\/search';
   ;;
 'zh_zh')
   SEARCHURL='http:\/\/one.cn.yahoo.com\/s';
   ;;
 'zh_cn')
   SEARCHURL='http:\/\/one.cn.yahoo.com\/s';
   ;;
 'zh_hk')
   SEARCHURL='http:\/\/hk.search.yahoo.com\/search';
   ;;
 'zh_tw')
   SEARCHURL='http:\/\/tw.search.yahoo.com\/search';
   ;;
 *)
   SEARCHURL='http:\/\/search.yahoo.com\/search';
   ;;
esac

# Replace the default search engine URL
sed -i "s/template=\"http:\/\/search\.yahoo\.com\/search\"/template=\"$SEARCHURL\"/" /usr/lib/firefox-addons/searchplugins/yahoo.xml

# A few Yahoo intl codes that differ from the standard ones:
if [[ $COUNTRY == 'gb' ]]
then
   COUNTRY='uk'
fi
if [[ $LANGUAGE == 'ko' ]]
then
   LANGUAGE='kr'
fi
if [[ $LANGUAGE == 'ja' ]]
then
   LANGUAGE='jp'
fi

# Toolbar-supported codes are accepted, otherwise default to UK
if [[ $COUNTRY != 'us' && $COUNTRY != 'uk' && $COUNTRY != 'de' && $COUNTRY != 'fr' && $COUNTRY != 'hk' && $COUNTRY != 'jp' && $COUNTRY != 'kr' && $COUNTRY != 'tw' ]]
then
   COUNTRY='uk'
   LANGUAGE='en'
fi

db_get passwd/make-user
if [ "$RET" = true ]; then
  db_get passwd/username
  USER="$RET"
  
  # Create firefox profile, as it does not yet exist
  FIREFOXDIR="/home/$USER/.mozilla/firefox"
  
  sudo -u "$USER" mkdir -p $FIREFOXDIR
  
  # Generate a random 8-char profile code (doesn't use full range of
  # alphabet, but not a big deal).
  CODE=`date | md5sum`
  CODE=${CODE:0:8}
  
  # Write .ini file
  sudo -u "$USER" echo "[General]
StartWithLastProfile=1

[Profile0]
Name=default
IsRelative=1
Path=${CODE}.default" > "$FIREFOXDIR/profiles.ini"
  
  PROFILEDIR="$FIREFOXDIR/${CODE}.default"
  sudo -u "$USER" mkdir $PROFILEDIR
  
  # Now write actual prefs
  PREFSFILE="$PROFILEDIR/prefs.js"
  sudo -u "$USER" echo "// Yahoo! customizations:
user_pref(\"yahoo.installer.dc\", \"v1_dellia\");
user_pref(\"yahoo.installer.sc\", \"dellia\");
user_pref(\"yahoo.installer.pc\", \"dellia\");
user_pref(\"yahoo.installer.language\", \"$LANGUAGE\");
user_pref(\"yahoo.installer.country\", \"$COUNTRY\");" >> $PREFSFILE
fi

