-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.go
More file actions
142 lines (125 loc) Β· 4.15 KB
/
Copy pathmain.go
File metadata and controls
142 lines (125 loc) Β· 4.15 KB
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
//go:generate go install -v github.com/josephspurrier/goversioninfo/cmd/goversioninfo
package main
import (
"os"
"path/filepath"
"github.com/portapps/portapps/v3"
"github.com/portapps/portapps/v3/pkg/files"
"github.com/portapps/portapps/v3/pkg/log"
"github.com/portapps/portapps/v3/pkg/registry"
)
type config struct {
Cleanup bool `yaml:"cleanup" mapstructure:"cleanup"`
Verbose string `yaml:"verbose" mapstructure:"verbose"`
}
var (
app *portapps.App
cfg *config
)
func init() {
var err error
// Default config
cfg = &config{
Cleanup: false,
Verbose: "1",
}
// Init app
if app, err = portapps.NewWithCfg("vlc-portable", "VLC", cfg); err != nil {
log.Fatal().Err(err).Msg("Cannot initialize application. See log file for more info.")
}
}
func main() {
if err := os.MkdirAll(app.DataPath, os.ModePerm); err != nil {
log.Fatal().Err(err).Msg("Cannot create data directory.")
}
app.Process = filepath.Join(app.AppPath, "vlc.exe")
app.Args = []string{
"--vlm-conf=" + filepath.Join(app.DataPath, "vlcrc"),
"--config=" + filepath.Join(app.DataPath, "vlcrc"),
"--no-plugins-cache",
"--no-qt-updates-notif",
}
// VLC paths
vlcRoamingPath := filepath.Join(os.Getenv("APPDATA"), "vlc")
vlcTmpPath := filepath.Join(app.AppPath, "tmp")
if err := os.MkdirAll(vlcTmpPath, os.ModePerm); err != nil {
log.Fatal().Err(err).Msg("Cannot create temporary directory.")
}
// Set env vars
vlcPluginPath := filepath.Join(app.DataPath, "plugins")
if err := os.MkdirAll(vlcPluginPath, os.ModePerm); err != nil {
log.Fatal().Err(err).Msg("Cannot create plugins directory.")
}
os.Setenv("VLC_PLUGIN_PATH", vlcPluginPath)
os.Setenv("VLC_VERBOSE", cfg.Verbose)
os.Setenv("TEMP", vlcTmpPath)
// VLC volatile files
dataDvdcssPath := filepath.Join(app.DataPath, "dvdcss")
dataMlXspf := filepath.Join(app.DataPath, "ml.xspf")
dataVlcQtInterface := filepath.Join(app.DataPath, "vlc-qt-interface.ini")
roamingDvdcssPath := filepath.Join(os.Getenv("APPDATA"), "dvdcss")
roamingMlXspf := filepath.Join(vlcRoamingPath, "ml.xspf")
roamingVlcQtInterface := filepath.Join(vlcRoamingPath, "vlc-qt-interface.ini")
// Copy existing files from data to roaming folder for the current user
if err := os.MkdirAll(vlcRoamingPath, os.ModePerm); err != nil {
log.Fatal().Err(err).Msg("Cannot create VLC roaming directory.")
}
if _, err := os.Stat(dataMlXspf); err == nil {
if err := files.CopyFile(dataMlXspf, roamingMlXspf); err != nil {
log.Error().Err(err).Msgf("Cannot copy %s", dataMlXspf)
}
}
if _, err := os.Stat(dataVlcQtInterface); err == nil {
if err := files.CopyFile(dataVlcQtInterface, roamingVlcQtInterface); err != nil {
log.Error().Err(err).Msgf("Cannot copy %s", dataVlcQtInterface)
}
}
// Handle reg key
regPath := filepath.Join(app.RootPath, "reg")
if err := os.MkdirAll(regPath, os.ModePerm); err != nil {
log.Fatal().Err(err).Msg("Cannot create registry directory.")
}
regFile := filepath.Join(regPath, "VLC.reg")
regKey := registry.Key{
Key: `HKCU\Software\VideoLAN\VLC`,
Arch: "32",
}
if err := regKey.Import(regFile); err != nil {
log.Warn().Err(err).Msg("Cannot import registry key")
}
// On exit
defer func() {
// Copy back to data
if _, err := os.Stat(dataDvdcssPath); err == nil {
if err = files.CopyFolder(dataDvdcssPath, roamingDvdcssPath); err != nil {
log.Warn().Err(err).Msgf("Cannot copy back %s", dataDvdcssPath)
}
}
if _, err := os.Stat(roamingMlXspf); err == nil {
if err = files.CopyFile(roamingMlXspf, dataMlXspf); err != nil {
log.Warn().Err(err).Msgf("Cannot copy back %s", roamingMlXspf)
}
}
if _, err := os.Stat(roamingVlcQtInterface); err == nil {
if err = files.CopyFile(roamingVlcQtInterface, dataVlcQtInterface); err != nil {
log.Warn().Err(err).Msgf("Cannot copy back %s", roamingVlcQtInterface)
}
}
// Export reg key
if err := regKey.Export(regFile); err != nil {
log.Error().Err(err).Msg("Cannot export registry key")
}
// Cleanup
if cfg.Cleanup {
files.Cleanup(
vlcRoamingPath,
vlcTmpPath,
)
if err := regKey.Delete(true); err != nil {
log.Error().Err(err).Msg("Cannot remove registry key")
}
}
}()
defer app.Close()
app.Launch(os.Args[1:])
}