#!/bin/bash

source "${BASH_SOURCE[0]%/*}/.common.sh"

# if sourced from another script, do nothing
if (return 0 2>/dev/null); then
	return
fi


help() {
	cat <<-EOF
		Usage: ${SELF##*/}  [options..]  <command>
		
		Options:
		    --help          Show this help
		    --dev           Consider development dependencies
		
		Commands:
		    optimize        Optimize class autoloading.
		    install-fix     Fix vendor directory and install missing packages, then optimize.
		EOF
}


parse_args() {
	for arg in "${ARGS[@]}"; do
		case "$arg" in
			--dev)
				export COMPOSER_NO_DEV=0
				;;
			-*)
				help
				exit 1
				;;
			*)
				command="$arg"
				break
				;;
		esac
		array_shift ARGS
	done
}


main() {
	local command
	parse_args
	case "$command" in
		optimize)
			composer_optimize
			;;
		
		install-fix)
			if ((COMPOSER_NO_DEV)); then
				composer_no_dev
			else
				composer_dev
			fi
			;;
		
		*)
			help
			exit 1
			;;
	esac
}

ARGS=("$@")
main
