#!/bin/sh
################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2014 Stephan Raue (stephan@openelec.tv)
#
#  OpenELEC 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 of the License, or
#  (at your option) any later version.
#
#  OpenELEC 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.  If not, see <http://www.gnu.org/licenses/>.
################################################################################

BACKUP_FILE=`ls -1 /storage/.restore/??????????????.tar 2>/dev/null | tail -1`

if [ -e "$BACKUP_FILE" ] ; then
  echo -en "please wait.. checking backup file: "
  tar tf $BACKUP_FILE &>/dev/null
  ret=$?
  if [ $ret -eq 0 ] ; then
    echo OK
    echo -en "restoring.. this may take long time to complete, please wait.. "
    rm -rf /storage/.xbmc &>/dev/null
    rm -rf /storage/.cache &>/dev/null
    rm -rf /storage/.config &>/dev/null
    tar xf $BACKUP_FILE -C / &>/dev/null
    rm -f $BACKUP_FILE &>/dev/null
    echo done
    echo "all good. normal startup in 5s..."
    sleep 5
  else
    echo FAILED
    echo "normal startup in 30s..."
    rm -f $BACKUP_FILE &>/dev/null
    sleep 30
  fi
fi

