#!/usr/bin/env python3
import optparse
import sys

# Find right directory when running from source tree
sys.path.insert(0, "bin/python")


import samba
from samba import getopt as options
from samba import NTSTATUSError
from samba.credentials import Credentials
parser = optparse.OptionParser("machineaccountpw")
sambaopts = options.SambaOptions(parser)
parser.add_option_group(sambaopts)
parser.add_option_group(options.VersionOptions(parser))
opts, args = parser.parse_args()

if len(args) != 0:
    parser.print_usage()
    sys.exit(1)

try:
    lp_ctx = sambaopts.get_loadparm()
except RuntimeError as error:
    print("Unable to load smb.conf %s: %s" % (sambaopts.get_loadparm_path(),
                                              error),
          file=sys.stderr)
    sys.exit(1)

creds = Credentials()

creds.guess(lp_ctx)
try:
    creds.set_machine_account(lp_ctx)
except NTSTATUSError as error:
    print("Failed to find a stored machine account credential on this system: %s" \
          % error.args[1],
          file=sys.stderr)
    sys.exit(1)

print(creds.get_password())
