2016-08-13 12:49:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-09-06 20:05:18 +00:00
|
|
|
"path/filepath"
|
2016-09-01 20:49:56 +00:00
|
|
|
"runtime/pprof"
|
2016-08-13 12:49:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-09-02 20:04:26 +00:00
|
|
|
envUser = os.Getenv("USER")
|
|
|
|
envHome = os.Getenv("HOME")
|
|
|
|
envHost = os.Getenv("HOSTNAME")
|
|
|
|
envPath = os.Getenv("PATH")
|
|
|
|
envShell = os.Getenv("SHELL")
|
|
|
|
envConfig = os.Getenv("XDG_CONFIG_HOME")
|
2016-08-13 12:49:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
gExitFlag bool
|
2016-08-14 12:45:24 +00:00
|
|
|
gLastDirPath string
|
2016-08-13 12:49:04 +00:00
|
|
|
gSelectionPath string
|
|
|
|
gSocketPath string
|
|
|
|
gLogPath string
|
|
|
|
gServerLogPath string
|
|
|
|
gConfigPath string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if envUser == "" {
|
2016-09-07 11:37:32 +00:00
|
|
|
log.Print("$USER not set")
|
2016-08-13 12:49:04 +00:00
|
|
|
}
|
|
|
|
if envHome == "" {
|
|
|
|
envHome = "/home/" + envUser
|
|
|
|
}
|
|
|
|
if envHost == "" {
|
|
|
|
host, err := os.Hostname()
|
|
|
|
if err != nil {
|
2016-08-17 20:06:45 +00:00
|
|
|
log.Printf("hostname: %s", err)
|
2016-08-13 12:49:04 +00:00
|
|
|
}
|
|
|
|
envHost = host
|
|
|
|
}
|
2016-09-02 20:04:26 +00:00
|
|
|
if envConfig == "" {
|
2016-09-06 20:05:18 +00:00
|
|
|
envConfig = filepath.Join(envHome, ".config")
|
2016-09-02 20:04:26 +00:00
|
|
|
}
|
2016-08-13 12:49:04 +00:00
|
|
|
|
|
|
|
tmp := os.TempDir()
|
|
|
|
|
2016-09-06 20:05:18 +00:00
|
|
|
gSocketPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.sock", envUser))
|
2016-08-13 12:49:04 +00:00
|
|
|
|
|
|
|
// TODO: unique log file for each client
|
2016-09-06 20:05:18 +00:00
|
|
|
gLogPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.log", envUser))
|
|
|
|
gServerLogPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.server.log", envUser))
|
2016-08-13 12:49:04 +00:00
|
|
|
|
2016-09-06 20:05:18 +00:00
|
|
|
gConfigPath = filepath.Join(envConfig, "lf", "lfrc")
|
2016-08-13 12:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func startServer() {
|
|
|
|
cmd := exec.Command(os.Args[0], "-server")
|
2016-08-17 20:28:42 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
2016-08-17 20:06:45 +00:00
|
|
|
log.Printf("starting server: %s", err)
|
2016-08-13 12:49:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2016-09-13 21:40:14 +00:00
|
|
|
showDoc := flag.Bool("doc", false, "show documentation")
|
2016-08-13 12:49:04 +00:00
|
|
|
serverMode := flag.Bool("server", false, "start server (automatic)")
|
2016-09-01 20:49:56 +00:00
|
|
|
cpuprofile := flag.String("cpuprofile", "", "path to the file to write the cpu profile")
|
2016-08-14 12:45:24 +00:00
|
|
|
flag.StringVar(&gLastDirPath, "last-dir-path", "", "path to the file to write the last dir on exit (to use for cd)")
|
2016-08-13 12:49:04 +00:00
|
|
|
flag.StringVar(&gSelectionPath, "selection-path", "", "path to the file to write selected files on exit (to use as open file dialog)")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2016-09-13 21:40:14 +00:00
|
|
|
if *showDoc {
|
|
|
|
fmt.Println(genDocString)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-01 20:49:56 +00:00
|
|
|
if *cpuprofile != "" {
|
|
|
|
f, err := os.Create(*cpuprofile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("could not create CPU profile: ", err)
|
|
|
|
}
|
|
|
|
if err := pprof.StartCPUProfile(f); err != nil {
|
|
|
|
log.Fatal("could not start CPU profile: ", err)
|
|
|
|
}
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
|
2016-08-13 12:49:04 +00:00
|
|
|
if *serverMode {
|
|
|
|
serve()
|
|
|
|
} else {
|
|
|
|
// TODO: check if the socket is working
|
2016-08-17 20:06:45 +00:00
|
|
|
if _, err := os.Stat(gSocketPath); os.IsNotExist(err) {
|
2016-08-13 12:49:04 +00:00
|
|
|
startServer()
|
|
|
|
}
|
|
|
|
|
|
|
|
client()
|
|
|
|
}
|
|
|
|
}
|