// Compiled by ClojureScript 0.0-2138
goog.provide('lt.objs.deploy');
goog.require('cljs.core');
goog.require('lt.util.cljs');
goog.require('lt.util.js');
goog.require('lt.objs.app');
goog.require('lt.objs.sidebar.command');
goog.require('lt.objs.files');
goog.require('lt.util.js');
goog.require('lt.objs.platform');
goog.require('lt.objs.popup');
goog.require('lt.objs.cache');
goog.require('lt.objs.popup');
goog.require('lt.objs.notifos');
goog.require('lt.objs.notifos');
goog.require('clojure.string');
goog.require('lt.util.cljs');
goog.require('lt.objs.platform');
goog.require('lt.objs.files');
goog.require('lt.objs.app');
goog.require('lt.objs.clients');
goog.require('lt.util.load');
goog.require('clojure.string');
goog.require('lt.object');
goog.require('lt.object');
goog.require('lt.util.load');
goog.require('lt.objs.console');
goog.require('lt.objs.console');
goog.require('lt.objs.cache');
goog.require('lt.objs.clients');
goog.require('lt.objs.sidebar.command');
goog.require('fetch.core');
goog.require('fetch.core');
lt.objs.deploy.shell = lt.util.load.node_module.call(null,"shelljs");
lt.objs.deploy.fs = require("fs");
lt.objs.deploy.zlib = require("zlib");
lt.objs.deploy.request = lt.util.load.node_module.call(null,"request");
lt.objs.deploy.tar = lt.util.load.node_module.call(null,"tar");
lt.objs.deploy.home_path = lt.objs.files.lt_home.call(null,"");
lt.objs.deploy.tar_path = (function tar_path(v){if(cljs.core.truth_(lt.objs.cache.fetch.call(null,new cljs.core.Keyword(null,"edge","edge",1017012527))))
{return [cljs.core.str("https://api.github.com/repos/LightTable/LightTable/tarball/master")].join('');
} else
{return [cljs.core.str("https://api.github.com/repos/LightTable/LightTable/tarball/"),cljs.core.str(v)].join('');
}
});
lt.objs.deploy.version_regex = /^\d+\.\d+\.\d+(-.*)?$/;
lt.objs.deploy.get_versions = (function get_versions(){var vstr = new cljs.core.Keyword(null,"content","content",1965434859).cljs$core$IFn$_invoke$arity$1(lt.objs.files.open_sync.call(null,lt.objs.files.lt_home.call(null,"core/version.json")));return cljs.core.js__GT_clj.call(null,JSON.parse(vstr),new cljs.core.Keyword(null,"keywordize-keys","keywordize-keys",4191781672),true);
});
lt.objs.deploy.proxy_QMARK_ = (function proxy_QMARK_(){var p = lt.objs.deploy.get_proxy.call(null,lt.objs.deploy.tar_path.call(null,"0.5.0"));if(lt.util.cljs.str_contains_QMARK_.call(null,p,"PROXY"))
{return cljs.core.second.call(null,clojure.string.split.call(null,p," "));
} else
{return null;
}
});
lt.objs.deploy.version_timeout = ((60 * 60) * 1000);
lt.objs.deploy.version = lt.objs.deploy.get_versions.call(null);
lt.objs.deploy.str__GT_version = (function str__GT_version(s){var vec__7857 = clojure.string.split.call(null,s,".");var major = cljs.core.nth.call(null,vec__7857,0,null);var minor = cljs.core.nth.call(null,vec__7857,1,null);var patch = cljs.core.nth.call(null,vec__7857,2,null);return new cljs.core.PersistentArrayMap(null, 3, [new cljs.core.Keyword(null,"major","major",1117563179),parseInt(major),new cljs.core.Keyword(null,"minor","minor",1117805351),parseInt(minor),new cljs.core.Keyword(null,"patch","patch",1120342970),parseInt(patch)], null);
});
lt.objs.deploy.compare_versions = (function compare_versions(v1,v2){if(cljs.core._EQ_.call(null,v1,v2))
{return false;
} else
{return !(((new cljs.core.Keyword(null,"major","major",1117563179).cljs$core$IFn$_invoke$arity$1(v2) < new cljs.core.Keyword(null,"major","major",1117563179).cljs$core$IFn$_invoke$arity$1(v1))) || ((cljs.core._EQ_.call(null,new cljs.core.Keyword(null,"major","major",1117563179).cljs$core$IFn$_invoke$arity$1(v2),new cljs.core.Keyword(null,"major","major",1117563179).cljs$core$IFn$_invoke$arity$1(v1))) && ((new cljs.core.Keyword(null,"minor","minor",1117805351).cljs$core$IFn$_invoke$arity$1(v2) < new cljs.core.Keyword(null,"minor","minor",1117805351).cljs$core$IFn$_invoke$arity$1(v1)))) || ((cljs.core._EQ_.call(null,new cljs.core.Keyword(null,"major","major",1117563179).cljs$core$IFn$_invoke$arity$1(v2),new cljs.core.Keyword(null,"major","major",1117563179).cljs$core$IFn$_invoke$arity$1(v1))) && (cljs.core._EQ_.call(null,new cljs.core.Keyword(null,"minor","minor",1117805351).cljs$core$IFn$_invoke$arity$1(v2),new cljs.core.Keyword(null,"minor","minor",1117805351).cljs$core$IFn$_invoke$arity$1(v1))) && ((new cljs.core.Keyword(null,"patch","patch",1120342970).cljs$core$IFn$_invoke$arity$1(v2) < new cljs.core.Keyword(null,"patch","patch",1120342970).cljs$core$IFn$_invoke$arity$1(v1)))));
}
});
/**
* Returns true if second version is newer/greater than first version.
*/
lt.objs.deploy.is_newer_QMARK_ = (function is_newer_QMARK_(v1,v2){return lt.objs.deploy.compare_versions.call(null,lt.objs.deploy.str__GT_version.call(null,v1),lt.objs.deploy.str__GT_version.call(null,v2));
});
lt.objs.deploy.download_file = (function download_file(from,to,cb){var options = (function (){var obj7863 = {"url":from,"headers":(function (){var obj7865 = {"User-Agent":"Light Table"};return obj7865;
})()};return obj7863;
})();var out = lt.objs.deploy.fs.createWriteStream(to);var temp__4092__auto___7866 = (function (){var or__3520__auto__ = process.env.http_proxy;if(cljs.core.truth_(or__3520__auto__))
{return or__3520__auto__;
} else
{return process.env.https_proxy;
}
})();if(cljs.core.truth_(temp__4092__auto___7866))
{var proxy_7867 = temp__4092__auto___7866;options.proxy = proxy_7867;
} else
{}
return lt.objs.deploy.request.get(options,cb).on("response",(function (resp){if(cljs.core._EQ_.call(null,resp.statusCode,200))
{return null;
} else
{lt.objs.notifos.done_working.call(null);
throw (new Error([cljs.core.str("Error downloading: "),cljs.core.str(from),cljs.core.str(" status code: "),cljs.core.str(resp.statusCode)].join('')));
}
})).pipe(out);
});
lt.objs.deploy.download_zip = (function download_zip(ver,cb){var n = lt.objs.notifos.working.call(null,[cljs.core.str("Downloading version "),cljs.core.str(ver),cljs.core.str(" ..")].join(''));return lt.objs.deploy.download_file.call(null,lt.objs.deploy.tar_path.call(null,ver),[cljs.core.str(lt.objs.deploy.home_path),cljs.core.str("/tmp.tar.gz")].join(''),(function (e,r,body){lt.objs.notifos.done_working.call(null);
return cb.call(null,e,r,body);
}));
});
lt.objs.deploy.untar = (function untar(from,to,cb){var t = lt.objs.deploy.fs.createReadStream(from);return t.pipe(lt.objs.deploy.zlib.createGunzip()).pipe(lt.objs.deploy.tar.Extract((function (){var obj7871 = {"path":to};return obj7871;
})())).on("end",cb);
});
lt.objs.deploy.move_tmp = (function move_tmp(){var parent_dir_7880 = cljs.core.first.call(null,lt.objs.files.full_path_ls.call(null,[cljs.core.str(lt.objs.deploy.home_path),cljs.core.str("/tmp/")].join('')));var seq__7876_7881 = cljs.core.seq.call(null,lt.objs.files.full_path_ls.call(null,[cljs.core.str(parent_dir_7880),cljs.core.str("/deploy/")].join('')));var chunk__7877_7882 = null;var count__7878_7883 = 0;var i__7879_7884 = 0;while(true){
if((i__7879_7884 < count__7878_7883))
{var file_7885 = cljs.core._nth.call(null,chunk__7877_7882,i__7879_7884);lt.objs.deploy.shell.cp("-rf",file_7885,lt.objs.deploy.home_path);
{
var G__7886 = seq__7876_7881;
var G__7887 = chunk__7877_7882;
var G__7888 = count__7878_7883;
var G__7889 = (i__7879_7884 + 1);
seq__7876_7881 = G__7886;
chunk__7877_7882 = G__7887;
count__7878_7883 = G__7888;
i__7879_7884 = G__7889;
continue;
}
} else
{var temp__4092__auto___7890 = cljs.core.seq.call(null,seq__7876_7881);if(temp__4092__auto___7890)
{var seq__7876_7891__$1 = temp__4092__auto___7890;if(cljs.core.chunked_seq_QMARK_.call(null,seq__7876_7891__$1))
{var c__4262__auto___7892 = cljs.core.chunk_first.call(null,seq__7876_7891__$1);{
var G__7893 = cljs.core.chunk_rest.call(null,seq__7876_7891__$1);
var G__7894 = c__4262__auto___7892;
var G__7895 = cljs.core.count.call(null,c__4262__auto___7892);
var G__7896 = 0;
seq__7876_7881 = G__7893;
chunk__7877_7882 = G__7894;
count__7878_7883 = G__7895;
i__7879_7884 = G__7896;
continue;
}
} else
{var file_7897 = cljs.core.first.call(null,seq__7876_7891__$1);lt.objs.deploy.shell.cp("-rf",file_7897,lt.objs.deploy.home_path);
{
var G__7898 = cljs.core.next.call(null,seq__7876_7891__$1);
var G__7899 = null;
var G__7900 = 0;
var G__7901 = 0;
seq__7876_7881 = G__7898;
chunk__7877_7882 = G__7899;
count__7878_7883 = G__7900;
i__7879_7884 = G__7901;
continue;
}
}
} else
{}
}
break;
}
return lt.objs.deploy.shell.rm("-rf",[cljs.core.str(lt.objs.deploy.home_path),cljs.core.str("/tmp*")].join(''));
});
lt.objs.deploy.fetch_and_deploy = (function fetch_and_deploy(ver){return lt.objs.deploy.download_zip.call(null,ver,(function (){lt.objs.notifos.working.call(null,"Extracting update...");
return lt.objs.deploy.untar.call(null,[cljs.core.str(lt.objs.deploy.home_path),cljs.core.str("/tmp.tar.gz")].join(''),[cljs.core.str(lt.objs.deploy.home_path),cljs.core.str("/tmp")].join(''),(function (){lt.objs.deploy.move_tmp.call(null);
lt.objs.notifos.done_working.call(null);
lt.objs.deploy.version = cljs.core.assoc.call(null,lt.objs.deploy.version,new cljs.core.Keyword(null,"version","version",1365512266),ver);
return lt.objs.popup.popup_BANG_.call(null,new cljs.core.PersistentArrayMap(null, 3, [new cljs.core.Keyword(null,"header","header",4087600639),"Light Table has been updated!",new cljs.core.Keyword(null,"body","body",1016933652),[cljs.core.str("Light Table has been updated to "),cljs.core.str(ver),cljs.core.str("! Just\n                                                         restart to get the latest and greatest.")].join(''),new cljs.core.Keyword(null,"buttons","buttons",1255256819),new cljs.core.PersistentVector(null, 1, 5, cljs.core.PersistentVector.EMPTY_NODE, [new cljs.core.PersistentArrayMap(null, 1, [new cljs.core.Keyword(null,"label","label",1116631654),"ok"], null)], null)], null));
}));
}));
});
lt.objs.deploy.tags_url = "https://api.github.com/repos/LightTable/LightTable/tags";
lt.objs.deploy.should_update_popup = (function should_update_popup(data){return lt.objs.popup.popup_BANG_.call(null,new cljs.core.PersistentArrayMap(null, 3, [new cljs.core.Keyword(null,"header","header",4087600639),"There's a newer version of Light Table!",new cljs.core.Keyword(null,"body","body",1016933652),[cljs.core.str("Would you like us to download and install version "),cljs.core.str(data),cljs.core.str("?")].join(''),new cljs.core.Keyword(null,"buttons","buttons",1255256819),new cljs.core.PersistentVector(null, 2, 5, cljs.core.PersistentVector.EMPTY_NODE, [new cljs.core.PersistentArrayMap(null, 1, [new cljs.core.Keyword(null,"label","label",1116631654),"Cancel"], null),new cljs.core.PersistentArrayMap(null, 2, [new cljs.core.Keyword(null,"label","label",1116631654),"Download and install",new cljs.core.Keyword(null,"action","action",3885920680),(function (){return lt.objs.deploy.fetch_and_deploy.call(null,data);
})], null)], null)], null));
});
/**
* Returns latest LT version for github api tags endpoint.
*/
lt.objs.deploy.__GT_latest_version = (function __GT_latest_version(body){var temp__4092__auto__ = (function (){try{return JSON.parse(body);
}catch (e7904){var e = e7904;return lt.objs.console.error.call(null,[cljs.core.str("Invalid JSON response from "),cljs.core.str(lt.objs.deploy.tags_url),cljs.core.str(": "),cljs.core.str(cljs.core.pr_str.call(null,body))].join(''));
}})();if(cljs.core.truth_(temp__4092__auto__))
{var parsed_body = temp__4092__auto__;return cljs.core.last.call(null,cljs.core.sort.call(null,cljs.core.keep.call(null,(function (p1__7902_SHARP_){if(cljs.core.truth_(cljs.core.re_find.call(null,lt.objs.deploy.version_regex,p1__7902_SHARP_.name)))
{return p1__7902_SHARP_.name;
} else
{return null;
}
}),parsed_body)));
} else
{return null;
}
});
/**
* @param {...*} var_args
*/
lt.objs.deploy.check_version = (function() { 
var check_version__delegate = function (p__7905){var vec__7907 = p__7905;var notify_QMARK_ = cljs.core.nth.call(null,vec__7907,0,null);return fetch.core.xhr.call(null,lt.objs.deploy.tags_url,cljs.core.PersistentArrayMap.EMPTY,(function (data){var latest_version = lt.objs.deploy.__GT_latest_version.call(null,data);if(cljs.core.truth_(cljs.core.re_find.call(null,lt.objs.deploy.version_regex,latest_version)))
{if(cljs.core.truth_((function (){var and__3508__auto__ = cljs.core.not_EQ_.call(null,latest_version,"");if(and__3508__auto__)
{var and__3508__auto____$1 = cljs.core.not_EQ_.call(null,latest_version,new cljs.core.Keyword(null,"version","version",1365512266).cljs$core$IFn$_invoke$arity$1(lt.objs.deploy.version));if(and__3508__auto____$1)
{var and__3508__auto____$2 = lt.objs.deploy.is_newer_QMARK_.call(null,new cljs.core.Keyword(null,"version","version",1365512266).cljs$core$IFn$_invoke$arity$1(lt.objs.deploy.version),latest_version);if(and__3508__auto____$2)
{var or__3520__auto__ = notify_QMARK_;if(cljs.core.truth_(or__3520__auto__))
{return or__3520__auto__;
} else
{return cljs.core.not_EQ_.call(null,localStorage.fetchedVersion,latest_version);
}
} else
{return and__3508__auto____$2;
}
} else
{return and__3508__auto____$1;
}
} else
{return and__3508__auto__;
}
})()))
{localStorage.fetchedVersion = latest_version;
return lt.objs.deploy.should_update_popup.call(null,latest_version);
} else
{if(cljs.core.truth_(notify_QMARK_))
{return lt.objs.notifos.set_msg_BANG_.call(null,[cljs.core.str("At latest version: "),cljs.core.str(new cljs.core.Keyword(null,"version","version",1365512266).cljs$core$IFn$_invoke$arity$1(lt.objs.deploy.version))].join(''));
} else
{return null;
}
}
} else
{return null;
}
}));
};
var check_version = function (var_args){
var p__7905 = null;if (arguments.length > 0) {
  p__7905 = cljs.core.array_seq(Array.prototype.slice.call(arguments, 0),0);} 
return check_version__delegate.call(this,p__7905);};
check_version.cljs$lang$maxFixedArity = 0;
check_version.cljs$lang$applyTo = (function (arglist__7908){
var p__7905 = cljs.core.seq(arglist__7908);
return check_version__delegate(p__7905);
});
check_version.cljs$core$IFn$_invoke$arity$variadic = check_version__delegate;
return check_version;
})()
;
/**
* Binary/electron version. The two versions are in sync since binaries updates
* only occur with electron updates.
*/
lt.objs.deploy.binary_version = (function binary_version(){return (process.versions["electron"]);
});
/**
* @param {...*} var_args
*/
lt.objs.deploy.button = (function() { 
var button__delegate = function (label,p__7909){var vec__7917 = p__7909;var cb = cljs.core.nth.call(null,vec__7917,0,null);var e__3325__auto__ = crate.core.html.call(null,new cljs.core.PersistentVector(null, 2, 5, cljs.core.PersistentVector.EMPTY_NODE, [new cljs.core.Keyword(null,"div.button.right","div.button.right",629503503),label], null));var seq__7918_7924 = cljs.core.seq.call(null,cljs.core.partition.call(null,2,new cljs.core.PersistentVector(null, 2, 5, cljs.core.PersistentVector.EMPTY_NODE, [new cljs.core.Keyword(null,"click","click",1108654330),(function (){if(cljs.core.truth_(cb))
{return cb.call(null);
} else
{return null;
}
})], null)));var chunk__7919_7925 = null;var count__7920_7926 = 0;var i__7921_7927 = 0;while(true){
if((i__7921_7927 < count__7920_7926))
{var vec__7922_7928 = cljs.core._nth.call(null,chunk__7919_7925,i__7921_7927);var ev__3326__auto___7929 = cljs.core.nth.call(null,vec__7922_7928,0,null);var func__3327__auto___7930 = cljs.core.nth.call(null,vec__7922_7928,1,null);lt.util.dom.on.call(null,e__3325__auto__,ev__3326__auto___7929,func__3327__auto___7930);
{
var G__7931 = seq__7918_7924;
var G__7932 = chunk__7919_7925;
var G__7933 = count__7920_7926;
var G__7934 = (i__7921_7927 + 1);
seq__7918_7924 = G__7931;
chunk__7919_7925 = G__7932;
count__7920_7926 = G__7933;
i__7921_7927 = G__7934;
continue;
}
} else
{var temp__4092__auto___7935 = cljs.core.seq.call(null,seq__7918_7924);if(temp__4092__auto___7935)
{var seq__7918_7936__$1 = temp__4092__auto___7935;if(cljs.core.chunked_seq_QMARK_.call(null,seq__7918_7936__$1))
{var c__4262__auto___7937 = cljs.core.chunk_first.call(null,seq__7918_7936__$1);{
var G__7938 = cljs.core.chunk_rest.call(null,seq__7918_7936__$1);
var G__7939 = c__4262__auto___7937;
var G__7940 = cljs.core.count.call(null,c__4262__auto___7937);
var G__7941 = 0;
seq__7918_7924 = G__7938;
chunk__7919_7925 = G__7939;
count__7920_7926 = G__7940;
i__7921_7927 = G__7941;
continue;
}
} else
{var vec__7923_7942 = cljs.core.first.call(null,seq__7918_7936__$1);var ev__3326__auto___7943 = cljs.core.nth.call(null,vec__7923_7942,0,null);var func__3327__auto___7944 = cljs.core.nth.call(null,vec__7923_7942,1,null);lt.util.dom.on.call(null,e__3325__auto__,ev__3326__auto___7943,func__3327__auto___7944);
{
var G__7945 = cljs.core.next.call(null,seq__7918_7936__$1);
var G__7946 = null;
var G__7947 = 0;
var G__7948 = 0;
seq__7918_7924 = G__7945;
chunk__7919_7925 = G__7946;
count__7920_7926 = G__7947;
i__7921_7927 = G__7948;
continue;
}
}
} else
{}
}
break;
}
return e__3325__auto__;
};
var button = function (label,var_args){
var p__7909 = null;if (arguments.length > 1) {
  p__7909 = cljs.core.array_seq(Array.prototype.slice.call(arguments, 1),0);} 
return button__delegate.call(this,label,p__7909);};
button.cljs$lang$maxFixedArity = 1;
button.cljs$lang$applyTo = (function (arglist__7949){
var label = cljs.core.first(arglist__7949);
var p__7909 = cljs.core.rest(arglist__7949);
return button__delegate(label,p__7909);
});
button.cljs$core$IFn$_invoke$arity$variadic = button__delegate;
return button;
})()
;
lt.objs.deploy.alert_binary_update = (function alert_binary_update(){return lt.objs.popup.popup_BANG_.call(null,new cljs.core.PersistentArrayMap(null, 3, [new cljs.core.Keyword(null,"header","header",4087600639),"There's been a binary update!",new cljs.core.Keyword(null,"body","body",1016933652),"There's a new version of the Light Table binary. Clicking below will open the\n                                 Light Table website so you can download the updated version.",new cljs.core.Keyword(null,"buttons","buttons",1255256819),new cljs.core.PersistentVector(null, 1, 5, cljs.core.PersistentVector.EMPTY_NODE, [new cljs.core.PersistentArrayMap(null, 2, [new cljs.core.Keyword(null,"label","label",1116631654),"Download latest",new cljs.core.Keyword(null,"action","action",3885920680),(function (){lt.objs.platform.open_url.call(null,"http://www.lighttable.com");
return lt.objs.popup.remain_open.call(null);
})], null)], null)], null));
});
lt.objs.deploy.__BEH__check_deploy = (function __BEH__check_deploy(this$){if(lt.objs.deploy.is_newer_QMARK_.call(null,lt.objs.deploy.binary_version.call(null),new cljs.core.Keyword(null,"electron","electron",996780372).cljs$core$IFn$_invoke$arity$1(lt.objs.deploy.version)))
{return lt.objs.deploy.alert_binary_update.call(null);
} else
{return null;
}
});
lt.object.behavior_STAR_.call(null,new cljs.core.Keyword("lt.objs.deploy","check-deploy","lt.objs.deploy/check-deploy",1596260355),new cljs.core.Keyword(null,"reaction","reaction",4441361819),lt.objs.deploy.__BEH__check_deploy,new cljs.core.Keyword(null,"triggers","triggers",2516997421),new cljs.core.PersistentHashSet(null, new cljs.core.PersistentArrayMap(null, 1, [new cljs.core.Keyword(null,"deploy","deploy",3973538905),null], null), null));
lt.objs.deploy.__BEH__check_version = (function __BEH__check_version(this$){if(lt.objs.app.first_window_QMARK_.call(null))
{localStorage.fetchedVersion = null;
} else
{}
lt.objs.deploy.check_version.call(null);
return lt.util.js.every.call(null,lt.objs.deploy.version_timeout,lt.objs.deploy.check_version);
});
lt.object.behavior_STAR_.call(null,new cljs.core.Keyword("lt.objs.deploy","check-version","lt.objs.deploy/check-version",3501318508),new cljs.core.Keyword(null,"reaction","reaction",4441361819),lt.objs.deploy.__BEH__check_version,new cljs.core.Keyword(null,"desc","desc",1016984067),"App: Automatically check for updates",new cljs.core.Keyword(null,"triggers","triggers",2516997421),new cljs.core.PersistentHashSet(null, new cljs.core.PersistentArrayMap(null, 1, [new cljs.core.Keyword(null,"init","init",1017141378),null], null), null),new cljs.core.Keyword(null,"type","type",1017479852),new cljs.core.Keyword(null,"user","user",1017503549));
lt.object.tag_behaviors.call(null,new cljs.core.Keyword(null,"app","app",1014001043),new cljs.core.PersistentVector(null, 1, 5, cljs.core.PersistentVector.EMPTY_NODE, [new cljs.core.Keyword("lt.objs.deploy","check-deploy","lt.objs.deploy/check-deploy",1596260355)], null));
