#!/usr/bin/perl


use strict;
my $start_pwd;

BEGIN {
  use File::Basename;
  use Cwd;
  $start_pwd = getcwd;
  chdir(dirname($0));
}

use lib ".";
use pba;
use db;
use helper;
use Cwd 'abs_path';
use lib abs_path(".");


my %tables = ();
$tables{AttrType}->{Name} = 1;
$tables{AttrType}->{Descr} = 1;
$tables{AttrTypeArc}->{Name} = 1;
$tables{AttrTypeArc}->{Descr} = 1;
$tables{BMResource}->{description} = 1;
$tables{BMResourceArc}->{description} = 1;
$tables{CardClass}->{Name} = 1;
$tables{CardClassArc}->{Name} = 1;
$tables{Country}->{Name} = 1;
$tables{CountryArc}->{Name} = 1;
$tables{CountryUnion}->{Name} = 1;
$tables{CountryUnionArc}->{Name} = 1;
$tables{Currency}->{Name} = 1;
$tables{CurrencyArc}->{Name} = 1;
$tables{FraudScreeningRule}->{FulfillMessage} = 1;
$tables{FraudScreeningRule}->{FailMessage} = 1;
$tables{FraudScreeningRule}->{ExcOnOrderPlacement} = 1;
$tables{FraudScreeningRuleArc}->{FulfillMessage} = 1;
$tables{FraudScreeningRuleArc}->{FailMessage} = 1;
$tables{FraudScreeningRuleArc}->{ExcOnOrderPlacement} = 1;
$tables{MeasurementUnit}->{Description} = 1;
$tables{MeasurementUnitArc}->{Description} = 1;
$tables{Plan}->{name} = 1;
$tables{Plan}->{shortDescription} = 1;
$tables{Plan}->{longDescription} = 1;
$tables{PlanArc}->{name} = 1;
$tables{PlanArc}->{shortDescription} = 1;
$tables{PlanArc}->{longDescription} = 1;
$tables{PlanCategory}->{description} = 1;
$tables{PlanCategoryArc}->{description} = 1;
$tables{PlanRate}->{StoreText} = 1;
$tables{PlanRate}->{StorePriceText} = 1;
$tables{PlanRateArc}->{StoreText} = 1;
$tables{PlanRateArc}->{StorePriceText} = 1;
$tables{PlanRatePeriod}->{StoreText} = 1;
$tables{PlanRatePeriod}->{StorePriceText} = 1;
$tables{PlanRatePeriodArc}->{StoreText} = 1;
$tables{PlanRatePeriodArc}->{StorePriceText} = 1;
$tables{ResourceCategory}->{Description} = 1;
$tables{ResourceCategoryArc}->{Description} = 1;
$tables{ServiceTemplate}->{name} = 1;
$tables{ServiceTemplateArc}->{name} = 1;
$tables{StoreCategory}->{Name} = 1;
$tables{StoreCategory}->{ShortDescription} = 1;
$tables{StoreCategory}->{LongDescription} = 1;
$tables{StoreCategory}->{CCPDescr} = 1;
$tables{StoreCategoryArc}->{Name} = 1;
$tables{StoreCategoryArc}->{ShortDescription} = 1;
$tables{StoreCategoryArc}->{LongDescription} = 1;
$tables{StoreCategoryArc}->{CCPDescr} = 1;
$tables{UniqueGroup}->{Description} = 1;
$tables{UniqueGroupArc}->{Description} = 1;
$tables{ExternalHelpdeskSetup}->{StatusMessage} = 1;
$tables{ExternalHelpdeskSetupArc}->{StatusMessage} = 1;
$tables{LicClass}->{Description} = 1;
$tables{LicClassArc}->{Description} = 1;
$tables{DefaultNicTRRejectStatus}->{LongDescr} = 1;
$tables{DefaultNicTRRejectStatusArc}->{LongDescr} = 1;
$tables{NicTRRejectStatus}->{LongDescr} = 1;
$tables{NicTRRejectStatusArc}->{LongDescr} = 1;
$tables{StoreFeature}->{Title} = 1;
$tables{StoreFeature}->{Description} = 1;
$tables{StoreFeatureArc}->{Title} = 1;
$tables{StoreFeatureArc}->{Description} = 1;
$tables{StoreFeaturePlan}->{Value} = 1;
$tables{StoreFeaturePlanArc}->{Value} = 1;
$tables{StoreGeneralParam}->{Description} = 1;
$tables{StoreGeneralParamArc}->{Description} = 1;
$tables{StoreGeneralParamVendor}->{Description} = 1;
$tables{StoreGeneralParamVendorArc}->{Description} = 1;
$tables{StoreGeneralSection}->{Description} = 1;
$tables{StoreGeneralSectionArc}->{Description} = 1;
$tables{StoreGeneralSectionVendor}->{Description} = 1;
$tables{StoreGeneralSectionVendorArc}->{Description} = 1;
$tables{StoreLayout}->{Description} = 1;
$tables{StoreLayoutArc}->{Description} = 1;
$tables{StorePath}->{Name} = 1;
$tables{StorePath}->{Description} = 1;
$tables{StorePathArc}->{Name} = 1;
$tables{StorePathArc}->{Description} = 1;
$tables{StorePathParam}->{Description} = 1;
$tables{StorePathParamArc}->{Description} = 1;
$tables{StorePathTemplate}->{Description} = 1;
$tables{StorePathTemplateArc}->{Description} = 1;
$tables{StoreRedirect}->{Description} = 1;
$tables{StoreRedirectArc}->{Description} = 1;
$tables{StoreScreen}->{Title} = 1;
$tables{StoreScreen}->{Description} = 1;
$tables{StoreScreenArc}->{Title} = 1;
$tables{StoreScreenArc}->{Description} = 1;
$tables{StoreScreenParam}->{Description} = 1;
$tables{StoreScreenParamArc}->{Description} = 1;
$tables{StoreTemplate}->{Description} = 1;
$tables{StoreTemplateArc}->{Description} = 1;
$tables{StoreTemplateParam}->{Description} = 1;
$tables{StoreTemplateParamArc}->{Description} = 1;
$tables{SwisscomPlansWithCodes}->{Descr} = 1;
$tables{SwisscomPlansWithCodesArc}->{Descr} = 1;
$tables{SwisscomSettings}->{Emails} = 1;
$tables{SwisscomSettingsArc}->{Emails} = 1;


