//go:build linux || darwin
// +build linux darwin

package mount

/*
 * Copyright (c) 2021 Gilles Chehade <gilles@poolp.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

import (
	"fmt"

	"github.com/PlakarKorp/kloset/repository"
	"github.com/PlakarKorp/plakar/appcontext"
	"github.com/PlakarKorp/plakar/plakarfs"
	"github.com/anacrolix/fuse"
	"github.com/anacrolix/fuse/fs"
)

func (cmd *Mount) Execute(ctx *appcontext.AppContext, repo *repository.Repository) (int, error) {
	c, err := fuse.Mount(
		cmd.Mountpoint,
		fuse.FSName("plakar"),
		fuse.Subtype("plakarfs"),
		fuse.LocalVolume(),
	)
	if err != nil {
		return 1, fmt.Errorf("mount: %v", err)
	}

	loc, err := repo.Location()
	if err != nil {
		return 1, fmt.Errorf("mount: %v", err)
	}

	ctx.GetLogger().Info("mounted repository %s at %s", loc, cmd.Mountpoint)

	go func() {
		<-ctx.Done()
		fuse.Unmount(cmd.Mountpoint)
	}()

	err = fs.Serve(c, plakarfs.NewFS(repo, cmd.Mountpoint))
	if err != nil {
		return 1, err
	}
	<-c.Ready
	if err := c.MountError; err != nil {
		return 1, err
	}
	return 0, nil
}
