package main

import (
	"encoding/binary"
	"errors"
	"fmt"
	"github.com/recoilme/slowpoke"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
)

func main(){

	file := "pos/pos.db"

	// close all opened database
	// init key/val
	key := []byte("startPos")
	//val := []byte("bar")
	id := make([]byte, 4)
	binary.BigEndian.PutUint32(id, uint32(10))

	defer slowpoke.Close("pos/pos.db")
	//for i:=0;i<10 ; i++ {
	//	slowpoke.Set(file, []byte(strconv.Itoa(i)) , []byte(strconv.Itoa(i)))
	//}
	////slowpoke.Close("pos/pos.db")
	//store
	//err :=slowpoke.Set(file, []byte("startPos"), []byte("startPos"))
	//if err != nil{
	//	fmt.Println(err.Error())
	//}
	// get
	res, err := slowpoke.Get(file, key)

	if err != nil {
		println(err.Error())
	}
	path,_ :=GetCurrentPath()
	fmt.Println(string(res),path)


	//res, err = slowpoke.Get(file, []byte("4"))
	//
	//if err != nil {
	//	println(err.Error())
	//}
	////path,_ :=GetCurrentPath()
	//
	//fmt.Println(string(res))


}

func GetCurrentPath() (string, error) {
	file, err := exec.LookPath(os.Args[0])
	if err != nil {
		return "", err
	}
	path, err := filepath.Abs(file)
	if err != nil {
		return "", err
	}
	//fmt.Println("path111:", path)
	if runtime.GOOS == "windows" {
		path = strings.Replace(path, "\\", "/", -1)
	}
	//fmt.Println("path222:", path)
	i := strings.LastIndex(path, "/")
	if i < 0 {
		return "", errors.New(`Can't find "/" or "\".`)
	}
	//fmt.Println("path333:", path)
	return string(path[0 : i+1]), nil
}