#! /bin/sh

# Auteur : Pierre Brochard (pierre.brochard.1982@m4x.org)
# Site : http(s)://logicielnarcisse.fr

if [ "$#" -ne "1" ] ;then
        echo "Usage : $0 Chemin de firefox"
        echo "Exemple : $0 /usr/bin/firefox"
        exit 1
fi

NEWFFHOME=`dirname "$1"`
cd
cd .mozilla/firefox
cp profiles.ini profiles.ini.s$$

$NEWFFHOME/firefox

NPROFILO=`ls -d *.default|head -1`
if [ "$NPROFILO" = '' ] ;then
    NPROFILO=`ls -d *.default-[0-9]*|head -1`
fi

NPROFILN=`ls -d *.default-release|head -1`
if [ "$NPROFILN" = '' ] ;then
    NPROFILN=`ls -d *.default-[a-z]*|head -1`
fi

if [ "$NPROFILN" = '' ] ;then
    echo "Nouveau profil non existant."
    exit 0
fi

echo "Profil old $NPROFILO trouve"
echo "Profil new $NPROFILN trouve"

sed "/^\[Profile/,/^Path=/ {
/^\[Profile/{h;d}
/^Path=/!{H;d}
/^Path=$NPROFILN/d
/^Path=/{H;g}
}" profiles.ini |\
sed "s/^Default=$NPROFILN/Default=$NPROFILO/" > profiles.ini.new

mv profiles.ini.new profiles.ini
rm -rf $NPROFILN
rm -f profiles.ini.s$$
