#!/usr/bin/env python
# Copyright (C) 2010, Paraguay Educa <tecnologia@paraguayeduca.org>
#
# 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 3 of the License, 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os
import sys
import shutil
import logging
import subprocess

from sugar import env
#from sugar.datastore import datastore

backup_identifier = sys.argv[2]
volume_path = sys.argv[1]

if len(sys.argv) != 3:
    print 'Usage: %s <volume_path> <backup_identifier>' % sys.argv[0]
    exit(1)

logging.debug('Restore started')

journal_path = os.path.join(env.get_profile_path(), 'datastore')
backup_path = os.path.join(volume_path, 'backup', backup_identifier, 'datastore.tar.gz')

if not os.path.exists(backup_path):
    logging.error('Could not find backup file %s', backup_path)
    exit(1)

#datastore.freeze()
subprocess.call(['pkill', '-9', '-f', 'python.*datastore-service'])

result = 0
try:
    if os.path.exists(journal_path):
        shutil.rmtree(journal_path)

    subprocess.check_call(['tar', '-C', env.get_profile_path(), '-xzf', backup_path])

except Exception, e:
    logging.error('Restore failed: %s', str(e))
    result = 1

try:
  shutil.rmtree(os.path.join(journal_path, 'index'))
  os.remove(os.path.join(journal_path, 'index_updated'))
  os.remove(os.path.join(journal_path, 'version'))
except:
  logging.debug('Restore has no index files')

#datastore.thaw()

logging.debug('Restore finished')
exit(result)
