package main

import (
	"errors"
	"fmt"
	"math/rand"
	"os"
	"path/filepath"
	"strconv"
	"time"
)

func main() {
	var start, end int = 1, 5000

	rand.Seed(time.Now().UnixNano())

	for i := start; i <= end; i++ {
		var randnum int
		for {
			randnum = rand.Intn(end) + 1
			if randnum != i {
				break
			}
		}

		fmt.Println(strconv.Itoa(i) + " replace to " + strconv.Itoa(randnum))

		err := fileReplace("./target/", strconv.Itoa(i)+".txt", strconv.Itoa(randnum)+".txt")
		checkErr(err)

	}
}

func fileReplace(path string, file1 string, file2 string) error {

	if filepath.Ext(file1) != filepath.Ext(file2) {
		return errors.New("The file extension it not same!")
	}

	err := os.Rename(path+file1, path+"temp.txt")
	checkErr(err)

	err = os.Rename(path+file2, path+file1)
	checkErr(err)

	err = os.Rename(path+"temp.txt", path+file2)
	checkErr(err)
	return nil
}

func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}