package main

import (
	"flag"
	"fmt"
	"log"
	"net"
	"path/filepath"
	"main/ftp"
)

var port int
var rootDir string

func init() {
	flag.IntVar(&port,"port",9090,"port to listet")
	flag.StringVar(&rootDir,"rootDir","public","root Directory")
	flag.Parse()
}

func handleConn(conn net.Conn) {
	defer conn.Close()

	absPath, err := filepath.Abs(rootDir)
	if err != nil {
		fmt.Print(err)
	}
	ftp.Serve(ftp.NewConn(conn,absPath))
}

func main() {
	server := fmt.Sprintf(":%d",port)
	listener,err := net.Listen("tcp",server)
	if err != nil {
		log.Fatal(err)
	}
	for {
		conn, err := listener.Accept()
		if err != nil {
			fmt.Print(err)
			continue
		}
		handleConn(conn)
	}
}
