#!/bin/sh

# CHENGE YOUR CONFIGRATION
DB=onedepo

ONEDEPOUSER=onedepo
ONEDEPOPASSWORD=onedepo

ADMINUSER=onedepoadmin
ADMINPASSWORD=9PFoz8br

export PGPASSWORD=$ADMINPASSWORD


#---------------------------------------------------------------------
## Create User
psql -U postgres -c "CREATE USER $ADMINUSER ENCRYPTED PASSWORD '$ADMINPASSWORD' CREATEDB CREATEUSER;"
psql -U postgres -c "CREATE USER $ONEDEPOUSER ENCRYPTED PASSWORD '$ONEDEPOPASSWORD' NOCREATEDB NOCREATEUSER;"

## Create Database
echo createdb -O $ADMINUSER -E UTF8 -D pg_default -U $ADMINUSER -e $DB "onedepo DB"
createdb -O $ADMINUSER -E UTF8 -D pg_default -U $ADMINUSER -e $DB "onedepo DB"

echo createlang -U $ADMINUSER -e plpgsql $DB
createlang -U $ADMINUSER -e plpgsql $DB



## Create Table
psql -d $DB -U $ADMINUSER -f scripts/g_master_stamp.sql

psql -d $DB -U $ADMINUSER -f scripts/t_access.sql
psql -d $DB -U $ADMINUSER -f scripts/t_bank.sql
psql -d $DB -U $ADMINUSER -f scripts/t_cast.sql
psql -d $DB -U $ADMINUSER -f scripts/t_cast_item.sql
psql -d $DB -U $ADMINUSER -f scripts/t_cast_type.sql
psql -d $DB -U $ADMINUSER -f scripts/t_consumer_attr.sql
psql -d $DB -U $ADMINUSER -f scripts/t_deposit.sql
psql -d $DB -U $ADMINUSER -f scripts/t_dl_history.sql
psql -d $DB -U $ADMINUSER -f scripts/t_dl_item.sql
psql -d $DB -U $ADMINUSER -f scripts/t_dl_order.sql
psql -d $DB -U $ADMINUSER -f scripts/t_ex_stock_detail.sql
psql -d $DB -U $ADMINUSER -f scripts/t_forward_detail.sql
psql -d $DB -U $ADMINUSER -f scripts/t_genre.sql
psql -d $DB -U $ADMINUSER -f scripts/t_genre_item.sql
psql -d $DB -U $ADMINUSER -f scripts/t_genre_member.sql
psql -d $DB -U $ADMINUSER -f scripts/t_image.sql
psql -d $DB -U $ADMINUSER -f scripts/t_item_basic_attr.sql
psql -d $DB -U $ADMINUSER -f scripts/t_item_struct.sql
psql -d $DB -U $ADMINUSER -f scripts/t_item_web_attr.sql
psql -d $DB -U $ADMINUSER -f scripts/t_label.sql
psql -d $DB -U $ADMINUSER -f scripts/t_mail.sql
psql -d $DB -U $ADMINUSER -f scripts/t_mailtemplate.sql
psql -d $DB -U $ADMINUSER -f scripts/t_maker.sql
psql -d $DB -U $ADMINUSER -f scripts/t_member_admin_attr.sql
psql -d $DB -U $ADMINUSER -f scripts/t_member_basic_attr.sql
psql -d $DB -U $ADMINUSER -f scripts/t_name.sql
psql -d $DB -U $ADMINUSER -f scripts/t_news.sql
psql -d $DB -U $ADMINUSER -f scripts/t_order_detail.sql
psql -d $DB -U $ADMINUSER -f scripts/t_order_title.sql
psql -d $DB -U $ADMINUSER -f scripts/t_pref.sql
psql -d $DB -U $ADMINUSER -f scripts/t_pre_genre_item.sql
psql -d $DB -U $ADMINUSER -f scripts/t_pre_item_attr.sql
psql -d $DB -U $ADMINUSER -f scripts/t_pre_order_detail.sql
psql -d $DB -U $ADMINUSER -f scripts/t_pre_order_title.sql
psql -d $DB -U $ADMINUSER -f scripts/t_ranking.sql
psql -d $DB -U $ADMINUSER -f scripts/t_recommend.sql
psql -d $DB -U $ADMINUSER -f scripts/t_refund.sql
psql -d $DB -U $ADMINUSER -f scripts/t_request.sql
psql -d $DB -U $ADMINUSER -f scripts/t_review.sql
psql -d $DB -U $ADMINUSER -f scripts/t_search_key.sql
psql -d $DB -U $ADMINUSER -f scripts/t_sec_question.sql
psql -d $DB -U $ADMINUSER -f scripts/t_seller_attr.sql
psql -d $DB -U $ADMINUSER -f scripts/t_seller_item.sql
psql -d $DB -U $ADMINUSER -f scripts/t_seller_val.sql
psql -d $DB -U $ADMINUSER -f scripts/t_series.sql
psql -d $DB -U $ADMINUSER -f scripts/t_stock_title.sql
psql -d $DB -U $ADMINUSER -f scripts/t_supplier.sql
psql -d $DB -U $ADMINUSER -f scripts/t_systeminfo.sql
psql -d $DB -U $ADMINUSER -f scripts/t_tag.sql
psql -d $DB -U $ADMINUSER -f scripts/t_trial_member.sql
psql -d $DB -U $ADMINUSER -f scripts/t_type.sql
psql -d $DB -U $ADMINUSER -f scripts/t_watch_list.sql


