#!/bin/bash

RED="\033[0;31m"
YELLOW="\033[0;33m"
GREEN="\033[0;32m"
NONE="\033[0m"

# declare all default directories
declare -a directories=( 'app/code' 'app/etc' 'app/locale' 'app/design' js lib media skin shell var)

include_others=0
include_others_files=0
include_others_files_mindepth=1
include_app_code_core=0
modman=modman

echo "# Modman file generated by 'generate-modman'" > $modman

function _handle_input() {
    argument_found=0
    case $1 in
        --subdirectory=*)
            subdirectory="${1#*=}/"
            argument_found=1
            ;;
        --include-others)
            include_others=1
            argument_found=1
        ;;
        --include-others-files)
            include_others_files=1
            argument_found=1
        ;;
        --include-others-files-mindepth=*)
            include_others_files_mindepth="${1#*=}"
            argument_found=1
        ;;
        --include-app-code-core)
            include_app_code_core=1
            argument_found=1
        ;;
    esac
    if [ $argument_found -eq 0 ]; then
        _print_help
        exit 0
    fi
}

function _print_help() {
        echo "┌─┐┌─┐┌┐┌┌─┐┬─┐┌─┐┌┬┐┌─┐  ┌┬┐┌─┐┌┬┐┌┬┐┌─┐┌┐┌"
        echo "│ ┬├┤ │││├┤ ├┬┘├─┤ │ ├┤───││││ │ │││││├─┤│││"
        echo "└─┘└─┘┘└┘└─┘┴└─┴ ┴ ┴ └─┘  ┴ ┴└─┘─┴┘┴ ┴┴ ┴┘└┘"
        echo ""
        echo -e "${YELLOW}Usage${NONE}"
        echo " generate-modman [options]"
        echo ""
        echo -e "${YELLOW}Options:${NONE}"
        echo -e "${GREEN}--include-others${NONE}             Include non default magento directories"
        echo -e "${GREEN}--include-others-files${NONE}       Include non default magento directories as files"
        echo -e "${GREEN}--include-others-files-mindepth=MINDEPTH${NONE}       Mindepth for include other files (default: 1)"
        echo -e "${GREEN}--include-app-code-core${NONE}      Include app/code/core files"
        echo -e "${GREEN}--subdirectory=DIRNAME${NONE}       If files are in a subdirectory of the module"
        echo ""
}

function handle_app_code() {
    # link only directories
    find app/code -mindepth 3 -maxdepth 3 -path "*app/code/local/*" -o -path "*app/code/community/*" -type d
    if [ $include_app_code_core -eq 1 ] && [ -d "app/code/core" ]; then
        # link files for core
        _default_file_handler app/code/core
    fi
}

function handle_app_etc() {
    # link all files
    _default_file_handler 'app/etc'
}

function handle_app_locale() {
    _default_file_handler 'app/locale'
}

function handle_app_design() {
    # link all directories who aren't default
    find app/design -mindepth 2 -maxdepth 2 \( ! -path "*frontend/rwd*" ! -path "*frontend/default*" ! -path "*frontend/base*" ! -path "*adminhtml/default*" ! -path "*adminhtml/base*" ! -path "*install/default*" \) -type d
    # link files in default directories
    find app/design ! -iname ".*" \( -path  "*frontend/rwd*" -o -path "*frontend/default*" -o -path "*frontend/base*" -o -path "*adminhtml/default*" -o -path "*adminhtml/base*" -o -path "*install/default*" \) -type f
}

function handle_js() {
    _default_file_handler js
}

function handle_lib() {
    _default_file_handler lib
}

function handle_media() {
    _default_file_handler media
}

function handle_skin() {
    # link all directories who aren't default
    find skin -mindepth 2 -maxdepth 2 \( ! -path "*frontend/rwd*" ! -path "*frontend/default*" ! -path "*frontend/base*" ! -path "*adminhtml/default*" ! -path "*adminhtml/base*" ! -path "*install/default*" \) -type d
    # link files in default directories
    find skin ! -iname ".*" \( -path  "*frontend/rwd*" -o -path "*frontend/default*" -o -path "*frontend/base*" -o -path "*adminhtml/default*" -o -path "*adminhtml/base*" -o -path "*install/default*" \) -type f
}

function handle_shell() {
    find shell -maxdepth 1 -path "*shell/*" -type d
    find shell -maxdepth 1 -type f
}

function handle_var() {
    find var -mindepth 1 -maxdepth 1 -type d
}

function _default_file_handler() {
    find $1 -mindepth 1 ! -iname ".*" -type f
}


if [ ! -z "$1" ]; then
   for argument in $@; do
        _handle_input $argument
   done
fi

prefix=''
if [ -d "$subdirectory" ]; then
    modman=${PWD}/modman;
    cd $subdirectory
    prefix=$subdirectory
fi

# call all handlers
for dir in "${directories[@]}"; do
  if [ -d "$dir" ]; then
    echo "# ${dir}" >> $modman
    handle_${dir//\//_} | awk -v prefix="$prefix" '{ print prefix$0 "            " $0 }' >> $modman
  fi
done

# handle all other directories
if [ $include_others -eq 1 ] && [ $include_others_files -ne 1 ]; then
    echo "# others" >> $modman
    find * -type d -maxdepth 0 \( ! -path "*app*" ! -path "*js*" ! -path "*lib*" ! -path "*media*" ! -path "*skin*" ! -path "*shell*" ! -path "*var*" \) | awk -v prefix="$prefix" '{ print prefix$0 "            " $0 }' >> $modman
fi
# handle all other directories (symlink files NOT directories)
if [ $include_others_files -eq 1 ]; then
    echo "# others (--include-others-files)" >> $modman
    find * ! -iname "modman" ! -iname "composer.json" ! -iname ".gitignore" -mindepth $include_others_files_mindepth \( ! -path "app/*" ! -path "js/*" ! -path "lib/*" ! -path "media/*" ! -path "skin/*" ! -path "shell/*" ! -path "var/*" \) -type f | awk -v prefix="$prefix" '{ print prefix$0 "            " $0 }' >> $modman
fi