#!/bin/sh

################################################################################
#      Copyright (C) 2009-2010 OpenELEC.tv
#      http://www.openelec.tv
#
#  This Program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, or (at your option)
#  any later version.
#
#  This Program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.tv; see the file COPYING.  If not, write to
#  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

# setup XBMC
#
# runlevels: openelec

. /etc/profile

progress "setup XBMC"

#
# clean temp dir
#
  rm -rf $HOME/.xbmc/temp/*

#
# add some default settings
#
  mkdir -p $HOME/.xbmc/userdata

# use dds fanarts by default
  if [ ! -f $HOME/.xbmc/userdata/advancedsettings.xml ] ; then
    cat > $HOME/.xbmc/userdata/advancedsettings.xml << EOF
<advancedsettings>
  <useddsfanart>true</useddsfanart>
  <cputempcommand>cputemp</cputempcommand>
  <gputempcommand>gputemp</gputempcommand>
  <samba>
    <clienttimeout>30</clienttimeout>
  </samba>
</advancedsettings>
EOF
  fi

#
# setup directories for XBMC sources
#

  [ ! -d "$HOME/music" ] && mkdir -p $HOME/music
  [ ! -d "$HOME/pictures" ] && mkdir -p $HOME/pictures
  [ ! -d "$HOME/tvshows" ] && mkdir -p $HOME/tvshows
  [ ! -d "$HOME/videos" ] && mkdir -p $HOME/videos

  mkdir -p $HOME/.xbmc/userdata

  if [ ! -f $HOME/.xbmc/userdata/sources.xml ] ; then
    cat > $HOME/.xbmc/userdata/sources.xml << EOF
<sources>
    <video>
        <default pathversion="1"></default>
        <source>
            <name>Videos</name>
            <path pathversion="1">$HOME/videos/</path>
        </source>
        <source>
            <name>TV Shows</name>
            <path pathversion="1">$HOME/tvshows/</path>
        </source>
    </video>
    <music>
        <default pathversion="1"></default>
        <source>
            <name>Music</name>
            <path pathversion="1">$HOME/music/</path>
        </source>
    </music>
    <pictures>
        <default pathversion="1"></default>
        <source>
            <name>Pictures</name>
            <path pathversion="1">$HOME/pictures/</path>
        </source>
    </pictures>
</sources>
EOF
  fi

#
# common setup guisettings
#

  mkdir -p $HOME/.xbmc/userdata

  [ ! -d "$HOME/screenshots" ] && mkdir -p $HOME/screenshots

  if [ ! -f $HOME/.xbmc/userdata/guisettings.xml ] ; then
    echo "<settings>" > $HOME/.xbmc/userdata/guisettings.xml

    cat >> $HOME/.xbmc/userdata/guisettings.xml << EOF
    <debug>
        <screenshotpath pathversion="1">$HOME/screenshots/</screenshotpath>
    </debug>
EOF

#
# common setup for amd and nvidia graphic
#

  # Always sync to vblank
    if [ "$GPUTYPE" = "NVIDIA" -o "$GPUTYPE" = "RADEON" ] ; then
      cat >> $HOME/.xbmc/userdata/guisettings.xml << EOF
    <videoscreen>
        <vsync>2</vsync>
    </videoscreen>
EOF
    fi
    echo "</settings>" >> $HOME/.xbmc/userdata/guisettings.xml
  fi
