#!/bin/bash

if [ $USER = "root" ]; then
  PERSO='~'
else
  PERSO=$HOME
fi

DOCUMENTS="$PERSO/Documents"
if [ ! -d $DOCUMENTS ];then
  mkdir -p $DOCUMENTS
fi
BOOKMARKS="$PERSO/Documents/bookmarks.html"

rm -f $PERSO/Documents/bookmarks-*.html

URL="file:///usr/share/doc/HTML/index.html"

PROFILE=$PERSO/.mozilla/firefox

if [ ! -e $PROFILE ]; then
 mkdir -p $PROFILE
 pushd $PROFILE >/dev/null
 MOZDIR=`mktemp -d XXXXXXXX`
 cat << EOF > profiles.ini
[General]
StartWithLastProfile=1

[Profile0]
Name=default
IsRelative=1
Path=$MOZDIR
Default=1
EOF
touch $PROFILE/$MOZDIR/prefs.js
touch $PROFILE/$MOZDIR/localstore.rdf
fi 

CNF=$PROFILE/*/prefs.js

mval() {
perl -pi -e "s|^.*$1\".*\n||;" $CNF
echo 'user_pref("'$1'", '$2');' >> $CNF
}
mstr() {
perl -pi -e "s|^.*$1\".*\n||;" $CNF
echo 'user_pref("'$1'", "'$2'");' >> $CNF
}

#Page par dfaut
mstr browser.startup.homepage $URL
mstr browser.throbber.url $URL
mstr startup.homepage_override_url $URL

#Sauvegarde par defaut dans /Documents, effacer la liste
#des telechargements au depart de firefox
mstr browser.bookmarks.file $BOOKMARKS
mstr browser.download.lastDir $DOCUMENTS
mval browser.download.manager.retention 1

#Ne pas verifier pour les mises a jour, c'est le sysadmin
#qui s'en charge. 
mval browser.shell.checkDefaultBrowser false
mval app.update.enabled false
mstr browser.search.selectedEngine Google
mval browser.search.update false
mval extensions.update.enabled false

#Les liens mailto: sont lancs par Thunderbird
mstr network.protocol-handler.app.mailto moztbremote

#Afficher les erreurs du serveur web au lieu
#d'avoir une page generee par firefox
mval browser.xul.error_pages.enabled false

#Desactiver le cache (nous sommes derriere un proxy)
mval browser.cache.disk.capacity 0
mval browser.cache.disk.enable false
mval browser.cache.memory.enable false

#Accelerer l'affichage des pages, permettre 8 connections simultanees
#et activer le pipelining
mval nglayout.initialpaint.delay 0
mval network.http.max-persistent-connections-per-server 8
mval network.http.pipelining true
mval network.http.pipelining.maxrequests 8
mval network.http.proxy.pipelining true

#Creer les Marque-Pages
if [ ! -e $BOOKMARKS ]; then
cat << EOF > $BOOKMARKS
<TITLE>Marque-Pages</TITLE><H1>Marque-Pages</H1>
<H3 PERSONAL_TOOLBAR_FOLDER="true">Barre Personnelle</H3>
<A HREF="http://www.google.ca/">Google</A>
<A HREF="http://www.cyberpresse.ca/">CyberPresse</A>
<A HREF="http://www.revolutionlinux.com/">Revolution Linux</A>
<A HREF="http://www.gulus.org/">GULUS</A>
<A HREF="http://www.linuxeduquebec.org/">Linuxedu-Quebec</A>
EOF
fi

#Elargir la fenetre de telechargement par defaut en 650x300
RDF=$PROFILE/*/localstore.rdf
if [ ! -s $RDF ]; then
cat << EOF > $RDF
<?xml version="1.0"?>
<RDF:RDF xmlns:NC="http://home.netscape.com/NC-rdf#"
         xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <RDF:Description RDF:about="chrome://global/content/filepicker.xul">
    <NC:persist RDF:resource="chrome://global/content/filepicker.xul#main-window"/>
  </RDF:Description>
  <RDF:Description RDF:about="chrome://browser/content/browser.xul">
    <NC:persist RDF:resource="chrome://browser/content/browser.xul#main-window"/>
    <NC:persist RDF:resource="chrome://browser/content/browser.xul#sidebar-box"/>
    <NC:persist RDF:resource="chrome://browser/content/browser.xul#sidebar-title"/>
  </RDF:Description>
  <RDF:Description RDF:about="chrome://global/content/filepicker.xul#main-window"
                   screenX="100"
                   screenY="100"
                   width="650"
                   height="300" />
</RDF:RDF>
EOF
fi

#POP=$PROFILE/$MOZDIR/hostperm.1
#if [ ! -s $POP ]; then
#cat << EOF > $POP
## Permission File
## This is a generated file! Do not edit.
#
#host    popup   1       web.taptouche.com
#
#EOF
#fi
