@python -x "%~f0" %* & goto :EOF


import os
import sys
import string
import DCOracle
import win32net


def main():

    # Check if we nbeed to show the help
    if not len(sys.argv) in [2, 3]:
        print "FixOnlineStatus username/password@connectionstring"
        sys.exit()
    
    # Check if we have are running in noupdate or not
    if len(sys.argv) == 3:
        if sys.argv[2] == "--noupdate":
            print "Running in NOUPDATE mode. No changes will be made to the database."
            noupdate = 1
        else:
            noupdate = 0
    else:
        noupdate = 0

    # Get the connectionstring
    connectstring = sys.argv[1]

    # Connect to the database
    print 'Connecting to database using ' + connectstring + '...',
    try:
        dbconn = DCOracle.Connect(connectstring)
        print 'Connected!\n'
    except:
        print 'Failed!\n'
        sys.exit()

    # Get a database cursor
    dbcur = dbconn.cursor()

    # SQL statement to get the jobs with the unknown online status
    sqlJobPhysLoc = ""
    sqlJobPhysLoc = sqlJobPhysLoc + "select JCKDNAME, "
    sqlJobPhysLoc = sqlJobPhysLoc + "'\\\\' || JCKDROOTFOLDERSERVERNAME || '\\' || JCKDROOTFOLDERSHARENAME ||"
    sqlJobPhysLoc = sqlJobPhysLoc + "'\\' || JCKDROOTFOLDERPATHNAME as JacketFullPath "
    sqlJobPhysLoc = sqlJobPhysLoc + "from DBCS_Jacket where JCKDONLINESTATECD = 0 order by JCKDNAME"
    
    # Execute the query
    dbcur.execute(sqlJobPhysLoc)

    # Fetch the results
    joblist = dbcur.fetchall()

    # Check if we need to update something
    if len(joblist) != 0:

        # Create the logfile
        fLog = open("FixOnlineStatus.log", "w")

        # Print the header
        print string.ljust("Job name", 40) + "Status"
        print ("-" * 60)

        # Write header to the logfile
        fLog.write(string.ljust("Job name", 40) + "Status\n")
        fLog.write(("-" * 60) + "\n")

        # Iterate over the jobs
        for jobspec in joblist:

            # Check if the jobfolder exists
            if os.path.exists(jobspec[1]):

                # Check if we have a system and userdefinedfolders in the jobfolder
                if os.path.exists(jobspec[1] + "\\System") and os.path.exists(jobspec[1] + "\\UserDefinedFolders"):

                    # Set the job to online
                    print string.ljust(jobspec[0], 40) + "ONLINE"
                    fLog.write(string.ljust(jobspec[0], 40) + "ONLINE\n")
                    sqlSetJobOnline = "update DBCS_Jacket set JCKDONLINESTATECD = 1 where JCKDNAME = '" + jobspec[0] + "'"

                    # Online update the database in not in noupdate
                    if not noupdate:
                        dbcur.execute(sqlSetJobOnline)

                # Looks like this is just an empty jobfolder, set it to offline
                else:

                    # Set the job to offline
                    print string.ljust(jobspec[0], 40) + "OFFLINE"
                    fLog.write(string.ljust(jobspec[0], 40) + "OFFLINE\n")
                    sqlSetJobOffline = "update DBCS_Jacket set JCKDONLINESTATECD = 2 where JCKDNAME = '" + jobspec[0] + "'"

                    # Online update the database in not in noupdate
                    if not noupdate:
                        dbcur.execute(sqlSetJobOffline)

            # Jobfolder was not found
            else:

                    # Set the job to offline
                    print string.ljust(jobspec[0], 40) + "OFFLINE"
                    fLog.write(string.ljust(jobspec[0], 40) + "OFFLINE\n")
                    sqlSetJobOffline = "update DBCS_Jacket set JCKDONLINESTATECD = 2 where JCKDNAME = '" + jobspec[0] + "'"

                    # Online update the database in not in noupdate
                    if not noupdate:
                        dbcur.execute(sqlSetJobOffline)

        # Commit all pending transactions
        if not noupdate:
            dbconn.commit()

        # Close the logfile
        fLog.close()

    # No jobs found with the unknown online status
    else:
        print "Found no jobs with the unknown online status"


if __name__ == '__main__':
    main()