'use strict';

const { execSync } = require('child_process');
const stdout = process.stdout;
const stderr = process.stderr;
const path = require('path');
const basedir = path.resolve(__dirname);
const libstorj = require('./package.json').libstorj;
const releases = libstorj.releases;

let installed = true;
try {
  execSync('pkg-config --exists libstorj');
} catch(e) {
  installed = false;
}

if (installed) {
  stdout.write(`Skipping download of libstorj, already installed.\n`);
  process.exit(0);
}

const arch = process.arch;
const platform = process.platform;
const baseUrl = libstorj.baseUrl;

let checksum = null;
let filename = null;
let sha256sum = (platform === 'darwin') ? 'shasum -a 256' : 'sha256sum';

for (var i = 0; i < releases.length; i++) {
  if (releases[i].arch === arch && releases[i].platform === platform) {
    filename = releases[i].filename;
    checksum = releases[i].checksum;
  }
}

if (!filename) {
  stderr.write(`Unable to download libstorj for platform: ${platform} and arch: ${arch}\n`);
  process.exit(1);
}

const url = baseUrl + '/' + filename;
const target = path.resolve(basedir, './' + filename);
const download = `curl --location --fail --connect-timeout 120 --retry 3 -o "${target}" "${url}"`
const extract = `tar --verbose -xf ${target}`;
const hasher = `${sha256sum} ${target} | awk '{print $1}'`

stdout.write(`Downloading libstorj \n  from: ${url} \n  to: ${target}\n`);
execSync(download);

const hashbuf = execSync(hasher);
const hash = hashbuf.toString().trim();
if (hash === checksum) {
  stdout.write(`Verified libstorj: \n  file: ${target}\n  hash: ${checksum}\n`);
} else {
  stderr.write(`Unable to verify libstorj release: ${target} \n  expect: ${checksum}\n  actual: ${hash}\n`);
  process.exit(1);
}

stdout.write(`Extracting target: ${target}\n`);
execSync(extract);

process.exit(0);

@@ -143,24 +143,50 @@
 re('path');%0A
+const fs = require('fs');%0A
 const basedi
@@ -1363,16 +1363,123 @@
  $1%7D'%60%0A%0A
+if (fs.existsSync(target)) %7B%0A  stdout.write(%60Already downloaded libstorj %5Cn  at: $%7Btarget%7D%5Cn%60);%0A%7D else %7B%0A  
 stdout.w
@@ -1537,32 +1537,34 @@
  $%7Btarget%7D%5Cn%60);%0A
+  
 execSync(downloa
@@ -1566,16 +1566,18 @@
 wnload);
+%0A%7D
 %0A%0Aconst 

Check if download already exists