/*
* Naamari-lautta - a wrapper of console cloud clients for Naamari
* Copyright (c) 2015-2017 Sunkware
* Key FP = 6B6D C8E9 3438 6E9C 3D97  56E5 2CE9 A476 99EF 28F6
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* [ WWW: sunkware.org ]                         [ E-MAIL: sunkware@gmail.com ]
 */

package main

import (
	"fmt"
	"os"
	"strings"
)

func main() {

	var err error

	if (len(os.Args) == 2) && (os.Args[1] == "--about") {
		About()

		return
	}

	if len(os.Args) < 3 {
		fmt.Println("Usage:")
		fmt.Println("Naamari-lautta <CloudClientSyntax.json> <HarbourDirPath> [--on-error-<retry^skip^halt>]")
		fmt.Println("or")
		fmt.Println("Naamari-lautta <MountDirPath> <HarbourDirPath> [--on-error-<retry^skip^halt>]")
		fmt.Println("or")
		fmt.Println("Naamari-lautta --about")
		fmt.Println("")
		fmt.Println("Examples:")
		fmt.Println("Naamari-lautta \"D:/clouderSyntax.json\" \"D:/Data/Harbour\" --on-error-halt")
		fmt.Println("Naamari-lautta clouderSyntax.json ~/harbour --on-error-skip")
		fmt.Println("Naamari-lautta /mnt/cloud ~/somewhere/harbour --on-error-retry")
		fmt.Println("Naamari-lautta --about      :)")
		fmt.Println("")
		fmt.Println("--on-error-retry is set by default")

		os.Exit(1)
	}

	// Extracting things from command line...
	var syntaxJsonOrMountPath string = os.Args[1]

	// Remove trailing slash if the path isn't just "/"
	if (syntaxJsonOrMountPath != "/") && (strings.HasSuffix(syntaxJsonOrMountPath, "/")) {
		syntaxJsonOrMountPath = syntaxJsonOrMountPath[:len(syntaxJsonOrMountPath)-1]
	}

	var workDirPath string = os.Args[2]

	// Remove trailing slash (not for "/") as well...
	if (workDirPath != "/") && (strings.HasSuffix(workDirPath, "/")) {
		workDirPath = workDirPath[:len(workDirPath)-1]
	}

	// Setting error handle type...
	var errorHandleType ErrorHandleType = ON_ERROR_RETRY

	if len(os.Args) == 4 {
		switch os.Args[3] {
		case "--on-error-retry":
			errorHandleType = ON_ERROR_RETRY

		case "--on-error-skip":
			errorHandleType = ON_ERROR_SKIP

		case "--on-error-halt":
			errorHandleType = ON_ERROR_HALT

		default:
			fmt.Println("Unknown option \"" + os.Args[3] + "\"!")

			os.Exit(1)
		}

	}

	if len(os.Args) > 4 {
		fmt.Println("Too many options!")

		os.Exit(1)
	}

	// Getting cloud client syntax OR mount dir path...

	var cloudClientSyntax CloudClientSyntax
	var mountDirPath string

	var objStat os.FileInfo

	objStat, err = os.Stat(syntaxJsonOrMountPath)
	if err != nil {
		fmt.Printf("Error: %s\n", err)
		fmt.Println("Cannot get info about \"" + syntaxJsonOrMountPath + "\"!")

		os.Exit(1)
	}

	if objStat.IsDir() == true {
		mountDirPath = syntaxJsonOrMountPath

	} else {
		cloudClientSyntax, err = LoadClientSyntax(syntaxJsonOrMountPath)

		if err != nil {
			fmt.Printf("Error: %s\n", err)
			fmt.Println("Cannot get cloud client syntax from \"" + syntaxJsonOrMountPath + "\"!")

			os.Exit(1)
		}
	}

	// Now: if 1st arg was filepath to .json with client syntax, mountDirPath == "";
	// else (going to use system routines on mounted dir) mountDirPath is path to that dir.

	// Checking work dir...
	objStat, err = os.Stat(workDirPath)
	if err != nil {
		fmt.Printf("Error: %s\n", err)
		fmt.Println("Cannot get info about \"" + workDirPath + "\"!")

		os.Exit(1)
	}

	if objStat.IsDir() == false {
		fmt.Printf("Error: %s\n", err)
		fmt.Println("Cannot work with \"" + workDirPath + "\": not a dir!")

		os.Exit(1)
	}

	err = Lautta(&cloudClientSyntax, mountDirPath, workDirPath, errorHandleType)

	if err != nil {
		fmt.Printf("Error: %s\n", err)
		fmt.Println("Lautta sank!")

		os.Exit(1)
	}

}
