################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2012 Stephan Raue (stephan@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, 51 Franklin Street, Suite 500, Boston, MA 02110, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

# setup XBMC
#
# runlevels: openelec

progress "setup XBMC"

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

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

#
# 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
    if [ -f /usr/share/xbmc/config/sources.xml ]; then
      # include project specific sources
      cp /usr/share/xbmc/config/sources.xml $HOME/.xbmc/userdata
    else
      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
  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

  #
  # include project specific options
  #

    if [ -f /usr/share/xbmc/config/guisettings.xml ]; then
      cat /usr/share/xbmc/config/guisettings.xml >> $HOME/.xbmc/userdata/guisettings.xml
    fi

  #
  # common setup for amd and nvidia graphic
  #

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