var permalink = require('../../../models/entry/build/prepare/permalink');
var moment = require('moment');

module.exports = function (req, res, next) {

  var sample = {
    dateStamp: moment.utc(),
    slug: 'sample-post',
    id: 342,
    metadata: {}
  };

  var formats = [
    ['Post name', '{{slug}}'],
    ['Date and name', '{{YYYY}}/{{MM}}/{{D}}/{{slug}}'],
    ['Custom', '']
  ];

  var custom = true;

  formats = formats.map(function(arr){

    var checked = arr[1] === req.blog.permalink.format ? 'checked' : '';

    if (checked) custom = false;

    return {
      name: arr[0],
      value: arr[1],
      checked: checked,
      custom: arr[0] === 'Custom' ? 'custom' : '',
      example: permalink(req.blog.timeZone, arr[1], sample)
    };
  });

  if (custom) {
    formats[formats.length - 1].checked = 'checked';
  }

  res.locals.permalinkFormats = formats;

  next();
};
@@ -388,30 +388,8 @@
 %5D;%0A%0A
-  var custom = true;%0A%0A
   fo
@@ -446,19 +446,21 @@
 d = 
-arr%5B1%5D === 
+'';%0A%0A    if (
 req.
@@ -478,66 +478,189 @@
 ink.
-format ? 'checked' : '';%0A%0A    if (checked) custom = false;
+isCustom && arr%5B0%5D === 'Custom') %7B%0A      checked = 'checked';%0A    %7D else if (!req.blog.permalink.isCustom && arr%5B1%5D === req.blog.permalink.format) %7B%0A      checked = 'checked';%0A    %7D
 %0A%0A  
@@ -864,82 +864,8 @@
 );%0A%0A
-  if (custom) %7B%0A    formats%5Bformats.length - 1%5D.checked = 'checked';%0A  %7D%0A%0A
   re

use is custom flag