/*
 * Copyright (c) 2017 Christoph Heiss
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

use std::env;
use std::path::Path;

use state::State;
use parser::{ExecResult, ToExecResult};

/*
 * https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html
 */
pub fn exec(state: &mut State, args: &[String]) -> ExecResult 
{
        if args.len() == 1 {
                return ExecResult::failure();
        }

        let args = &args[..args.len()-1];

        match args[0].as_ref() {
                "-a" => Path::new(&args[1]).exists().to_exec_result(),
                "-d" => Path::new(&args[1]).is_dir().to_exec_result(),
                "-f" => Path::new(&args[1]).is_file().to_exec_result(),
                "-h" => {
                        match Path::new(&args[1]).symlink_metadata() {
                                Ok(metadata) => metadata.file_type().is_symlink().to_exec_result(),
                                Err(_) => ExecResult::failure(),
                        }
                },
                "-n" => (!args[1].is_empty()).to_exec_result(),
                "-v" => {
                        match state.var(&args[1]) {
                                Some(var) => (!var.value.is_empty()).to_exec_result(),
                                None => match env::var(&args[1]) {
                                        Ok(var) => (!var.is_empty()).to_exec_result(),
                                        Err(_) => ExecResult::failure(),
                                },
                        }
                },
                "-R" => {
                        match state.var(&args[1]) {
                                Some(var) => (!var.value.is_empty() && var.reference).to_exec_result(),
                                None => ExecResult::failure(),
                        }
                },
                "-z" => args[1].is_empty().to_exec_result(),
                _ => ExecResult::with_code(2),
        }
}