Use Windows.pkg
Use cHtmlHelp.pkg
Use cApplication.pkg
Use akefs.pkg
Use ShellExecute.pkg
Use Batchdd.pkg

Define C_VDFREPOSITORY for "http://192.168.50.9/svn/vdf" // Must be URL encoded.
Define C_VDFREP_MAINDEVDIR for "/Main"  // Main developement line (i.e. /trunk/)

Object oHtmlHelp is a cHtmlHelp
End_Object

Object oApplication is a cApplication
    Set pbPreserveEnvironment to False
    Set peHelpType to htHtmlHelp
End_Object

Object oSWS is a cIniFile
End_Object

Object oMain is a BusinessProcess
    
    Property String psBatFile

    Set Display_Error_State to True
    Set Process_Caption to "Repository modification"
    Set Process_Message to "Adding workspace to the repository..."
    Set Process_Title to "Workspace creation on Subversion repository"

    // Start procedure
    Procedure OnProcess
        Handle hCmdLine
        Integer iMaxArguments iPos
        String sArgument sSWSDirectory sSWSfile sOrgCurDir sTmpFile sBatFile
        Boolean bFound bRemoved bOk
        Handle hArgs
        
        Send Update_Status "Checking workspace configuration file..."
        Get phoCommandLine of oApplication to hCmdLine
        Get CountOfArgs of hCmdLine to iMaxArguments
        
        // Test
