-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.ps1
178 lines (149 loc) · 6.32 KB
/
setup.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Read and parse configuration file
try {
$jsonContent = Get-Content -Raw -Path "config.json" | ConvertFrom-Json
} catch {
Write-Host "Error reading config.json. Please ensure the file exists and is valid JSON." -ForegroundColor Red
exit 1
}
# Initialize constants from config
$ADDONS_LIST = $jsonContent.addons_list -split ',' | ForEach-Object { $_.Trim() }
$FIREFOX_PROFILE_PATH = if ($jsonContent.firefox_profile_path) {
$jsonContent.firefox_profile_path
} else {
"$env:APPDATA\Mozilla\Firefox"
}
function Create-Profile {
<#
.SYNOPSIS
Creates a new Firefox profile and returns the profile directory path.
.OUTPUTS
System.String. The path to the newly created profile directory.
#>
Write-Host "Creating Profile..." -ForegroundColor Green
$profilesIniPath = "$FIREFOX_PROFILE_PATH\profiles.ini"
Write-Host "Enter name of the firefox profile: " -ForegroundColor Green -NoNewline
$name = Read-Host
Start-Process "firefox" -ArgumentList "-CreateProfile $name" -Wait
# Find and cd into the new profile
$profileContent = Get-Content $profilesIniPath -ErrorAction Stop
$folder = $profileContent |
Select-String -Pattern "Path=.*$name$" |
ForEach-Object { $_ -replace 'Path=', '' } |
Select-Object -First 1 # Added to handle multiple matches
if (-not $folder) {
Write-Host "Error: Could not find newly created profile." -ForegroundColor Red
exit 1
}
$profileDir = Join-Path -Path $FIREFOX_PROFILE_PATH $folder
Set-Location -Path $profileDir -ErrorAction Stop
Write-Host "Profile Creation Finished" -ForegroundColor Green
return $profileDir
}
function Init-GitRepo {
<#
.SYNOPSIS
Initializes a Git repository with Firefox user preferences and configurations.
#>
Write-Host "Initializing Git Repository..." -ForegroundColor Green
try {
# Check if git is installed
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
throw "Git is not installed or not in PATH"
}
# Initialize repository and configure
git init
git remote add origin https://github.com/ghoulboii/foxdots
# Fetch and checkout master branch
git fetch --quiet
git checkout --force --track origin/master
# Update submodules
git submodule update --init --recursive --remote
# Copy necessary files
$filesToCopy = @("user.js", "prefsCleaner.bat", "updater.bat")
foreach ($file in $filesToCopy) {
if (Test-Path "userjs\$file") {
Copy-Item "userjs\$file" -Destination "." -Force
}
}
# Run updater if it exists
if (Test-Path ".\updater.bat") {
.\updater.bat -unattended
}
Write-Host "Git Repository Initialized" -ForegroundColor Green
} catch {
Write-Host "Error during Git initialization: $_" -ForegroundColor Red
exit 1
}
}
function Download-Addons {
<#
.SYNOPSIS
Downloads and installs Firefox addons from Mozilla's addon store.
.PARAMETER profileDir
The Firefox profile directory where addons should be installed.
#>
param (
[Parameter(Mandatory=$true)]
[string]$profileDir
)
Write-Host "Downloading Addons..." -ForegroundColor Green
$MOZILLA_ADDON_URL = "https://addons.mozilla.org"
$addonTmpDir = New-Item -ItemType Directory -Path "$env:TEMP\firefox_addons" -Force
$extensionsDir = New-Item -ItemType Directory -Path "$profileDir\extensions"
foreach ($addon in $ADDONS_LIST) {
try {
Write-Host "Installing addon: $addon" -ForegroundColor Green
# Get addon download URL
$addonPageUrl = "$MOZILLA_ADDON_URL/en-US/firefox/addon/$addon/"
$addonPageContent = Invoke-WebRequest -Uri $addonPageUrl -UseBasicParsing
$addonDownloadURL = $addonPageContent.Links |
Where-Object { $_.href -match "/firefox/downloads/file/" } |
Select-Object -First 1 -ExpandProperty href
if (-not $addonDownloadURL) {
throw "Could not find download URL for addon: $addon"
}
# Download and extract addon
$addonFileName = Split-Path $addonDownloadURL -Leaf
$addonDownloadPath = Join-Path -Path $addonTmpDir.FullName -ChildPath $addonFileName
Invoke-WebRequest -Uri $addonDownloadURL -OutFile $addonDownloadPath
$addonExtractDir = Join-Path -Path $addonTmpDir.FullName -ChildPath "${addon}_extracted"
Expand-Archive -Path $addonDownloadPath -DestinationPath $addonExtractDir -Force
# Get addon ID and install
$manifestPath = Join-Path $addonExtractDir "manifest.json"
if (-not (Test-Path $manifestPath)) {
throw "manifest.json not found in addon package"
}
$manifestContent = Get-Content $manifestPath -Raw | ConvertFrom-Json
# Handle both Manifest V2 and V3 addon ID locations
$addonId = if ($manifestContent.applications -and $manifestContent.applications.gecko.id) {
# Manifest V2
$manifestContent.applications.gecko.id
} elseif ($manifestContent.browser_specific_settings -and $manifestContent.browser_specific_settings.gecko.id) {
# Manifest V3
$manifestContent.browser_specific_settings.gecko.id
} else {
throw "Could not find addon ID in manifest (neither V2 nor V3 format)"
}
if (-not $addonId) {
throw "Could not find addon ID in manifest"
}
Move-Item -Path $addonDownloadPath -Destination "$extensionsDir\$addonId.xpi" -Force
} catch {
Write-Host "Error installing addon $addon`: $_" -ForegroundColor Red
continue
}
}
# Cleanup
Remove-Item -Path $addonTmpDir -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "Addons Installation Completed" -ForegroundColor Green
}
# Main
try {
$profileDir = Create-Profile
Init-GitRepo
Download-Addons -profileDir $profileDir
Write-Host "Firefox profile setup completed successfully!" -ForegroundColor Green
} catch {
Write-Host "An error occurred during profile setup: $_" -ForegroundColor Red
exit 1
}