func main() {
	//abs, err := filepath.Abs("./../")
	//log.Printf(filepath.Base(abs))
	flag.Parse()
	if *csvpath == "" {
		flag.Usage()
		return
	}
	fileInfo, err := os.Stat(*csvpath)
	if err != nil {
		log.Panic(err)
		return
	}
	isOutOneFile := false
	if *outpath != "" && strings.ToLower(filepath.Ext(*outpath)) == ".json" {
		isOutOneFile = true
	}
	if *outpath == "" {
		*outpath = *csvpath
	}

	mapAllList := make(map[string]string, 0)
	if fileInfo.IsDir() {
		infos, err := ioutil.ReadDir(*csvpath)
		if err != nil {
			log.Panic(err)
			return
		}
		if *outpath == "" {
			*outpath = *csvpath
		}
		for _, info := range infos {
			ext := filepath.Ext(info.Name())
			if ext != ".csv" {
				continue
			}

			name := filename(info.Name());
			byteContent, err := readFile(path.Join(*csvpath, info.Name()), *gbk)
			if err != nil {
				log.Fatalf("read csv: %v, error: %v", info.Name(), err)
				return
			}
			if isOutOneFile {
				mapAllList[name] = string(byteContent)
			} else {
				mapOneList := map[string]string{
					name : string(byteContent),
				}
				jsonfile := strings.Replace(info.Name(), ".csv", ".json", -1)
				outFile := path.Join(*outpath, jsonfile)
				err = writeJsonFile(outFile, mapOneList)
				if err != nil {
					log.Fatalf("write file: %v error: %v", outFile, err)
					return
				}
				log.Printf("write file: %v", outFile)
			}

		}

	} else {
		name := upper(filename(fileInfo.Name()));
		byteContent, err := readFile(path.Join(*csvpath, fileInfo.Name()), true)
		if err != nil {
			log.Fatalf("read csv error: %v", err)
			return
		}
		if isOutOneFile {
			mapAllList[name] = string(byteContent)
		} else {
			mapOneList := map[string]string{
				name : string(byteContent),
			}
			jsonfile := strings.Replace(fileInfo.Name(), ".csv", ".json", -1)
			outFile := path.Join(*outpath, jsonfile)
			err = writeJsonFile(outFile, mapOneList)
			if err != nil {
				log.Fatalf("write file: %v error: %v", outFile, err)
				return
			}
			log.Printf("write file: %v", outFile)
		}
	}

	if isOutOneFile {
		outFile := *outpath
		err = writeJsonFile(outFile, mapAllList)
		if err != nil {
			log.Fatalf("write file: %v error: %v", outFile, err)
			return
		}
		log.Printf("write file: %v", outFile)
	}

	log.Print("generator done!")
}