sub usage {
my $usage=<<USAGE;
Script $0 removes specified locale from locales-list and database.

Usage example:
perl $0 de=Deutsch

Note: Specified locale should match exactly with the locale definition in %pba_root%/etc/ssm.conf.d/locales-list
USAGE

print $usage;
exit 0;
}

sub main {
  usage() if (!$ARGV[0] || ($ARGV[0] eq '-h') || ($ARGV[0] eq '--help'));
  my $locale = $ARGV[0];
  my ($locale_id,$locale_name) = split '=', $locale;
  usage() unless ($locale_id && $locale_name);
  print "Are you sure that you want to delete $locale? [yes/no]: ";
  my $confirm = ask_var("no");
  if ($confirm ne "yes") {
    print "Break exection\n";
    return 0;
  }

  db::connect();

  pba::remove_locale($locale);
  foreach my $table ( keys(%tables) ) {
    next if (!db::is_table_exist($table));
    foreach my $col ( keys(%{$tables{$table}}) ) {
	my $locale_col = $col . "_" . $locale_id;
        next unless (db::is_column_exist($table,$locale_col));
        my $sql = "ALTER TABLE `$table` DROP COLUMN `$locale_col`";
        db::sql_do($sql);
        db::commit();
        if ( $@ ) {
          print "ERROR:$@ when drop column '$locale_col' from table '$table'\n";
        }
    }
  }
}##/main


main();
