123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package main
- import (
- "flag"
- "fmt"
- "log"
- "os"
- "path/filepath"
- "github.com/monkeybird/autimaat/app"
- "github.com/monkeybird/autimaat/irc"
- )
- func main() {
-
- profile := parseArgs()
-
- writePid()
-
- err := Run(profile)
- if err != nil {
- log.Fatal("[bot]", err)
- }
- }
- func writePid() {
- fd, err := os.Create("app.pid")
- if err != nil {
- log.Println("[bot] Create PID file:", err)
- return
- }
- fmt.Fprintf(fd, "%d", os.Getpid())
- fd.Close()
- }
- func parseArgs() irc.Profile {
- flag.Usage = func() {
- fmt.Println("usage:", os.Args[0], "[options] <profile directory>")
- flag.PrintDefaults()
- }
- newconf := flag.Bool("new", false, "Create a new, default configuration file and exit.")
- version := flag.Bool("version", false, "Display version information.")
- flag.Parse()
- if *version {
- fmt.Println(app.Version())
- os.Exit(0)
- }
- if flag.NArg() == 0 {
- flag.Usage()
- os.Exit(1)
- }
-
- root, err := filepath.Abs(flag.Arg(0))
- if err != nil {
- fmt.Fprintln(os.Stderr, err)
- os.Exit(1)
- }
-
- err = os.Chdir(root)
- if err != nil {
- fmt.Fprintln(os.Stderr, err)
- os.Exit(1)
- }
-
- profile := irc.NewProfile(root)
-
- if *newconf {
- err := profile.Save()
- if err != nil {
- fmt.Fprintln(os.Stderr, err)
- os.Exit(1)
- }
- fmt.Println("New configuration saved.")
- fmt.Println("Please edit it and relaunch the program.")
- os.Exit(0)
- }
-
- err = profile.Load()
- if err != nil {
- fmt.Fprintln(os.Stderr, err)
- os.Exit(1)
- }
- return profile
- }
|