: bharatvaj/dotfiles

Cross platform dotfiles for linux, mac and windows

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 
@echo on
rem TODO Make this work for Administrator accounts

goto :main

:set_var
    set "_tail=%*"
    call set "_tail=%%_tail:*%1 =%%"
    echo.%~1		%_tail%
    setx "%~1" "%_tail%" >nul
    set "%~1=%_tail%"
goto :eof

:create_dir
setlocal
    if not exist "%~1\" (
        echo mkdir "%~1"
        mkdir "%~1"
    )
endlocal
goto :eof

:add_path
    setlocal EnableDelayedExpansion

    for /f "skip=2 tokens=2*" %%i in ('reg query "HKCU\Environment" /v PATH 2^>nul') do (
        set "UserPath=%%j"
    )

    if not defined UserPath set "UserPath="

    echo !UserPath! | findstr /I /L /C:"%~1" >nul
    if errorlevel 1 (
        if defined UserPath (
            set "NewUserPath=!UserPath!;%~1"
        ) else (
            set "NewUserPath=%~1"
        )
        echo Adding to PATH: %~1
        setx PATH "!NewUserPath!" >nul
        set PATH="!NewUserPath!"
    ) else (
        echo Skipping PATH add: %~1
    )
endlocal
goto :eof

:update_shim <shim_file> <path> <args>
    echo path = "%~2"> %~1
    echo args = %~3>> %~1
goto :eof

:main
@call %~dp0\..\profile.cmd %1

if "%~1" == "" (
    call :set_var XDG_HOME %USERPROFILE%
    call :set_var SCOOP %SYSTEMROOT:~0,2%\ProgramData\scoop
) else (
    call :set_var XDG_HOME %~1
    call :set_var SCOOP %XDG_HOME%\scoop
)
echo SCOOP is "%SCOOP%"

call :set_var XDG_CACHE_HOME %XDG_HOME%\.cache
call :set_var XDG_CONFIG_HOME %XDG_HOME%\.config
call :set_var XDG_DATA_HOME %XDG_HOME%\.local\share
call :set_var XDG_STATE_HOME %XDG_HOME%\.local\state

if not exist "%SCOOP%\apps\scoop\current\bin\scoop.ps1" (

    rem Run this in powershell to install scoop and run this script again:
    rem Set-ExecutionPolicy RemoteSigned -scope CurrentUser
    rem echo powershell -Command "Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')"
    rem TODO Autorun? Print the current settings like Mode: USER or Mode: Admin
    echo If you want to install scoop globally run the contents of
    echo %XDG_HOME%\.config\cmd\global_scoop.ps1
    
    rem Install only the absolute essentials
    rem TODO Fix global install apps like gpg and firefox, by making it use the XDG_HOME
    rem set base="7zip clink dos2unix grep make mingit mpv sed sudo unzip vim"
    rem set full="%cmd% git-annex firefox"
    rem TODO Use a parameter to mention this rather hardcoding this value
    rem scoop install %full%
    scoop config shim kiennq

    goto :EOF
)

call :set_var VISUAL vim
call :set_var EDITOR %VISUAL%

call :set_var GNUPGHOME %XDG_DATA_HOME%\gnupg
call :set_var GPGHOME %XDG_DATA_HOME%\gnupg
call :set_var PASSWORD_STORE_DIR %XDG_DATA_HOME%\pass
call :set_var PASS_BASE_DIR %XDG_DATA_HOME%\pass

call :set_var LYNX_CFG %XDG_CONFIG_HOME%\lynx\lynx.cfg
call :set_var LYNX_LSS %XDG_CONFIG_HOME%\lynx\lynx.lss

call :set_var VIFM %XDG_CONFIG_HOME%\vifm

call :set_var FUZZER wlines
call :set_var ChocolateyToolsLocation %SYSTEMROOT:~0,2%\ProgramData\Tools

rem GUI
call :set_var GIMP2_DIRECTORY %XDG_DATA_HOME%\GIMP\2.8

rem XDG Dirs
call :create_dir "%XDG_HOME%"
call :create_dir "%XDG_CACHE_HOME%"
call :create_dir "%XDG_CONFIG_HOME%"
call :create_dir "%XDG_DATA_HOME%"
call :create_dir "%XDG_STATE_HOME%"

rem Vim Dirs
call :create_dir "%XDG_STATE_HOME%\vim"
call :create_dir "%XDG_STATE_HOME%\vim\backup"
call :create_dir "%XDG_STATE_HOME%\vim\swap"

rem Apply config patches
rem TODO Use mklink instead of xcopy, it will prevent overriding changed files in the destination dir.
xcopy /S /E /Y %XDG_HOME%\.config\cmd\patch %XDG_HOME%\

rem Install "UNIX" interface
if not exist "%SYSTEMDRIVE%\bin" ( mkdir %SYSTEMDRIVE%\bin )

if EXIST "%SCOOP%\apps\busybox\current\busybox.exe" (
    copy "%SCOOP%\apps\busybox\current\busybox.exe" %SYSTEMDRIVE%\bin
    copy "%SCOOP%\apps\scoop\current\supporting\shims\kiennq\shim.exe" %SYSTEMDRIVE%\bin\sh.exe
    call :update_shim C:\bin\sh.shim "C:\bin\busybox.exe" "sh -l"
)

rem TODO Report this in scoop and remove this line
rem TODO Or use custom installer for gpg
if EXIST %SCOOP%\apps\gpg\current\bin\gpgconf.ctl (
    del %SCOOP%\apps\gpg\current\bin\gpgconf.ctl
)

call :add_path "%SCOOP%\shims"
rem Always use \\ infront of '.' (causes issue with findstr)
call :add_path "%XDG_HOME%\.local\bin\cmd"
call :add_path "%SYSTEMDRIVE%\bin"
call :add_path "%APPDATA%\Python\Python313\Scripts"

if errorlevel 0 (
    echo Setting up clink
    clink autorun set >nul
    clink autorun install
) else (
    echo Cannot find clink in system
)