const fs = require('fs');
const client = require('scp2');

const folder = './dist/';
const target = process.argv[2];

if (!target){
  console.error('No target defined');
  process.exit(0);
}

const config = JSON.parse(fs.readFileSync('.deploy.json', 'utf8'));
const scpConfig = {
  'darkly': {
    port: 22,
    host: 'bilbo.the-reincarnation.com',
    username: config['username'],
    privateKey: require("fs").readFileSync(config['privateKey']),
    path: '/var/www/recode/html/assets/'
  },
  'classic': {
    port: 22,
    host: 'bilbo.the-reincarnation.com',
    username: config['username'],
    privateKey: require("fs").readFileSync(config['privateKey']),
    path: '/var/www/recode/html/assets/classic/'
  }
};

fs.readdir(folder, (err, files) => {
  if (err){
    console.error(err);
  } else {
    files.forEach(filename => {
      console.log('Uploading ' + filename);
      client.scp(folder + filename, scpConfig[target], (err) => {
         if(err){
           console.error(err);
         } else {
           console.log('Upload ' + filename + ' complete');
         }
      });
    });
  }
});