psql -d $DB -U $ADMINUSER -f scripts/v_item_ranking.sql
psql -d $DB -U $ADMINUSER -f scripts/v_stock_order.sql
psql -d $DB -U $ADMINUSER -f scripts/v_stock_list.sql

psql -d $DB -U $ADMINUSER -f scripts/v_cast_export.sql
psql -d $DB -U $ADMINUSER -f scripts/v_cast_list.sql
psql -d $DB -U $ADMINUSER -f scripts/v_cast_ranking.sql
psql -d $DB -U $ADMINUSER -f scripts/v_genre_export.sql
psql -d $DB -U $ADMINUSER -f scripts/v_item_basic2_list.sql
psql -d $DB -U $ADMINUSER -f scripts/v_item_basic_list.sql
psql -d $DB -U $ADMINUSER -f scripts/v_item_basic_term.sql
psql -d $DB -U $ADMINUSER -f scripts/v_item_import_export.sql
psql -d $DB -U $ADMINUSER -f scripts/v_item_list.sql
psql -d $DB -U $ADMINUSER -f scripts/v_item_list_compact.sql
psql -d $DB -U $ADMINUSER -f scripts/v_item_tag.sql
psql -d $DB -U $ADMINUSER -f scripts/v_label_export.sql
psql -d $DB -U $ADMINUSER -f scripts/v_label_list.sql
psql -d $DB -U $ADMINUSER -f scripts/v_maker_export.sql
psql -d $DB -U $ADMINUSER -f scripts/v_maker_list.sql
psql -d $DB -U $ADMINUSER -f scripts/v_order.sql
psql -d $DB -U $ADMINUSER -f scripts/v_order_basic_list.sql
psql -d $DB -U $ADMINUSER -f scripts/v_order_detail_list.sql
psql -d $DB -U $ADMINUSER -f scripts/v_payment_list.sql
psql -d $DB -U $ADMINUSER -f scripts/v_payment_other.sql
#--psql -d $DB -U $ADMINUSER -f scripts/v_recommend_item.sql
psql -d $DB -U $ADMINUSER -f scripts/v_series_export.sql
psql -d $DB -U $ADMINUSER -f scripts/v_series_list.sql
psql -d $DB -U $ADMINUSER -f scripts/v_stock_item.sql
psql -d $DB -U $ADMINUSER -f scripts/v_stock_stocktitle_list.sql
psql -d $DB -U $ADMINUSER -f scripts/v_supplier_export.sql

psql -d $DB -U $ADMINUSER -f scripts/s_check_length.sql
psql -d $DB -U $ADMINUSER -f scripts/s_check_upd.sql
psql -d $DB -U $ADMINUSER -f scripts/s_get_labelcd.sql
psql -d $DB -U $ADMINUSER -f scripts/s_get_lowprice.sql
psql -d $DB -U $ADMINUSER -f scripts/s_get_makercd.sql
psql -d $DB -U $ADMINUSER -f scripts/s_get_nocase_text.sql
psql -d $DB -U $ADMINUSER -f scripts/s_get_nocase_text_lower.sql
psql -d $DB -U $ADMINUSER -f scripts/s_get_seriescd.sql