//        Get phoArgs of hCmdLine to hArgs
//        Set Value of hArgs 0 to "d:\Udvk140\aketest\aketest.sws"
//        Get CountOfArgs of hCmdLine to iMaxArguments

        If (iMaxArguments > 0) Begin
            Get Argument of hCmdLine 1 to sArgument
            // Send Info_Box sArgument
            Move (Pos(".sws", sArgument)) to iPos
            If (iPos > 0) Begin
                Move sArgument to sSWSfile
                Get FileExists sSWSfile to bFound
                If (bFound) Begin
                    Move (ExtractFilePath(sSWSfile)) to sSWSDirectory
                    Get_Current_Directory to sOrgCurDir
                    // Set_Directory sSWSDirectory
                    Move (SetCurrentDirectory(sSWSDirectory)) to bOk
                    Get GetTempFileName "" "" to sTmpfile
                    Move (sTmpfile + ".bat") to sBatFile
                    Get FileRename sTmpFile sBatFile to bOk
                    Set psBatFile to sBatFile
                    Send DoAddToRepository sSWSfile
                    Get FileDelete sBatFile to bRemoved
                    // Set_Directory sOrgCurDir
                    Move (SetCurrentDirectory(sOrgCurDir)) to bOk
                End
                Else Error DFERR_PROGRAM "The passed workspace configuration file does not exist."
            End
            Else Error DFERR_PROGRAM "The passed file is not a workspace configuration file."
        End
        Else Error DFERR_PROGRAM "No workspace configuration file has been defined."
        
    End_Procedure
    
    
    // Add the passed workspace to a repository.
    // Current working directory has to be set to where the sSWSfile is located.
    Procedure DoAddToRepository String sSWSfile
        Handle hWorkspace 
        Integer iResult
        String sError sCmd sWorkspaceName sWorkspaceDesc sWsFile sBuffer
        String sRepositoryHome sHomeDir sCurPath sBatFile sCurDir sSWSPath
        String sURL
        Boolean bFound bDeleted
        
        Send Update_Status "Creating script file"
        Get psBatFile to sBatFile

        Move (ExtractFilePath(sSWSfile)) to sSWSPath

        // Find the workspace file.
        Set psFileName of oSWS to sSWSfile
        Get KeyExists of oSWS "WorkspacePaths" "ConfigFile" to bFound
        If (not(bFound)) Begin
            Error DFERR_PROGRAM "ConfigFile definition not found in the workspace configuration file."
            Procedure_Return
        End
        Get ReadString of oSWS "WorkspacePaths" "ConfigFile" "" to sWsFile
        Move (sSWSPath + sWsFile) to sWsFile
        
        // Try to open the workspace.
        Get phoWorkspace of oApplication to hWorkspace
        Send DoClearPaths of hWorkspace
        Get OpenWorkspaceFile of hWorkspace sWsFile to iResult
        If (iResult <> wsWorkspaceOpened) Begin
            Get OpenWorkspaceErrorMessage of hWorkspace iResult to sError
            Send Stop_Box sError "Error Opening Workspace"
            Procedure_Return
        End        
        
        // Prepare a new entry in the repository for the new workspace.
        // Send Update_Status "Preparing workspace for adding it to the in repostory"
        Move (ExtractFileName(sSWSfile)) to sBuffer
        Move (sBuffer + (Character(0))) to sBuffer
        Move (PathRemoveExtension(AddressOf(sBuffer))) to iResult
        Move (CString(sBuffer)) to sWorkspaceName
        Get psDescription of hWorkspace to sWorkspaceDesc
        Move ("mkdir ") to sCmd
        Move (sCmd + '-m "Preparing workspace (' + sWorkspaceDesc  +') for import." ') to sCmd
        Move ('"' + C_VDFREPOSITORY + "/" + sWorkspaceName + '"') to sURL
        Move (sCmd + sURL) to sCmd
        Send DoSVNCommand sCmd
        
        // Make main production line
        // Send Update_Status "Creating main production line"
        Move (C_VDFREPOSITORY + "/" + sWorkspaceName + C_VDFREP_MAINDEVDIR) to sRepositoryHome
        Move ("mkdir ") to sCmd
        Move (sCmd + '-m "Main production line." ' + '"' + sRepositoryHome + '"') to sCmd
        Send DoSVNCommand sCmd
        
        // Get the new entry from the repository to the workspace so it becomes a "working copy".
        // Send Update_Status "Making a working copy"
        Get psHome of hWorkspace to sHomeDir
        If (Right(sHomeDir, 1) = "\") Move (Left(sHomeDir, (Length(sHomeDir) - 1))) to sHomeDir
        Move ("checkout ") to sCmd
        Move (sCmd + '"' + sRepositoryHome + '" "' + (ToANSI(sHomeDir)) + '"') to sCmd
        Send DoSVNCommand sCmd

        // Add the workspace
        // Send Update_Status "Adding the workspace to the repository"

        Get psAppSrcPath of hWorkspace to sCurPath
        Send DoAddPathToRepository sCurPath

        Get psBitmapPath of hWorkspace to sCurPath
        Send DoAddPathToRepository sCurPath

        Get psDataPath of hWorkspace to sCurPath
        Send DoAddPathToRepository sCurPath

        Get psDdSrcPath of hWorkspace to sCurPath
        Send DoAddPathToRepository sCurPath
        
        Get psHelpPath of hWorkspace to sCurPath
        Send DoAddPathToRepository sCurPath
        
        Get psAppHtmlPath of hWorkspace to sCurPath
        Send DoAddPathToRepository sCurPath
        
        // Only add the IdeSrc directory but not its contents.
        Get psIdeSrcPath of hWorkspace to sCurPath
        Get PathAtIndex of hWorkspace sCurPath 1 to sCurPath    // Only integration to one directory here.
        Move ('add --depth=empty "' + sCurPath + '"') to sCmd
        Send DoSVNCommand sCmd
        
        // Tell the repository to ignore its contents.
        
        // Only add the IdeSrc directory.
        Get psProgramPath of hWorkspace to sCurPath
        Get PathAtIndex of hWorkspace sCurPath 1 to sCurPath    // Only integration to one directory here.
        Move ('add --depth=empty "' + sCurPath + '"') to sCmd
        Send DoSVNCommand sCmd
        
        // Add the workspace files
        Move ('add --depth=empty "' + sCurPath + '\*.ws"') to sCmd
        Send DoSVNCommand sCmd
        
        // Ignore these file extentions in the programs directory
        // *.dbg, *.exe
        
        // Add the workspace configuration file
        Move ('add --depth=empty "' + sSWSfile + '"') to sCmd
        Send DoSVNCommand sCmd        

        // Commit the whole thing.
        // Send Update_Status "Committing the import" 
        Move ('commit -m "Import of workspace files."') to sCmd
        Send DoSVNCommand sCmd

        
        Send DoWriteToBatFile "pause"
        
        Send Update_Status "Adding the workspace to the repository"
        Runprogram Shell Wait sBatFile ""
        
    End_Procedure
    
    
    Procedure DoWriteToBatFile String sLine
        String sBatFile
        Integer iFile
        Get Seq_New_Channel to iFile
        Get psBatFile to sBatFile
        Append_Output channel iFile sBatFile
        Writeln channel iFile sLine
        Close_Output channel iFile
        Send Seq_Release_Channel iFile
    End_Procedure

    
    // Execute a SVN dos command with sCommand as parameter.
    Procedure DoSVNCommand String sCommand
        Send DoWriteToBatFile ("svn " + sCommand)
    End_Procedure
    
    
    // Add all direcotories from the collected path to the repository
    Procedure DoAddPathToRepository String sPath
        Integer iMaxPath iCurPath
        Handle hWorkspace
        String sCurPath sCmd
        Get phoWorkspace of oApplication to hWorkspace
        Get CountOfPaths of hWorkspace sPath to iMaxPath
        For iCurPath from 1 to iMaxPath
            Get PathAtIndex of hWorkspace sPath iCurPath to sCurPath
            Move ('add "' + sCurPath + '"') to sCmd
            Send DoSVNCommand sCmd
        Loop
    End_Procedure
    
    
End_Object

Send DoProcess of oMain

