package com.github.aureliano.achmed.os.fs;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.github.aureliano.achmed.command.CommandFacade;
import com.github.aureliano.achmed.command.CommandResponse;
import com.github.aureliano.achmed.exception.FileResourceException;
import com.github.aureliano.achmed.helper.StringHelper;
import com.github.aureliano.achmed.resources.properties.FileProperties;

public class PosixFileProvider implements IFileProvider {

	private static final Logger logger = Logger.getLogger(PosixFileProvider.class);
	
	private FileProperties properties;
	
	public PosixFileProvider() {
		super();
	}

	@Override
	public void setFileMode() {
		CommandResponse res = CommandFacade.executeCommand(
			"chmod", this.properties.getMode(), this.properties.getPath());
		
		if (!res.isOK()) {
			throw new FileResourceException(res);
		}
	}

	@Override
	public void setFileOwner() {
		List<String> ownerAndGroup = new ArrayList<String>();
		if (!StringHelper.isEmpty(this.properties.getOwner())) {
			ownerAndGroup.add(this.properties.getOwner());
		}
		
		if ((!StringHelper.isEmpty(this.properties.getOwner())) && (!StringHelper.isEmpty(this.properties.getGroup()))) {
			ownerAndGroup.add(":");
		}
		
		if (!StringHelper.isEmpty(this.properties.getGroup())) {
			ownerAndGroup.add(this.properties.getOwner());
		}
		
		if (ownerAndGroup.isEmpty()) {
			logger.warn("Ignore setting file owner because no owner neither group was provided.");
			return;
		}
		
		ownerAndGroup.add(0, "chown");
		CommandResponse res = CommandFacade.executeCommand(ownerAndGroup.toArray(new String[0]));
		
		if (!res.isOK()) {
			throw new FileResourceException(res);
		}
	}

	@Override
	public void copyFile() {
		throw new UnsupportedOperationException("Not implemented yet.");
	}

	@Override
	public void copyFolder() {
		throw new UnsupportedOperationException("Not implemented yet.");
	}

	@Override
	public void setFileProperties(FileProperties properties) {
		this.properties = properties;
	}

	@Override
	public FileProperties getFileProperties() {
		return this.properties;
	}
}
@@ -435,16 +435,75 @@
 perties;
+%0Aimport com.github.aureliano.achmed.types.EnsureFileStatus;
 %0A%0Apublic
@@ -1763,32 +1763,172 @@
 ion(res);%0A%09%09%7D%0A%09%7D
+%0A%09%0A%09@Override%0A%09public void createFile() %7B%0A%09%09if (EnsureFileStatus.LINK.equals(this.properties.getEnsure())) %7B%0A%09%09%09this.createSymLink();%0A%09%09%7D%0A%09%7D
 %0A%0A%09@Override%0A%09pu
@@ -2323,9 +2323,387 @@
 ies;%0A%09%7D%0A
+%09%0A%09private void createSymLink() %7B%0A%09%09if (StringHelper.isEmpty(this.properties.getTarget())) %7B%0A%09%09%09throw new FileResourceException(%22Property 'target' not provided to symlink file.%22);%0A%09%09%7D%0A%09%09%0A%09%09CommandResponse res = CommandFacade.executeCommand(%22ln%22, %22-s%22, this.properties.getPath(), this.properties.getTarget());%0A%09%09if (!res.isOK()) %7B%0A%09%09%09throw new FileResourceException(res);%0A%09%09%7D%0A%09%7D%0A
 %7D

Create symlink file.