#!/bin/bash

brand=ClubDeJeux
brandlc=clubdejeux
branduc=CLUBDEJEUX
locale=fr
download_url=http://downloads.clubdejeux.com/

if [ `whoami` == "root" ]
then
    echo "running as root user"
    echo "changing session to `ls /home`"
    su `ls /home` -c "$0"
    exit
fi

rm -rf $HOME/$brand
mkdir $HOME/$brand
cd $HOME/$brand

echo "install folder $HOME/$brand"

wget $download_url/console-$locale/index.mu .

echo "\n" >> index.mu
filename=index.mu

while read line
do
    name=$line
    file=`echo $name|cut -d'=' -f 1`
    echo $file
    if [ ${file: -4} != ".exe" ]
    then
	wget -x -nH --cut-dirs=2 $download_url/console-$locale/$file
        if [ ${file: -4} == ".bin" ]
        then
            chmod +x $file
        fi
    fi
done < $filename



rm index.mu

rm /usr/share/applications/$brandlc.desktop
sudo su -c "cat <<EOT >> /usr/share/applications/$brandlc.desktop
[Desktop Entry]
Type=Application
MimeType=application/$brandlc;x-scheme-handler/$brandlc;
Name=$brand
Terminal=false
Path=`pwd`
Exec=`pwd`/starter.bin %U
Icon=`pwd`/splash.png
StartupNotify=true
StartupWMClass=$branduc
EOT"

sudo chmod +x /usr/share/applications/$brandlc.desktop
sudo chown `ls /home` /usr/share/applications/$brandlc.desktop

cp /usr/share/applications/$brandlc.desktop $HOME/Desktop/.

sudo update-desktop-database

if [ `uname -i` == "x86_64" ]
then
    echo "Unsupported architecture: `uname -i`"
    sudo apt-get install libc6:i386
    sudo apt-get install libncurses5:i386
    sudo apt-get install libstdc++6:i386
    sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386
    sudo apt-get install libxtst6:i386
    sudo apt-get install libXrender:i386
    sudo apt-get install libxrender1:i386
    sudo apt-get install libxrender6:i386
    sudo apt-get install libxi6:i386
    sudo apt-get install libgtk2.0-0:i386
fi

./starter.bin

