var settings = require('./settings')

var fs = require('fs')
var path = require('path')
var mkdirp = require('mkdirp')

const GLOBAL_FORMS = settings.getGlobalFormsDirectory()
const GLOBAL_MAPS = settings.getGlobalMapsDirectory()

function getFormUrls (cb) {
  var local_forms = settings.getUserFormsDirectory()
  fs.readdir(GLOBAL_FORMS, function (err, global) {
    fs.readdir(local_forms, function (err2, local) {
      if (err) {
        cb(err)
      } else if (err2) {
        cb(err2)
      } else {
        var files = global.concat(local)
        var links = []
        for (var i = 0; i < files.length; i++) {
          links[i] = settings.getBaseUrl() + '/forms/' + files[i]
        }
        cb(err, links)
      }
    })
  })
}

function getForm (file, cb) {
  var local_forms = settings.getUserFormsDirectory()
  fs.readFile(local_forms + '/' + file, function (err, data) {
    if (err) {
      fs.readFile(GLOBAL_FORMS + '/' + file, function (err2, data2) {
        cb(err2, data2)
      })
    } else {
      cb(err, data)
    }
  })
}

function getMap (file, cb) {
  var mapFile = path.join(GLOBAL_MAPS, file)
  fs.exists(mapFile, function (exists) {
    if (exists) {
      fs.readFile(mapFile, function (err, data) {
        cb(err, data)
      })
    } else {
      mkdirp(path.dirname(mapFile), function (err) {
        if (err) {
          cb(err)
        } else {
          var blank = '{ "type": "FeatureCollection", "features": [] }'
          fs.writeFile(mapFile, blank, function (err) {
            if (err) {
              cb(err)
            } else {
              getMap(file, cb)
            }
          })
        }
      })
    }
  })
}

function saveMap (file, data, cb) {
  var contents = data
  if (typeof(data) != 'string')
    contents = JSON.stringify(data, null, 2)
  var mapFile = path.join(GLOBAL_MAPS, file)
  fs.writeFile(mapFile, contents, function (err) {
    cb(err)
  })
}

module.exports = {
  getFormUrls: getFormUrls,
  getForm: getForm,
  getMap: getMap,
  saveMap: saveMap
}

@@ -117,64 +117,8 @@
 ')%0A%0A
-const GLOBAL_FORMS = settings.getGlobalFormsDirectory()%0A
 cons
@@ -253,62 +253,8 @@
 y()%0A
-  fs.readdir(GLOBAL_FORMS, function (err, global) %7B%0A  
   fs
@@ -288,17 +288,16 @@
 ion (err
-2
 , local)
@@ -299,26 +299,24 @@
 ocal) %7B%0A    
-  
 if (err) %7B%0A 
@@ -316,26 +316,24 @@
 rr) %7B%0A      
-  
 cb(err)%0A    
@@ -336,63 +336,17 @@
     
- 
- 
 %7D else 
-if (err2) %7B%0A        cb(err2)%0A      %7D else %7B%0A  
+%7B%0A
     
@@ -363,31 +363,14 @@
 s = 
-g
 lo
-bal.concat(local)%0A  
+cal%0A
     
@@ -392,18 +392,16 @@
 %5D%0A      
-  
 for (var
@@ -429,26 +429,24 @@
 gth; i++) %7B%0A
-  
         link
@@ -499,30 +499,26 @@
 es%5Bi%5D%0A      
- 
- 
 %7D%0A
-  
       cb(err
@@ -530,23 +530,14 @@
 ks)%0A
-  
     %7D%0A
-    %7D)%0A
   %7D)
@@ -690,141 +690,8 @@
 ) %7B%0A
-    if (err) %7B%0A      fs.readFile(GLOBAL_FORMS + '/' + file, function (err2, data2) %7B%0A        cb(err2, data2)%0A      %7D)%0A    %7D else %7B%0A  
     
@@ -704,22 +704,16 @@
 , data)%0A
-    %7D%0A
   %7D)%0A%7D%0A%0A
@@ -1398,24 +1398,25 @@
  if 
+(
 (typeof
-(
+ 
 data) !=
  'st
@@ -1411,16 +1411,17 @@
 data) !=
+=
  'string
@@ -1422,16 +1422,18 @@
 string')
+ %7B
 %0A    con
@@ -1470,16 +1470,20 @@
 ull, 2)%0A
+  %7D%0A
   var ma

Remove global forms abstraction