+#!/bin/sh
+
+#------------------------------------------#
+# component db upgrade ver.1 (4.0 -> 5.5) #
+#------------------------------------------#
+
+# Macro
+PATH=/bin:/usr/bin:/sbin:/usr/sbin
+
+PATH_DB="$1"
+PATH_NEW_DB="${PATH_DB}.new"
+PATH_BACKUP_DB="${PATH_DB}.bak"
+
+Init() {
+ echo "### Initialize"
+ cp ${PATH_DB} ${PATH_BACKUP_DB}
+ cp ${PATH_DB}-journal ${PATH_BACKUP_DB}-journal
+
+ if [ -e "${PATH_NEW_DB}" ]; then
+ rm ${PATH_NEW_DB}
+ fi
+
+ if [ -e "${PATH_NEW_DB}-journal" ]; then
+ rm ${PATH_NEW_DB}-journal
+ fi
+}
+
+# Create new DB
+CreateNewDB() {
+sqlite3 ${PATH_NEW_DB} << EOF
+PRAGMA journal_mode = PERSIST;
+PRAGMA user_version = @COMPONENT_DB_VERSION@;
+
+CREATE TABLE IF NOT EXISTS component_info (
+ package TEXT NOT NULL,
+ app_id TEXT NOT NULL,
+ component_id TEXT NOT NULL,
+ component_type TEXT NOT NULL,
+ component_launch_mode TEXT NOT NULL,
+ component_main TEXT NOT NULL,
+ component_icon_display NOT NULL,
+ component_taskmanage NOT NULL,
+ PRIMARY KEY (component_id)
+);
+
+CREATE TABLE IF NOT EXISTS component_localized_info (
+ component_id TEXT NOT NULL,
+ component_locale TEXT NOT NULL DEFAULT 'No Locale',
+ component_label TEXT,
+ component_icon TEXT,
+ PRIMARY KEY (component_id, component_locale)
+ FOREIGN KEY (component_id)
+ REFERENCES component_info(component_id) ON DELETE CASCADE
+);
+
+EOF
+}
+
+# Insert old data into new DB
+InsertComponentInfo() {
+ echo " - Insert component_info"
+ TABLE=`sqlite3 ${PATH_DB} 'SELECT package, app_id, component_id, component_type, component_launch_mode, component_main, component_icon_display, component_taskmanage FROM component_info;'`
+ for ROW in ${TABLE}; do
+ IFS='|' read -ra VALUE <<< "${ROW}";
+ QUERY="INSERT INTO component_info(package, app_id, component_id, component_type, component_launch_mode, component_main, component_icon_display, component_taskmanage) VALUES('${VALUE[0]}', '${VALUE[1]}', '${VALUE[2]}', '${VALUE[3]}', '${VALUE[4]}', '${VALUE[5]}', '${VALUE[6]}', '${VALUE[7]}');"
+ echo " + Query: ${QUERY}"
+ sqlite3 ${PATH_NEW_DB} "${QUERY}"
+ done
+}
+
+InsertComponentLocalizedInfo() {
+ echo " - Insert component_localized_info"
+ TABLE=`sqlite3 ${PATH_DB} 'SELECT component_id, component_locale, component_label, component_icon FROM component_localized_info;'`
+ for ROW in ${TABLE}; do
+ IFS='|' read -ra VALUE <<< "${ROW}";
+ QUERY="INSERT INTO component_localized_info(component_id, component_locale, component_label, component_icon) VALUES('${VALUE[0]}', '${VALUE[1]}', '${VALUE[2]}', '${VALUE[3]}');"
+ echo " + Query: ${QUERY}"
+ sqlite3 ${PATH_NEW_DB} "${QUERY}"
+ done
+}
+
+Upgrade() {
+ echo "### Upgrade"
+ CreateNewDB
+
+ InsertComponentInfo
+ InsertComponentLocalizedInfo
+
+ cp ${PATH_NEW_DB} ${PATH_DB}
+ cp ${PATH_NEW_DB}-journal ${PATH_DB}-journal
+
+ chown :system_share ${PATH_DB}
+ chown :system_share ${PATH_DB}-journal
+ chsmack -a User::Home ${PATH_DB}
+ chsmack -a User::Home ${PATH_DB}-journal
+ chmod 664 ${PATH_DB}
+ chmod 664 ${PATH_DB}-journal
+}
+
+Fini() {
+ echo "### Finish"
+ rm ${PATH_NEW_DB}
+ rm ${PATH_NEW_DB}-journal
+
+ rm ${PATH_BACKUP_DB}
+ rm ${PATH_BACKUP_DB}-journal
+}
+
+echo "#### 1. Upgrade component db ####"
+Init
+Upgrade
+Fini
+echo "#### 1. Upgrade component db [Done] ####"