# Follow this Instructions to install the gbase Add-in
  # 1. Copy this code (Select All : Ctrl+A -> Ctrl+V)
  # 2. Open PowerShell with Administrator Rights
  # 3. Paste This code and HIT ENTER!




  # Requires -RunAsAdministrator
param([switch]$Elevated)

function Test-Admin {
    $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
    $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}

if ((Test-Admin) -eq $false)  {
    if ($elevated) {
        # tried to elevate, did not work, aborting
    } else {
        Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
    }
    exit
  }

Write-Host "Jay Swaminarayan!"
Write-Host "................."
Write-Host "........................................................................"
Write-Host ".................bbbb..................................................."
Write-Host ".................bbbb..................................................."
Write-Host ".................bbbb..................................................."
Write-Host ".................bbbb..................................................."
Write-Host "....ggggggggggg..bbbbbbbbbbb.....aaaaaaaa.....ssssssss......eeeeeeee...."
Write-Host "...gggggggggggg..bbbbbbbbbbbb...aaaaaaaaaa...ssssssssss...eeeeeeeeeee..."
Write-Host "..ggggggggggggg..bbbbbbbbbbbbb.aaaaaaaaaaaa.ssssssssssss..eeeeeeeeeee..."
Write-Host "..ggggggggggggg..bbbbbbbbbbbbb.aaaaa..aaaaa.sssss..sssss.seeeee.eeeeee.."
Write-Host ".gggggg...ggggg..bbbbb...bbbbb.......aaaaaa.sssssssss....seeee...eeeee.."
Write-Host ".ggggg....ggggg..bbbbb...bbbbb...aaaaaaaaaa.sssssssssss..seeeeeeeeeeee.."
Write-Host ".ggggg....ggggg..bbbb....bbbbb.aaaaaaaaaaaa..sssssssssss.seeeeeeeeeeee.."
Write-Host ".ggggg....ggggg..bbbbb...bbbbb.aaaaaaaaaaaa...sssssssssssseeee.........."
Write-Host ".gggggg..gggggg..bbbbb...bbbbbbaaaaa..aaaaa......ssssssssseeee.........."
Write-Host "..ggggggggggggg..bbbbbbbbbbbbbbaaaa..aaaaaa.sssss...sssssseeeeeeeeeeee.."
Write-Host "..ggggggggggggg..bbbbbbbbbbbbbbaaaaaaaaaaaa.ssssss.ssssss.eeeeeeeeeee..."
Write-Host "...gggggggggggg..bbbbbbbbbbbb..aaaaaaaaaaaa..sssssssssss..eeeeeeeeeee..."
Write-Host "....ggggggggggg..bbbbbbbbbbb....aaaaaaaaaaa...sssssssss.....eeeeeeee...."
Write-Host "..........ggggg........................................................."
Write-Host "..gggggg.gggggg........................................................."
Write-Host "..gggggggggggg.........................................................."
Write-Host "..gggggggggggg.........................................................."
Write-Host "...gggggggggg..........................................................."
Write-Host "........................................................................"
Write-Host ""
Write-Host "gbase Add-In Installation Begins..."
Write-Host ""

$dest = "c:\gbase-addins\excel"
$share = "gbase-Addins"

Write-Host "Creating the catalog share..."

New-Item $dest -ItemType Directory
New-SMBShare -Name $share -Path $dest -FullAccess Administrators -ReadAccess Users
# Source URL
$url = "https://o365.gurukul.org/excel-manifest.xml"
# Destation file
# Download the file
Write-Host ""
Write-Host "Downloading the Addin..."
Invoke-WebRequest -Uri $url -OutFile $dest"/excel-manifest.xml"

Write-Host ""
Write-Host "Adding Addin share to Trusted Catalog..."

# Set variables to indicate value and key to set
$guid = '1fb9f8f2-66a9-4704-8871-75e7dca236fd'
$hostn = hostname
$RegistryPath = 'HKCU:\SOFTWARE\Microsoft\Office\16.0\WEF\TrustedCatalogs\{' + $guid +'}'

# Create the key if it does not exist
If (-NOT (Test-Path $RegistryPath)) {
  New-Item -Path $RegistryPath -Force | Out-Null
}
# Now set the value
New-ItemProperty -Path $RegistryPath -Name 'Flags' -Value 1 -PropertyType DWORD -Force
New-ItemProperty -Path $RegistryPath -Name 'Id' -Value "{$guid}" -Force
New-ItemProperty -Path $RegistryPath -Name 'Url' -Value "\\$hostn\$share" -Force


Write-Host "Adding Trusted Catalog completed."
Write-Host ""
Write-Host "Goto Excel..."
Write-Host "    -> Developer Tab"
Write-Host "    -> Addins"
Write-Host "    -> Shared Folder"
Write-Host "    -> Select 'gbase' & Add"
Write-Host ""

$cls = Read-Host -Prompt 'Press any key to close...'
stop-process -Id $PID