psql -d $DB -U $ADMINUSER -f scripts/f_access_increment.sql
psql -d $DB -U $ADMINUSER -f scripts/f_bank_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_cast_item_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_cast_item_update_one.sql
psql -d $DB -U $ADMINUSER -f scripts/f_cast_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_consumer_attr_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_data_count.sql
psql -v ADMINUSER=$ADMINUSER -v ONEDEPOUSER=$ONEDEPOUSER -d $DB -U $ADMINUSER -f scripts/f_deposit_cvsinfo_update.sql
psql -v ADMINUSER=$ADMINUSER -v ONEDEPOUSER=$ONEDEPOUSER -d $DB -U $ADMINUSER -f scripts/f_deposit_remiseinfo_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_deposit_state_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_deposit_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_deposit_update_sellerpaydate.sql
psql -d $DB -U $ADMINUSER -f scripts/f_dl_item_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_dl_order_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_ex_stock_detail_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_ex_stock_detail_update_rackid.sql
psql -d $DB -U $ADMINUSER -f scripts/f_forward_detail_state_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_forward_detail_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_forward_detail_update_returndate.sql
psql -d $DB -U $ADMINUSER -f scripts/f_genre_item_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_genre_member_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_genre_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_image_import.sql
psql -d $DB -U $ADMINUSER -f scripts/f_image_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_image_update_stockcd.sql
psql -d $DB -U $ADMINUSER -f scripts/f_import_cast.sql
psql -d $DB -U $ADMINUSER -f scripts/f_import_cast_item.sql
psql -d $DB -U $ADMINUSER -f scripts/f_import_cast_item2.sql
psql -d $DB -U $ADMINUSER -f scripts/f_import_delete_main.sql
psql -d $DB -U $ADMINUSER -f scripts/f_import_ex_stock_detail.sql
psql -d $DB -U $ADMINUSER -f scripts/f_import_genre_item.sql
psql -d $DB -U $ADMINUSER -f scripts/f_import_item_basic_attr.sql
psql -d $DB -U $ADMINUSER -f scripts/f_import_item_main.sql
psql -d $DB -U $ADMINUSER -f scripts/f_import_item_struct.sql
psql -d $DB -U $ADMINUSER -f scripts/f_import_item_web_attf.sql
psql -d $DB -U $ADMINUSER -f scripts/f_import_main.sql
psql -d $DB -U $ADMINUSER -f scripts/f_item_basic_attr_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_item_basic_attr_update_iteminfo.sql
psql -d $DB -U $ADMINUSER -f scripts/f_item_struct_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_item_web_attf_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_label_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_maker_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_member_basic_attr_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_name_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_news_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_order_detail_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_order_title_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_order_title_update_enquiryno.sql
psql -d $DB -U $ADMINUSER -f scripts/f_pre_genre_item_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_pre_item_attr_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_pre_order_detail_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_rankingrate_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_recommend_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_request_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_review_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_search_key_insert.sql
psql -d $DB -U $ADMINUSER -f scripts/f_seller_attr_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_seller_infomation_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_seller_item_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_seller_val_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_series_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_stock_title_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_stock_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_stock_update_price.sql
psql -d $DB -U $ADMINUSER -f scripts/f_supplier_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_systeminfo_bank_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_systeminfo_design_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_systeminfo_html_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_systeminfo_seo_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_systeminfo_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_tag_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_trial_member_update.sql
psql -d $DB -U $ADMINUSER -f scripts/f_upd_alltitlenum.sql
psql -d $DB -U $ADMINUSER -f scripts/f_upd_preorderflg.sql
psql -d $DB -U $ADMINUSER -f scripts/f_upd_ranking.sql
psql -d $DB -U $ADMINUSER -f scripts/f_upd_stockupddate.sql
psql -d $DB -U $ADMINUSER -f scripts/f_watch_list_update.sql

## Index
psql -d $DB -U $ADMINUSER -f scripts/all_index.sql

## Grant
psql -v ADMINUSER=$ADMINUSER -v ONEDEPOUSER=$ONEDEPOUSER -d $DB -U $ADMINUSER -f scripts/all_owner_grant.sql

## Foreign Key
psql -d $DB -U $ADMINUSER -f scripts/all_foreign_key.sql

## Data
psql -d $DB -U $ADMINUSER -f initialdata/t_name.sql
psql -d $DB -U $ADMINUSER -f initialdata/t_sec_question.sql
psql -d $DB -U $ADMINUSER -f initialdata/t_pref.sql
psql -d $DB -U $ADMINUSER -f initialdata/t_mailtemplate.sql
psql -d $DB -U $ADMINUSER -f initialdata/t_member_admin_attr.sql
psql -d $DB -U $ADMINUSER -f initialdata/t_systeminfo.sql

echo 'Finish!'
