#!/usr/bin/env node

var path = require('path'), fs = require('fs');
var argv = process.argv.slice(2), file = argv[0], locale = path.basename(file);


var lines = fs.readFileSync(file, 'utf8');
var time = lines.slice(lines.indexOf('\nLC_TIME'), lines.indexOf('END LC_TIME')).replace(/%[^\n\/"]+(\/?\n)/g, '$1').replace(/\/\n/g,'').split(/\n/);

var locale = {
  "name": locale,
  "day": {
    "abbrev": [ ],
    "full": [ ]
  },
  "month": { "abbrev": [], "full": [ ] },
  "meridiem": [],
  "date": "",
  "time24": "",
  "dateTime": ""
};
try {
time.filter(function (line) { return ! /^\s*$|^\s*%/.test(line) }).forEach(function (line) {
  var $;
  if ($ = /^([\w\d_]+)(.*)$/.exec(line)) {
    switch ($[1]) {
    case "abday":
      locale.day.abbrev = strings($[2]);
      break;
    case "day":
      locale.day.full = strings($[2]);
      break;
    case "abmon":
      locale.month.abbrev = strings($[2]);
      break;
    case "mon":
      locale.month.full = strings($[2]);
      break;
    case "d_t_fmt":
      locale.dateTime = strings($[2]).pop();
      break;
    case "d_fmt":
      locale.date = strings($[2]).pop();
      break;
    case "t_fmt":
      locale.time24 = strings($[2]).pop();
      break;
    case "t_fmt_ampm":
      locale.time12 = strings($[2]).pop();
      break;
    case "am_pm":
      locale.meridiem = strings($[2]);
      break;
    case "date_fmt":
      locale.full = strings($[2]).pop();
      break;
    }
  }
});
} catch (e) {
  throw e;
}

process.stdout.write("module.exports = ");
process.stdout.write(JSON.stringify(locale, null, 2));
process.stdout.write("\n");

require('child_process').exec('util/rigamorale ' + locale.name, function (error, stdout) {
  stdout = stdout.split(/\n/);
  var shortDays = stdout.shift().split('|');
  var days = stdout.shift().split('|');
  var shortMonths = stdout.shift().split('|');
  var months = stdout.shift().split('|');
  shortDays.pop();
  shortDays.shift();
  days.pop();
  days.shift();
  shortMonths.pop();
  shortMonths.shift();
  months.pop();
  months.shift();
  var AM = stdout.shift();
  var PM = stdout.shift();
  var am = stdout.shift();
  var pm = stdout.shift();
  var dateFormat = stdout.splice(0, 2).shift();
  var morningTimeFormat = stdout.shift();
  var eveningTimeFormat = stdout.shift();
  var morningDateTimeFormat = stdout.shift();
  var eveningDateTimeFormat = stdout.shift();
  //console.log(shortDays, days, shortMonths, months, AM, PM, am, pm, dateFormat, morningTimeFormat, eveningDateTimeFormat, morningDateTimeFormat, eveningDateTimeFormat
//  );
  var name = locale.name;
  fs.writeFileSync("t/locale/" + name + "/days.t", '\
#!/usr/bin/env node\n\
require("../../proof")(14, function (tz, equal) {\n\
  var tz = tz(require("timezone/' + name + '"));\n\
  // ' + name + ' abbreviated days of week\n\
  equal(tz("2006-01-01", "%a", "' + name + '"), "' + shortDays[0] + '", "Sun");\n\
  equal(tz("2006-01-02", "%a", "' + name + '"), "' + shortDays[1] + '", "Mon");\n\
  equal(tz("2006-01-03", "%a", "' + name + '"), "' + shortDays[2] + '", "Tue");\n\
  equal(tz("2006-01-04", "%a", "' + name + '"), "' + shortDays[3] + '", "Wed");\n\
  equal(tz("2006-01-05", "%a", "' + name + '"), "' + shortDays[4] + '", "Thu");\n\
  equal(tz("2006-01-06", "%a", "' + name + '"), "' + shortDays[5] + '", "Fri");\n\
  equal(tz("2006-01-07", "%a", "' + name + '"), "' + shortDays[6] + '", "Sat");\n\
\n\
  // ' + name + ' days of week\n\
  equal(tz("2006-01-01", "%A", "' + name + '"), "' + days[0] + '", "Sunday");\n\
  equal(tz("2006-01-02", "%A", "' + name + '"), "' + days[1] + '", "Monday");\n\
  equal(tz("2006-01-03", "%A", "' + name + '"), "' + days[2] + '", "Tuesday");\n\
  equal(tz("2006-01-04", "%A", "' + name + '"), "' + days[3] + '", "Wednesday");\n\
  equal(tz("2006-01-05", "%A", "' + name + '"), "' + days[4] + '", "Thursday");\n\
  equal(tz("2006-01-06", "%A", "' + name + '"), "' + days[5] + '", "Friday");\n\
  equal(tz("2006-01-07", "%A", "' + name + '"), "' + days[6] + '", "Saturday");\n\
});\n\
');
  fs.chmodSync("t/locale/" + name + "/days.t", 0755);
  fs.writeFileSync("t/locale/" + name + "/months.t", '\
#!/usr/bin/env node\n\
require("../../proof")(24, function (tz, equal) {\n\
  var tz = tz(require("timezone/' + name + '"));\n\
  //' + name + ' abbreviated months\n\
  equal(tz("2000-01-01", "%b", "' + name + '"), "' + shortMonths[0] + '", "Jan");\n\
  equal(tz("2000-02-01", "%b", "' + name + '"), "' + shortMonths[1] + '", "Feb");\n\
  equal(tz("2000-03-01", "%b", "' + name + '"), "' + shortMonths[2] + '", "Mar");\n\
  equal(tz("2000-04-01", "%b", "' + name + '"), "' + shortMonths[3] + '", "Apr");\n\
  equal(tz("2000-05-01", "%b", "' + name + '"), "' + shortMonths[4] + '", "May");\n\
  equal(tz("2000-06-01", "%b", "' + name + '"), "' + shortMonths[5] + '", "Jun");\n\
  equal(tz("2000-07-01", "%b", "' + name + '"), "' + shortMonths[6] + '", "Jul");\n\
  equal(tz("2000-08-01", "%b", "' + name + '"), "' + shortMonths[7] + '", "Aug");\n\
  equal(tz("2000-09-01", "%b", "' + name + '"), "' + shortMonths[8] + '", "Sep");\n\
  equal(tz("2000-10-01", "%b", "' + name + '"), "' + shortMonths[9] + '", "Oct");\n\
  equal(tz("2000-11-01", "%b", "' + name + '"), "' + shortMonths[10] + '", "Nov");\n\
  equal(tz("2000-12-01", "%b", "' + name + '"), "' + shortMonths[11] + '", "Dec");\n\
\n\
  // ' + name + ' months\n\
  equal(tz("2000-01-01", "%B", "' + name + '"), "' + months[0] + '", "January");\n\
  equal(tz("2000-02-01", "%B", "' + name + '"), "' + months[1] + '", "February");\n\
  equal(tz("2000-03-01", "%B", "' + name + '"), "' + months[2] + '", "March");\n\
  equal(tz("2000-04-01", "%B", "' + name + '"), "' + months[3] + '", "April");\n\
  equal(tz("2000-05-01", "%B", "' + name + '"), "' + months[4] + '", "May");\n\
  equal(tz("2000-06-01", "%B", "' + name + '"), "' + months[5] + '", "June");\n\
  equal(tz("2000-07-01", "%B", "' + name + '"), "' + months[6] + '", "July");\n\
  equal(tz("2000-08-01", "%B", "' + name + '"), "' + months[7] + '", "August");\n\
  equal(tz("2000-09-01", "%B", "' + name + '"), "' + months[8] + '", "September");\n\
  equal(tz("2000-10-01", "%B", "' + name + '"), "' + months[9] + '", "October");\n\
  equal(tz("2000-11-01", "%B", "' + name + '"), "' + months[10] + '", "November");\n\
  equal(tz("2000-12-01", "%B", "' + name + '"), "' + months[11] + '", "December");\n\
});\n\
');
  fs.chmodSync("t/locale/" + name + "/months.t", 0755);

  fs.writeFileSync("t/locale/" + name + "/formats.t", '\
#!/usr/bin/env node\n\
require("../../proof")(5, function (tz, equal) {\n\
  var tz = tz(require("timezone/' + name + '"));\n\
  // ' + name + ' date representation\n\
  equal(tz("2000-09-03", "%x", "' + name + '"), "' + dateFormat + '", "date format");\n\
\n\
  // ' + name + ' time representation\n\
  equal(tz("2000-09-03 08:05:04", "%X", "' + name + '"), "' + morningTimeFormat + '", "long time format morning");\n\
  equal(tz("2000-09-03 23:05:04", "%X", "' + name + '"), "' + eveningTimeFormat + '", "long time format evening");\n\
\n\
  // ' + name + ' date time representation\n\
  equal(tz("2000-09-03 08:05:04", "%c", "' + name + '"), "' + morningDateTimeFormat + '", "long date format morning");\n\
  equal(tz("2000-09-03 23:05:04", "%c", "' + name + '"), "' + eveningDateTimeFormat + '", "long date format evening");\n\
});\n\
');
  fs.chmodSync("t/locale/" + name + "/formats.t", 0755);
  if (am || AM || pm || PM) {
    fs.writeFileSync('t/locale/' + name + '/meridiem.t', '\
#!/usr/bin/env node\n\
require("../../proof")(4, function (tz, equal) {\n\
  var tz = tz(require("timezone/' + name + '"));\n\
  // ' + name + ' meridiem upper case\n\
  equal(tz("2000-09-03 08:05:04", "%P", "' + name + '"), "' + am + '", "ante meridiem lower case");\n\
  equal(tz("2000-09-03 23:05:04", "%P", "' + name + '"), "' + pm + '", "post meridiem lower case");\n\
\n\
  // ' + name + ' meridiem lower case\n\
  equal(tz("2000-09-03 08:05:04", "%p", "' + name +  '"), "' + AM + '", "ante meridiem upper case");\n\
  equal(tz("2000-09-03 23:05:04", "%p", "' + name + '"), "' + PM + '", "post meridiem upper case");\n\
});\n\
');
    fs.chmodSync('t/locale/' + name + '/meridiem.t', 0755);
  }
});


function strings (line) {
  return line.trim().split(/\s*;\s*/).map(function (string) {
    return JSON.parse(string.replace(/<U([\da-f]+)>/ig, '\\u$1'));
  });
}
