#!/bin/sh

# NOTE: This script is called by /usr/bin/datamigration during OTA and is called directly by FirstUse after 
# it completes

#########
# SETUP #
#########

#############
# MIGRATION #
#############

echo "Checking migration service health"
HEALTH_OUTPUT=`luna-send -n 1 palm://com.palm.service.migration/healthcheck '{}'`
echo $HEALTH_OUTPUT

while [ -e /media/internal/.save_for_software_update/pauseBeforeMigration ]; do
  echo "Waiting until /media/internal/.save_for_software_update/pauseBeforeMigration is removed"
  sleep 30
done

echo "Running migration service"
OUTPUT=`luna-send -n 1 palm://com.palm.service.migration/migrate '{}'`
echo $OUTPUT

# Set the return value to 0 if the migration succeeded, 1 if it failed
echo $OUTPUT | grep \"returnValue\":true > /dev/null
