#!/usr/bin/python3.11

# Copyright (C) 2010 Novell, Inc. All rights reserved.
#
# This work is subject to U.S. and international copyright laws and treaties.
# No part of this work may be used, practiced, performed, copied, distributed,
# revised, modified, translated, abridged, condensed, expanded, collected,
# compiled, linked, recast, transformed or adapted without the prior written
# consent of Novell, Inc. Any use or exploitation of this work without authorization
# could subject the perpetrator to criminal and civil liability.

import os
import sys
sys.path.append( "/opt/novell/ncs/bin" )
import clstrlibss

def usage():
    sys.stderr.write( "Usage: " + os.path.basename( sys.argv[0] ) + " username|password output_file_name\n" )

#############################################################################
#
# Function: main
#
# Returns: Nothing.
#
# Comments:
#
def main(argv):

    if 2 > len( argv ):
        usage()
        sys.exit(22)

    if "password" != str.lower( argv[0] ) and "username" != str.lower( argv[0] ):
        usage()
        sys.exit(1)

    try:
        os.umask( 0o177 )
        fd = open( argv[1], "w" )
    except:
        sys.exit( 2 )

    try:
        ss = clstrlibss.load()
        if "" == ss.adminDn:
            rc = 6
        else:
            if  "password" == str.lower( argv[0] ):
                fd.write( str( ss.adminPw ) + "\n" )
            else:
                fd.write( str( ss.adminDn ) + "\n" )
            rc = 0
    except:
        rc = 5

    fd.close()
    sys.exit(rc)


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))

