#!/usr/bin/env node
/**
 * Copyright 2017 Google Inc. All rights reserved.
 * Modifications copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const path = require('path');
const {Registry} = require('../lib/utils/registry');
const fs = require('fs');
const protocolGenerator = require('./protocol-types-generator');
const {execSync} = require('child_process');

const SCRIPT_NAME = path.basename(__filename);
const ROOT_PATH = path.resolve(path.join(__dirname, '..'));

function usage() {
  return `
usage: ${SCRIPT_NAME} <browser> <revision>

Roll the <browser> to a specific <revision> and generate new protocol.
Supported browsers: chromium, firefox, webkit, ffmpeg.

Example:
  ${SCRIPT_NAME} chromium 123456
`;
}

(async () => {
  // 1. Parse CLI arguments
  const args = process.argv.slice(2);
  if (args.some(arg => arg === '--help')) {
    console.log(usage());
    process.exit(1);
  } else if (args.length < 1) {
    console.log(`Please specify the browser name, e.g. 'chromium'.`);
    console.log(`Try running ${SCRIPT_NAME} --help`);
    process.exit(1);
  } else if (args.length < 2) {
    console.log(`Please specify the revision`);
    console.log(`Try running ${SCRIPT_NAME} --help`);
    process.exit(1);
  }
  const browserName = args[0].toLowerCase();
  if (!['chromium', 'firefox', 'webkit', 'ffmpeg'].includes(browserName)) {
    console.log(`Unknown browser "${browserName}"`);
    console.log(`Try running ${SCRIPT_NAME} --help`);
    process.exit(1);
  }
  const revision = args[1];
  console.log(`Rolling ${browserName} to ${revision}`);

  // 2. Update browsers.json.
  console.log('\nUpdating browsers.json...');
  const browsersJSON = require(path.join(ROOT_PATH, 'browsers.json'));
  browsersJSON.browsers.find(b => b.name === browserName).revision = String(revision);
  fs.writeFileSync(path.join(ROOT_PATH, 'browsers.json'), JSON.stringify(browsersJSON, null, 2) + '\n');

  // 3. Download new browser.
  console.log('\nDownloading new browser...');
  const { installBrowsersWithProgressBar } = require('../lib/install/installer');
  await installBrowsersWithProgressBar();

  // 4. Generate types.
  console.log('\nGenerating protocol types...');
  const executablePath = new Registry(ROOT_PATH).executablePath(browserName);
  await protocolGenerator.generateProtocol(browserName, executablePath).catch(console.warn);

  // 5. Update docs.
  console.log('\nUpdating documentation...');
  try {
    process.stdout.write(execSync('npm run --silent doc'));
  } catch (e) {
  }

  console.log(`\nRolled ${browserName} to ${revision}`);
})();

@@ -1211,16 +1211,59 @@
 , ffmpeg
+, firefox-stable, webkit-technology-preview
 .%0A%0AExamp
@@ -1832,110 +1832,204 @@
 wser
-Name = args%5B0%5D.toLowerCase();%0A  if (!%5B'chromium', 'firefox', 'webkit', 'ffmpeg'%5D.includes(browserName)
+sJSON = require(path.join(ROOT_PATH, 'browsers.json'));%0A  const browserName = args%5B0%5D.toLowerCase();%0A  const descriptor = browsersJSON.browsers.find(b =%3E b.name === browserName);%0A  if (!descriptor
 ) %7B%0A
@@ -2327,134 +2327,18 @@
 ;%0A  
-const browsersJSON = require(path.join(ROOT_PATH, 'browsers.json'));%0A  browsersJSON.browsers.find(b =%3E b.name === browserName)
+descriptor
 .rev
@@ -2465,24 +2465,63 @@
 ) + '%5Cn');%0A%0A
+  if (descriptor.installByDefault) %7B%0A  
   // 3. Down
@@ -2538,16 +2538,18 @@
 rowser.%0A
+  
   consol
@@ -2583,24 +2583,26 @@
 owser...');%0A
+  
   const %7B in
@@ -2671,16 +2671,18 @@
 ller');%0A
+  
   await 
@@ -2712,24 +2712,26 @@
 ressBar();%0A%0A
+  
   // 4. Gene
@@ -2742,16 +2742,18 @@
  types.%0A
+  
   consol
@@ -2793,16 +2793,18 @@
 s...');%0A
+  
   const 
@@ -2873,16 +2873,18 @@
 rName);%0A
+  
   await 
@@ -2965,24 +2965,26 @@
 ole.warn);%0A%0A
+  
   // 5. Upda
@@ -2992,16 +2992,18 @@
 e docs.%0A
+  
   consol
@@ -3044,16 +3044,18 @@
 ');%0A
+  
   try %7B%0A
     
@@ -3050,16 +3050,18 @@
   try %7B%0A
+  
     proc
@@ -3112,16 +3112,18 @@
 doc'));%0A
+  
   %7D catc
@@ -3132,18 +3132,23 @@
 (e) %7B%0A  
+  %7D%0A  
 %7D
-%0A
 %0A  conso

update `//utils/roll_browser.js` script to roll anything (#6279)