
import spawn from '../spawn'
import sf from '../sf'

import common from './common'

import tmp from 'tmp'

// note: itch-player's password is not a secret, as everything itch-player
// owns should be accessible by other users as well (but not the other
// way around)
const USER = 'itch-player'
const PASSWORD = 'salt'

export async function check () {
  const errors = []
  const needs = []

  // TODO: this doesn't check that the password is valid, unfortunately
  const userCheck = await spawn.exec({command: 'net.exe', args: ['user', 'itch-player']})
  if (userCheck.code !== 0) {
    needs.push({
      type: 'user',
      err: userCheck.err,
      code: userCheck.code
    })
  }

  return {errors, needs}
}

export async function install (opts, needs) {
  return await common.tendToNeeds(opts, needs, {
    user: async function () {
      const lines = []
      // in case the user was incorrectly setup
      lines.push(`net user ${USER} ${PASSWORD} /add`)
      // if we don't do this, it shows as a login user
      lines.push(`net localgroup Users ${USER} /delete`)
      await adminRunScript(lines)
    }
  })
}

export async function uninstall (opts) {
  return {errors: []}
}

async function adminRunScript (lines) {
  const contents = lines.join('\r\n')
  const tmpObj = tmp.fileSync({postfix: '.bat'})
  sf.writeFile(tmpObj.name, contents)

  console.log(`running batch script:\r\n${contents}`)

  const res = await spawn.exec({
    command: 'elevate.exe',
    args: ['cmd.exe', '/c', tmpObj.name]
  })

  if (process.env.KEEP_MESS_AROUND === '1') {
    console.log(`keeping temp batch script at ${tmpObj.name}`)
  } else {
    tmpObj.removeCallback()
  }

  if (res.code !== 0) {
    throw new Error(`adminRunScript failed with code ${res.code}. out = ${res.out}\n, err = ${res.err}\n`)
  }

  return {out: res.out}
}

export default {check, install, uninstall}

@@ -830,24 +830,294 @@
 nction () %7B%0A
+      const res = await spawn.exec(%7Bcommand: 'elevate.exe', args: %5B'--print-users-sid'%5D%7D)%0A      if (res.code !== 0) %7B%0A        throw new Error(%60print-users-sid failed with code $%7Bres.code%7D. out = $%7Bres.out%7D, err = $%7Bres.err%7D%60)%0A      %7D%0A      const group = res.out.trim()%0A%0A
       const 
@@ -1127,16 +1127,16 @@
 es = %5B%5D%0A
-
       //
@@ -1321,13 +1321,16 @@
 oup 
-Users
+$%7Bgroup%7D
  $%7BU

Use elevate to figure out localized version of 'Users' group (non-English windows)