<?
/*
FOR VERSION: 4.0.x

DESCRIPTION:
When converting a MySQL database from Windows to Linux/Unix the Windows server will often export
the database table names in all lowercase. HelpSpot on Linux/Unix requires mixed case table names.
This script will convert the lowercase names into proper mixed case names.

USAGE:
-Place this file in the same directory as config.php.
-Visit the script in your browser.
-Delete the script
*/


//Include DB variables
require_once 'config.php';

//Connect
$conn = mysql_connect(cDBHOSTNAME, cDBUSERNAME, cDBPASSWORD);
mysql_select_db(cDBNAME);


##########################################
# Convert table names
##########################################
mysql_query("RENAME TABLE `hs_address_book` TO `HS_Address_Book`");
mysql_query("RENAME TABLE `hs_assignment_chain` TO `HS_Assignment_Chain`");
mysql_query("RENAME TABLE `hs_automation_rules` TO `HS_Automation_Rules`");
mysql_query("RENAME TABLE `hs_bayesian_corpus` TO `HS_Bayesian_Corpus`");
mysql_query("RENAME TABLE `hs_bayesian_msgcounts` TO `HS_Bayesian_MsgCounts`");
mysql_query("RENAME TABLE `hs_category` TO `HS_Category`");
mysql_query("RENAME TABLE `hs_category_reportingtags` TO `HS_Category_ReportingTags`");
mysql_query("RENAME TABLE `hs_customfields` TO `HS_CustomFields`");
mysql_query("RENAME TABLE `hs_documents` TO `HS_Documents`");
mysql_query("RENAME TABLE `hs_errors` TO `HS_Errors`");
mysql_query("RENAME TABLE `hs_filter` TO `HS_Filter`");
mysql_query("RENAME TABLE `hs_filter_people` TO `HS_Filter_People`");
mysql_query("RENAME TABLE `hs_forums` TO `HS_Forums`");
mysql_query("RENAME TABLE `hs_forums_knownusers` TO `HS_Forums_KnownUsers`");
mysql_query("RENAME TABLE `hs_forums_posts` TO `HS_Forums_Posts`");
mysql_query("RENAME TABLE `hs_forums_topics` TO `HS_Forums_Topics`");
mysql_query("RENAME TABLE `hs_kb_books` TO `HS_KB_Books`");
mysql_query("RENAME TABLE `hs_kb_chapters` TO `HS_KB_Chapters`");
mysql_query("RENAME TABLE `hs_kb_documents` TO `HS_KB_Documents`");
mysql_query("RENAME TABLE `hs_kb_pages` TO `HS_KB_Pages`");
mysql_query("RENAME TABLE `hs_kb_relatedpages` TO `HS_KB_RelatedPages`");
mysql_query("RENAME TABLE `hs_login_attempts` TO `HS_Login_Attempts`");
mysql_query("RENAME TABLE `hs_mail_rules` TO `HS_Mail_Rules`");
mysql_query("RENAME TABLE `hs_mailboxes` TO `HS_Mailboxes`");
mysql_query("RENAME TABLE `hs_mailboxes_stuckemails` TO `HS_Mailboxes_StuckEmails`");
mysql_query("RENAME TABLE `hs_multi_portal` TO `HS_Multi_Portal`");
mysql_query("RENAME TABLE `hs_person` TO `HS_Person`");
mysql_query("RENAME TABLE `hs_person_photos` TO `HS_Person_Photos`");
mysql_query("RENAME TABLE `hs_person_status` TO `HS_Person_Status`");
mysql_query("RENAME TABLE `hs_portal_bayesian_corpus` TO `HS_Portal_Bayesian_Corpus`");
mysql_query("RENAME TABLE `hs_portal_bayesian_msgcounts` TO `HS_Portal_Bayesian_MsgCounts`");
mysql_query("RENAME TABLE `hs_portal_login` TO `HS_Portal_Login`");
mysql_query("RENAME TABLE `hs_reminder` TO `HS_Reminder`");
mysql_query("RENAME TABLE `hs_reminder_person` TO `HS_Reminder_Person`");
mysql_query("RENAME TABLE `hs_request` TO `HS_Request`");
mysql_query("RENAME TABLE `hs_request_history` TO `HS_Request_History`");
mysql_query("RENAME TABLE `hs_request_merged` TO `HS_Request_Merged`");
mysql_query("RENAME TABLE `hs_request_note_drafts` TO `HS_Request_Note_Drafts`");
mysql_query("RENAME TABLE `hs_request_pushed` TO `HS_Request_Pushed`");
mysql_query("RENAME TABLE `hs_request_reportingtags` TO `HS_Request_ReportingTags`");
mysql_query("RENAME TABLE `hs_reset_password` TO `HS_Reset_Password`");
mysql_query("RENAME TABLE `hs_responses` TO `HS_Responses`");
mysql_query("RENAME TABLE `hs_response_people` TO `HS_Response_People`");
mysql_query("RENAME TABLE `hs_saved_reports` TO `HS_Saved_Reports`");
mysql_query("RENAME TABLE `hs_search_queries` TO `HS_Search_Queries`");
mysql_query("RENAME TABLE `hs_sessions2` TO `HS_Sessions2`");
mysql_query("RENAME TABLE `hs_settings` TO `HS_Settings`");
mysql_query("RENAME TABLE `hs_stats_responses` TO `HS_Stats_Responses`");
mysql_query("RENAME TABLE `hs_subscriptions` TO `HS_Subscriptions`");
mysql_query("RENAME TABLE `hs_tags` TO `HS_Tags`");
mysql_query("RENAME TABLE `hs_tags_map` TO `HS_Tags_Map`");
mysql_query("RENAME TABLE `hs_time_tracker` TO `HS_Time_Tracker`");
mysql_query("RENAME TABLE `hs_triggers` TO `HS_Triggers`");
mysql_query("RENAME TABLE `hs_lusms` TO `HS_luSMS`");
mysql_query("RENAME TABLE `hs_lustatus` TO `HS_luStatus`");


