package main

import (
	"crypto/tls"
	"flag"
	"fmt"
	"log"
	"os"
	"time"

	"github.com/secsy/goftp"
)

var (
	host string = ""
	port string = ""
	user string = ""
	pswd string = ""
	rmt  string = ""
	loc  string = ""
)

func init() {
	flag.StringVar(&host, "h", "10.60.254.52", "FTPS Server Address")
	flag.StringVar(&port, "o", "3031", "FTPS Server Port")
	flag.StringVar(&user, "u", "idte", "Username")
	flag.StringVar(&pswd, "p", "wistronTE2022", "Password")
	flag.StringVar(&loc, "L", "", "Local Path")
	flag.StringVar(&rmt, "R", "", "Remote Path")
	flag.Parse()
}

func main() {
	log.SetFlags(log.Ldate | log.Ltime)
	log.SetOutput(os.Stdout)

	if host == "" || port == "" || user == "" || pswd == "" {
		log.Println("[ERR]  Invalid Arguments.")
		os.Exit(1)
	}

	cfg := goftp.Config{
		User:     user,
		Password: pswd,
		Timeout:  time.Second * 30,
		TLSMode:  goftp.TLSExplicit,
		TLSConfig: &tls.Config{
			InsecureSkipVerify: true,
			MinVersion:         tls.VersionTLS10,
		},
	}

	client, err := goftp.DialConfig(cfg, host+":"+port)
	if err != nil {
		log.Printf("[ERR]  %s\r\n", err.Error())
		os.Exit(1)
	}
	defer client.Close()

	cwd, err := client.Getwd()
	if err != nil {
		log.Printf("[ERR]  %s\r\n", err.Error())
		os.Exit(1)
	}
	log.Printf("[MSG]  CWD: %s\r\n", cwd)

	fis, err := client.ReadDir(cwd)
	if err != nil {
		log.Printf("[ERR]  %s\r\n", err.Error())
		os.Exit(1)
	}
	for _, f := range fis {
		fmt.Printf("FileName=%v; FileSize=%v; ModTime=%v; IsDir=%v\r\n", f.Name(), f.Size(), f.ModTime().Format("2006-01-02 15:04:05"), f.IsDir())
	}
}
