<commit_before>'use strict'
# prevent window being garbage collected
mainWindow = undefined
installWindow = undefined
settingsWindow = undefined
kakaduInstalled = false
electron = require('electron')
dialog = electron.dialog
app = electron.app
which = require('which')
kdu_compress = 'kdu_compress'

fs = require('fs')
Configstore = require('configstore')
package_json = require('./package.json')
expand_home_dir = require('expand-home-dir')
iiif_conversion_dir = expand_home_dir('~/iiif_conversion')

settings = new Configstore(package_json.name)

onClosed = ->
  # dereference the window
  # for multiple windows store them in an array
  mainWindow = null
  return

checkWhich = ->
  which kdu_compress, (err, path) ->
    if err #open a window with instructions on installing kakadu binaries
      installWindow = createInstallWindow()
    else
      kakaduInstalled = true
      installWindow.close() if installWindow?
      installWindow = null
      mainWindow = createMainWindow()

createMainWindow = ->
  if !mainWindow?
    win = new (electron.BrowserWindow)(
      width: 800
      height: 900
      icon: './app/images/image-image.png')
    win.setMenu(null)
    win.loadURL("file://#{__dirname}/app/views/index.html")
    win.on 'closed', onClosed
    # win.output_dir = settings.get('output_dir')
    win.settings = settings
    win

createInstallWindow = ->
  win = new (electron.BrowserWindow)(
    width: 800
    height: 900
    # frame: false
    icon: './app/images/image-image.png')
  win.setMenu(null)
  win.loadURL("file://#{__dirname}/app/views/install.html")
  win.on 'closed', checkWhich
  win

openSettings = ->
  win = new (electron.BrowserWindow)(
    width: 400
    height: 200
    # frame: false
    icon: './app/images/image-image.png')
  win.setMenu(null)
  win.loadURL("file://#{__dirname}/app/views/settings.html")
  win.settings = settings
  # win.on 'closed', something
  win

# report crashes to the Electron project
require('crash-reporter').start()

# adds debug features like hotkeys for triggering dev tools and reload
require('electron-debug')({showDevTools: true})

ipc_main = require('electron').ipcMain
ipc_main.on('open-image', (event, arg) ->
  image_window = new (electron.BrowserWindow)(
    width: 1000
    height: 1000
    show: false
    icon: './app/images/image-image.png')
  image_window.setMenu(null)
  image_window.on 'closed', ->
    image_window = null
  image_window.loadURL(arg)
  image_window.show()
)

ipc_main.on('open-jp2', (event, arg) ->
  jp2_window = new (electron.BrowserWindow)(
    show: false
    width: 1000
    height: 1000
    icon: './app/images/image-image.png'
  )
  jp2_window.setMenu(null)
  jp2_window.on 'closed', ->
    jp2_window = null
  encoded_image_path = encodeURIComponent arg
  url = "file://#{__dirname}/app/views/openseadragon.html?id=#{encoded_image_path}"
  jp2_window.loadURL(url)
  jp2_window.show()
)

ipc_main.on 'retry-launch', (event, arg) ->
  checkWhich()

ipc_main.on 'open-settings', (event, arg) ->
  if !settingsWindow?
    settingsWindow = openSettings()

ipc_main.on 'open-mainwindow-if-not', (event, arg) ->
  checkWhich()

app.on 'window-all-closed', ->
  if process.platform != 'darwin'
    app.quit()

app.on 'activate', ->
  if !mainWindow && kakaduInstalled
    mainWindow = createMainWindow()

app.on 'ready', ->
  # create the output directory if it doesn't exist already
  if !settings.get('output_dir')
    openSettings()
  else
    fs.stat settings.get('output_dir'), (err, stats) ->
      # console.log [err, stats]
      if err<commit_msg>if settings have been closed allow it to be opened again<commit_after>'use strict'
# prevent window being garbage collected
mainWindow = undefined
installWindow = undefined
settingsWindow = undefined
kakaduInstalled = false
electron = require('electron')
dialog = electron.dialog
app = electron.app
which = require('which')
kdu_compress = 'kdu_compress'

fs = require('fs')
Configstore = require('configstore')
package_json = require('./package.json')
expand_home_dir = require('expand-home-dir')
iiif_conversion_dir = expand_home_dir('~/iiif_conversion')

settings = new Configstore(package_json.name)

checkWhich = ->
  which kdu_compress, (err, path) ->
    if err #open a window with instructions on installing kakadu binaries
      installWindow = createInstallWindow()
    else
      kakaduInstalled = true
      installWindow.close() if installWindow?
      installWindow = null
      mainWindow = createMainWindow()

createMainWindow = ->
  if !mainWindow?
    win = new (electron.BrowserWindow)(
      width: 800
      height: 900
      icon: './app/images/image-image.png')
    win.setMenu(null)
    win.loadURL("file://#{__dirname}/app/views/index.html")
    win.on 'closed', ->
      win = null
    # win.output_dir = settings.get('output_dir')
    win.settings = settings
    win

createInstallWindow = ->
  win = new (electron.BrowserWindow)(
    width: 800
    height: 900
    # frame: false
    icon: './app/images/image-image.png')
  win.setMenu(null)
  win.loadURL("file://#{__dirname}/app/views/install.html")
  win.on 'closed', checkWhich
  win

openSettings = ->
  win = new (electron.BrowserWindow)(
    width: 400
    height: 200
    # frame: false
    icon: './app/images/image-image.png')
  win.setMenu(null)
  win.loadURL("file://#{__dirname}/app/views/settings.html")
  win.settings = settings
  win.on 'closed', ->
    settingsWindow = null
  win

# report crashes to the Electron project
require('crash-reporter').start()

# adds debug features like hotkeys for triggering dev tools and reload
require('electron-debug')({showDevTools: true})

ipc_main = require('electron').ipcMain
ipc_main.on('open-image', (event, arg) ->
  image_window = new (electron.BrowserWindow)(
    width: 1000
    height: 1000
    show: false
    icon: './app/images/image-image.png')
  image_window.setMenu(null)
  image_window.on 'closed', ->
    image_window = null
  image_window.loadURL(arg)
  image_window.show()
)

ipc_main.on('open-jp2', (event, arg) ->
  jp2_window = new (electron.BrowserWindow)(
    show: false
    width: 1000
    height: 1000
    icon: './app/images/image-image.png'
  )
  jp2_window.setMenu(null)
  jp2_window.on 'closed', ->
    jp2_window = null
  encoded_image_path = encodeURIComponent arg
  url = "file://#{__dirname}/app/views/openseadragon.html?id=#{encoded_image_path}"
  jp2_window.loadURL(url)
  jp2_window.show()
)

ipc_main.on 'retry-launch', (event, arg) ->
  checkWhich()

ipc_main.on 'open-settings', (event, arg) ->
  if not settingsWindow?
    settingsWindow = openSettings()

ipc_main.on 'open-mainwindow-if-not', (event, arg) ->
  checkWhich()

app.on 'window-all-closed', ->
  if process.platform != 'darwin'
    app.quit()

app.on 'activate', ->
  if !mainWindow && kakaduInstalled
    mainWindow = createMainWindow()

app.on 'ready', ->
  # create the output directory if it doesn't exist already
  if !settings.get('output_dir')
    openSettings()
  else
    fs.stat settings.get('output_dir'), (err, stats) ->
      # console.log [err, stats]
      if err
{'fraction_non_alphanumeric': 0.13395506570580754, 'fraction_numerical': 0.011586830577928501, 'mean_word_length': 3.7092481703260147, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6905591', 'n_tokens_mistral': 2395, 'n_tokens_neox': 2292, 'n_words': 527}