/*
 * Copyright 2026 Oleg Borodin <onborodin@gmail.com>
 */
package auxtool

import (
	"os"
	"path/filepath"
	"strings"
)

// Clean only overbase elements of dir path if possible
func CleanDirs(basedir, datadir string) {
	separator := string(os.PathSeparator)

	basedir = filepath.Clean(separator + basedir)
	datadir = filepath.Clean(separator + datadir)

	items := strings.Split(datadir, separator)
	for i := len(items); i > 0; i-- {
		p := filepath.Join(items[0:i]...)
		p = filepath.Clean(separator + p)
		if p == basedir {
			break
		}
		fileInfo, err := os.Stat(p)
		if err != nil {
			return
		}
		if fileInfo.IsDir() {
			err = os.Remove(p)
			if err != nil {
				return
			}
		}
	}
}