##########################################
# New Tables in v4
##########################################

# HS_Response_Group
#
mysql_query("SELECT Count(*)
INTO @exists
FROM information_schema.tables
WHERE table_schema = [".cDBNAME."]
    AND table_type = 'BASE TABLE'
    AND table_name = 'hs_response_group';

SET @query = If(@exists>0,
    'RENAME TABLE hs_response_group TO HS_Response_Group',
    'SELECT \'nothing to rename\' status');

PREPARE stmt FROM @query;

EXECUTE stmt;");


# HS_Permission_Groups
#
mysql_query("SELECT Count(*)
INTO @exists
FROM information_schema.tables
WHERE table_schema = [".cDBNAME."]
    AND table_type = 'BASE TABLE'
    AND table_name = 'hs_permission_groups';

SET @query = If(@exists>0,
    'RENAME TABLE hs_permission_groups TO HS_Permission_Groups',
    'SELECT \'nothing to rename\' status');

PREPARE stmt FROM @query;

EXECUTE stmt;");


# HS_Filter_Group
#
mysql_query("SELECT Count(*)
INTO @exists
FROM information_schema.tables
WHERE table_schema = [".cDBNAME."]
    AND table_type = 'BASE TABLE'
    AND table_name = 'hs_filter_group';

SET @query = If(@exists>0,
    'RENAME TABLE hs_filter_group TO HS_Filter_Group',
    'SELECT \'nothing to rename\' status');

PREPARE stmt FROM @query;

EXECUTE stmt;");


# HS_Migrations
#
mysql_query("SELECT Count(*)
INTO @exists
FROM information_schema.tables
WHERE table_schema = [".cDBNAME."]
    AND table_type = 'BASE TABLE'
    AND table_name = 'hs_migrations';

SET @query = If(@exists>0,
    'RENAME TABLE hs_migrations TO HS_Migrations',
    'SELECT \'nothing to rename\' status');

PREPARE stmt FROM @query;

EXECUTE stmt;");


##########################################
# Deleted in v4
##########################################

# mysql_query("RENAME TABLE `hs_documents_location` TO `HS_Documents_Location`");


//Complete
echo "Complete";
?>