docs: def run_init(device_type, args):
      """Initialize hardware-based GnuPG identity."""
      util.setup_logging(verbosity=args.verbose)
      log.warning('This GPG tool is still in EXPERIMENTAL mode, '
                  'so please note that the API and features may '
                  'change without backwards compatibility!')
  
      verify_gpg_version()
  
      # Prepare new GPG home directory for hardware-based identity
      device_name = os.path.basename(sys.argv[0]).rsplit('-', 1)[0]
      log.info('device name: %s', device_name)
      homedir = args.homedir
      if not homedir:
          homedir = os.path.expanduser('~/.gnupg/{}'.format(device_name))
  
      log.info('GPG home directory: %s', homedir)
  
      if os.path.exists(homedir):
          log.error('GPG home directory %s exists, '
                    'remove it manually if required', homedir)
          sys.exit(1)
  
      check_call(['mkdir', '-p', homedir])
      check_call(['chmod', '700', homedir])
  
      agent_path = util.which('{}-gpg-agent'.format(device_name))
  
      # Prepare GPG agent invocation script (to pass the PATH from environment).
      with open(os.path.join(homedir, 'run-agent.sh'), 'w') as f:
          f.write(r"""#!/bin/sh
  export PATH={0}
  {1} \
  -vv \
  --pin-entry-binary={pin_entry_binary} \
  --passphrase-entry-binary={passphrase_entry_binary} \
  --cache-expiry-seconds={cache_expiry_seconds} \
  $*
  """.format(os.environ['PATH'], agent_path, **vars(args)))
      check_call(['chmod', '700', f.name])
      run_agent_script = f.name
  
      # Prepare GPG configuration file
      with open(os.path.join(homedir, 'gpg.conf'), 'w') as f:
          f.write("""# Hardware-based GPG configuration
  agent-program {0}
  personal-digest-preferences SHA512
  default-key \"{1}\"
  """.format(run_agent_script, args.user_id))
  
      # Prepare a helper script for setting up the new identity
      with open(os.path.join(homedir, 'env'), 'w') as f:
          f.write("""#!/bin/bash
  set -eu
  export GNUPGHOME={0}
  COMMAND=$*
  if [ -z "${{COMMAND}}" ]
  then
      ${{SHELL}}
  else
      ${{COMMAND}}
  fi
  """.format(homedir))
      check_call(['chmod', '700', f.name])
  
      # Generate new GPG identity and import into GPG keyring
      pubkey = write_file(os.path.join(homedir, 'pubkey.asc'),
                          export_public_key(device_type, args))
      verbosity = ('-' + ('v' * args.verbose)) if args.verbose else '--quiet'
      check_call(keyring.gpg_command(['--homedir', homedir, verbosity,
                                      '--import', pubkey.name]))
  
      # Make new GPG identity with "ultimate" trust (via its fingerprint)
      out = check_output(keyring.gpg_command(['--homedir', homedir,
                                              '--list-public-keys',
                                              '--with-fingerprint',
                                              '--with-colons']))
      fpr = re.findall('fpr:::::::::([0-9A-F]+):', out)[0]
      f = write_file(os.path.join(homedir, 'ownertrust.txt'), fpr + ':6\n')
      check_call(keyring.gpg_command(['--homedir', homedir,
                                      '--import-ownertrust', f.name]))
  
      # Load agent and make sure it responds with the new identity
      check_call(keyring.gpg_command(['--homedir', homedir,
                                      '--list-secret-keys', args.user_id]))