import {app, autoUpdater, ipcMain} from 'electron';
import isDev from 'electron-is-dev';
import ms from 'ms';
import {log} from '../common/logger';
import reporter from '../common/reporter';

const FEED_URL = `https://kap-updates.now.sh/update/osx/${app.getVersion()}`;

function createInterval() {
  return setInterval(() => {
    autoUpdater.checkForUpdates();
  }, ms('30m'));
}

let manualCheckTimeout;

function init(window) {
  if (isDev) {
    return;
  }

  autoUpdater.setFeedURL(FEED_URL);

  setTimeout(() => {
    log('checking');
    autoUpdater.checkForUpdates();
  }, ms('5s')); // At this point the app is fully started and ready for everything

  let intervalId = createInterval();

  autoUpdater.on('update-available', () => {
    clearTimeout(manualCheckTimeout);
    clearInterval(intervalId);
    intervalId = undefined;
    log('update available, starting download');
  });

  autoUpdater.on('update-downloaded', () => {
    log('update downloaded, will notify the user');
    window.webContents.send('update-downloaded');
  });

  ipcMain.on('install-update', () => {
    autoUpdater.quitAndInstall();
  });

  autoUpdater.on('error', err => {
    if (intervalId === undefined) { // If the error occurred during the download
      intervalId = createInterval();
    }

    log('Error fetching updates', err);
    reporter.report(err);
  });
}

// The `callback` will be called if no update is available at the moment
function checkForUpdates(callback) {
  manualCheckTimeout = setTimeout(() => {
    callback();
  }, ms('10s'));
}

exports.init = init;
exports.checkForUpdates = checkForUpdates;

@@ -185,16 +185,88 @@
 rter';%0A%0A
+const URL_APP_NAME = app.getName() === 'Kap Beta' ? 'kap-beta' : 'kap';%0A
 const FE
@@ -283,19 +283,31 @@
 https://
-kap
+$%7BURL_APP_NAME%7D
 -updates
@@ -325,11 +325,13 @@
 ate/
+mac
 os
-x
 /$%7Ba

Add beta update functionality