<commit_before>param($installPath, $toolsPath, $package, $project)

function FindRootFolder {
    # worst case scenario, we dump this in the same folder as the solution file
    $solution_path = $dte.Solution.FullName
    $solution_dir = (get-item $solution_path).Directory.FullName

    Write-Host "Defaulting to $solution_dir if we can't find a source control folder"

    # best case scenario, we dump this in the root of the repository, which we get by looking for .git or .hg

    $root_dir = $null
    $current_dir = $toolsPath
    $depth_from_tool_path = 7 # how deep should we go?

    do {
	    $current_dir = (Get-Item $current_dir).Parent.FullName
        
        if ([IO.Directory]::Exists($current_dir) -eq $false)
        {
        	Write-Host "We can't go further, bailing out"
            break;
        }

        if ([IO.Directory]::Exists((Join-Path -Path $current_dir ".git")))
        {
            Write-Host "Found a .git folder at $current_dir!"
            $root_dir = $current_dir
        }

        if ([IO.Directory]::Exists((Join-Path -Path $current_dir ".hg")))
        {
            Write-Host "Found a .hg folder at $current_dir!"
            $root_dir = $current_dir
        }
	
	    $depth_from_tool_path--
    } while ($depth_from_tool_path -gt 0 -and $root_dir -eq $null)

    if ($root_dir -ne $null)  {
    	Write-Host "We found a source code directory, so we'll put the docs in the same location"
        return $root_dir
    } else { 
    	Write-Host "No source code folder found, using solution directory"
        return $solution_dir
    }
}

# get the "project" root folder
$project_root = (FindRootFolder);

Write-Host "Installing docs folder inside $project_root"

$docs_folder = Join-Path -Path $project_root "docs"
$metafile = Join-Path -Path $docs_folder ".duchess"
$version = $package.Version.Version.ToString()

# if this is the first time we run the package
if ([IO.Directory]::Exists($docs_folder) -eq $false) {
    # create folder
	$templatePath = Join-Path -Path $toolsPath "template\*"

	# populate folder with template contents
    New-Item -ItemType directory -Path $docs_folder
	Copy-Item -Path $templatePath -Destination $docs_folder -Recurse

	# dump the package version to a metafile
    $version | Out-File $metafile

	# open the file in Visual Studio
    $index = Join-Path -Path $docs_folder "index.md"
	$dte.ItemOperations.OpenFile($index)
}
else {
	# if we have a different version installed, this is an upgrade
	$installed_version = $null

	if ([IO.File]::Exists($metafile) -eq $false) {
		$installed_version = "i have no idea"
	} else {
		$installed_version = Get-Content $metafile | Out-String
	}

	if ($installed_version -ne $package) {
		# update the version 
		Remove-Item $metafile
		$version | Out-File $metafile	
	}
}

# TODO: hook in any other commands

# launch ze missiles
$launch_script = Join-Path -Path $toolsPath "_pretzel\Launch-Docs.ps1"
powershell -File $launch_script -DocsRoot $docs_folder<commit_msg>some better messages<commit_after>param($installPath, $toolsPath, $package, $project)

function FindRootFolder {
    # worst case scenario, we dump this in the same folder as the solution file
    $solution_path = $dte.Solution.FullName
    $solution_dir = (get-item $solution_path).Directory.FullName

    Write-Host "Defaulting to $solution_dir if we can't find a source control folder"

    # best case scenario, we dump this in the root of the repository, which we get by looking for .git or .hg

    $root_dir = $null
    $current_dir = $toolsPath
    $depth_from_tool_path = 7 # how deep should we go?

    do {
	    $current_dir = (Get-Item $current_dir).Parent.FullName
        
        if ([IO.Directory]::Exists($current_dir) -eq $false)
        {
        	Write-Host "We can't go further, bailing out"
            break;
        }

        if ([IO.Directory]::Exists((Join-Path -Path $current_dir ".git")))
        {
            Write-Host "Found a .git folder at $current_dir!"
            $root_dir = $current_dir
        }

        if ([IO.Directory]::Exists((Join-Path -Path $current_dir ".hg")))
        {
            Write-Host "Found a .hg folder at $current_dir!"
            $root_dir = $current_dir
        }
	
	    $depth_from_tool_path--
    } while ($depth_from_tool_path -gt 0 -and $root_dir -eq $null)

    if ($root_dir -ne $null)  {
    	Write-Host "We found a source code directory, so we'll put the docs in the same location"
        return $root_dir
    } else { 
    	Write-Host "No source code folder found, using solution directory"
        return $solution_dir
    }
}

function Setup-FolderStructure {
    Write-Host "First time setup of documentation"
}

function Upgrade-FolderStructure {
    Write-Host "Existing documentation folder already found"
}

# get the "project" root folder
$project_root = (FindRootFolder);

Write-Host "Installing docs folder inside $project_root"

$docs_folder = Join-Path -Path $project_root "docs"
$metafile = Join-Path -Path $docs_folder ".duchess"
$version = $package.Version.Version.ToString()

# if this is the first time we run the package
if ([IO.Directory]::Exists($docs_folder) -eq $false) {

    Setup-FolderStructure
    
    # create folder
	$templatePath = Join-Path -Path $toolsPath "template\*"

	# populate folder with template contents
    New-Item -ItemType directory -Path $docs_folder
	Copy-Item -Path $templatePath -Destination $docs_folder -Recurse | Out-Null

	# dump the package version to a metafile
    $version | Out-File $metafile

	# open the file in Visual Studio
    $index = Join-Path -Path $docs_folder "index.md"
	$dte.ItemOperations.OpenFile($index)

} else {  

	$installed_version = $null
	if ([IO.File]::Exists($metafile) -eq $true) {
		$installed_version = Get-Content $metafile | Out-String
    }

	if ($installed_version -ne $package) {
        
        Upgrade-FolderStructure

		# update the version stored in the docs folder
        if ([IO.File]::Exists($metafile) -eq $true) {
            Remove-Item $metafile
        }
		$version | Out-File $metafile	
	} else {
        Write-Host "No changes necessary, you've got the latest code"
    }
}

# TODO: hook in any other commands

# launch ze missiles
$launch_script = Join-Path -Path $toolsPath "_pretzel\Launch-Docs.ps1"
powershell -File $launch_script -DocsRoot $docs_folder
{'fraction_non_alphanumeric': 0.10601990835835046, 'fraction_numerical': 0.0009480170643071576, 'mean_word_length': 3.0012642225031607, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6929648', 'n_tokens_mistral': 2033, 'n_tokens_neox': 1931, 'n_words': 620}