-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathINSTALL.BAT
executable file
·285 lines (243 loc) · 9.17 KB
/
INSTALL.BAT
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
@echo off
setlocal EnableExtensions
:: ----------------------------------------------------------------
:: Set console to bright white on black initially
:: ----------------------------------------------------------------
color F0
:: ----------------------------------------------------------------
:: 1. Identify this script's directory (where mettalog.cmd is).
:: ----------------------------------------------------------------
set "SCRIPT_DIR=%~dp0"
REM ----------------------------------------------------------------
REM 1. Get domain & user
REM ----------------------------------------------------------------
set "CURRENT_DOMAIN=%USERDOMAIN%"
set "CURRENT_USER=%USERNAME%"
echo Detected Domain : %CURRENT_DOMAIN%
echo Detected User : %CURRENT_USER%
echo.
REM ----------------------------------------------------------------
REM 2. Check admin privileges
REM ----------------------------------------------------------------
>nul 2>&1 net session
if %errorlevel% NEQ 0 (
echo(
call :ColorError
echo ================================================
echo This script must be run with admin privileges.
echo ================================================
call :ColorNormal
echo 1^) Right-click the file and choose "Run as administrator", OR
echo 2^) Open an elevated Command Prompt ^(Run as administrator^) and
echo navigate to this script's folder, then run:
echo %~nx0
echo(
echo ---------------------------------------------------------------
echo If you're running this script from a network drive,
echo the elevated session might not see that drive mapping.
echo Let's try to detect the UNC path for the current drive...
echo ---------------------------------------------------------------
echo.
REM Attempt to detect the UNC path from `net use`.
set "DRV=%~d0"
set "UNC="
echo Current drive is: %DRV%
echo(
echo Checking if %DRV% is mapped via "net use"...
for /f "skip=4 tokens=1,2,3,*" %%A in ('net use') do (
REM Typically lines look like:
REM OK H: \\Server\Share Microsoft Windows Network
if /i "%%B"=="%DRV%" (
set "UNC=%%C"
goto :NONADMIN_FOUND
)
)
:NONADMIN_FOUND
if defined UNC (
call :ColorWarn
echo Found UNC for drive %DRV%: %UNC%
call :ColorNormal
echo(
echo To map this drive in the elevated session
echo.
echo net use %DRV% "%UNC%"
echo %DRV%
echo cd %SCRIPT_DIR%
) else (
call :ColorWarn
echo Could not detect a UNC path automatically.
call :ColorNormal
net use %DRV%
)
echo.
echo Then re-run this script as Administrator from that newly
echo mapped drive letter.
echo ---------------------------------------------------------------
pause
call :ResetColors
exit /b 1
)
REM ----------------------------------------------------------------
REM 3. If we get here, we *are* running with administrative privileges.
REM ----------------------------------------------------------------
echo Running as admin: Domain=%CURRENT_DOMAIN%, User=%CURRENT_USER%.
REM (A) Detect or map the UNC in the *admin* session
set "DRV=%~d0"
set "UNC="
echo Current drive is: %DRV%
echo(
echo Checking if %DRV% is mapped via "net use"...
for /f "skip=4 tokens=1,2,3,*" %%A in ('net use') do (
if /I "%%B"=="%DRV%" (
set "UNC=%%C"
goto :ADMIN_MAPPING_FOUND
)
)
:ADMIN_MAPPING_FOUND
if defined UNC (
call :ColorWarn
echo Found UNC path for %DRV%: %UNC%
call :ColorNormal
echo Attempting to ensure this drive is mapped in the admin session...
net use %DRV% "%UNC%" >nul 2>&1
if %errorlevel% NEQ 0 (
echo(
call :ColorError
echo Failed to map drive %DRV% to %UNC% with user.
call :ColorNormal
echo Ensure the credentials are correct and try again manually:
echo net use %DRV% "%UNC%"
echo %DRV%
echo cd %SCRIPT_DIR%
pause
call :ResetColors
exit /b 1
)
echo Drive %DRV% mapped successfully to %UNC% for admin session.
) else (
echo No UNC mapping found for drive %DRV%.
echo Attempting to map it to \\server\share with user ...
net use >nul 2>&1
if %errorlevel% NEQ 0 (
echo(
call :ColorError
echo Failed to map drive %DRV% to \\server\share with user.
call :ColorNormal
echo Ensure the share path and credentials are correct and try again manually:
echo net use %DRV%
echo %DRV%
echo cd %SCRIPT_DIR%
pause
call :ResetColors
exit /b 1
)
echo Drive %DRV% successfully mapped to \\server\share for admin session.
)
echo(
echo Script is running with administrative privileges. Proceeding...
echo.
REM ----------------------------------------------------------------
REM 4. EnableLinkedConnections (so Admin context sees mapped drives)
REM ----------------------------------------------------------------
echo Enabling EnableLinkedConnections...
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" ^
/v EnableLinkedConnections /t REG_DWORD /d 1 /f >nul 2>&1
REM ----------------------------------------------------------------
REM 5. Prompt user: Install or Uninstall the .metta file association
REM ----------------------------------------------------------------
:MENU
@echo(
@echo ===========================================
@echo .metta File Association
@echo ===========================================
@echo I = Install
@echo U = Uninstall
@echo ===========================================
choice /C IU /N /M "Select an option (I/U) then press ENTER: "
if errorlevel 2 goto UNINSTALL
if errorlevel 1 goto INSTALL
goto MENU
REM ----------------------------------------------------------------
REM (A) INSTALL SECTION
REM ----------------------------------------------------------------
:INSTALL
@echo Installing .metta file association...
:: 2. Escape backslashes in SCRIPT_DIR for the .reg file.
set "SCRIPT_DIR_ESCAPED=%SCRIPT_DIR:\=\\%"
:: 3. Name for the .reg file we will generate.
set "REGFILE=%SystemRoot%\temp\metta-association.reg"
@echo Creating registry file: "%REGFILE%"
(
@echo Windows Registry Editor Version 5.00
@echo ; Associate .metta files with mettalog.cmd
@echo [HKEY_CLASSES_ROOT\.metta]
@echo @="MettaFile"
@echo [HKEY_CLASSES_ROOT\MettaFile]
@echo @="Metta Script File"
@echo ; Set a system icon for .metta files
@echo [HKEY_CLASSES_ROOT\MettaFile\DefaultIcon]
@echo @="%%SystemRoot%%\\regedit.exe,0"
@echo [HKEY_CLASSES_ROOT\MettaFile\shell]
@echo @="open"
@echo ; Default action ^(open^)
@echo [HKEY_CLASSES_ROOT\MettaFile\shell\open]
@echo @="Run with Metta"
@echo [HKEY_CLASSES_ROOT\MettaFile\shell\open\command]
@echo @="\"%SCRIPT_DIR_ESCAPED%mettalog.cmd\" \"%%1\""
@echo ; Add 'Edit with Notepad' to the context menu
@echo [HKEY_CLASSES_ROOT\MettaFile\shell\edit]
@echo @="Edit with Notepad"
@echo [HKEY_CLASSES_ROOT\MettaFile\shell\edit\command]
@echo @="notepad.exe \"%%1\""
)>"%REGFILE%"
@echo Importing "%REGFILE%" into the registry...
reg import "%REGFILE%"
if errorlevel 1 (
@echo.
call :ColorError
echo Registry import failed. Make sure you are running as Administrator.
call :ColorNormal
goto :EOF
)
@echo Installation complete! .metta files should now open with mettalog.cmd.
goto :EOF
REM ----------------------------------------------------------------
REM (B) UNINSTALL SECTION
REM ----------------------------------------------------------------
:UNINSTALL
@echo Uninstalling .metta file association...
@echo Removing registry keys...
reg delete "HKCR\.metta" /f >nul 2>&1
reg delete "HKCR\MettaFile" /f >nul 2>&1
@echo Uninstall complete! .metta file association removed.
goto :EOF
endlocal
pause
:EOF
call :ResetColors
exit /b
:: ----------------------------------------------------------------
:: ColorError: Switch to bright red text on black background.
:: ----------------------------------------------------------------
:ColorError
color C0
goto :EOFColor
:: ----------------------------------------------------------------
:: ColorWarn: Switch to bright yellow text on black background.
:: ----------------------------------------------------------------
:ColorWarn
color E0
goto :EOFColor
:: ----------------------------------------------------------------
:: ColorNormal: Switch back to bright white text on black background.
:: ----------------------------------------------------------------
:ColorNormal
color F0
goto :EOFColor
:: ----------------------------------------------------------------
:: ResetColors: Return console to the typical default (white on black).
:: ----------------------------------------------------------------
:ResetColors
color 07
goto :EOFColor
:EOFColor