#!/bin/bash

# Constants
component_path_suffix="src/components"
TSCONFIG="tsconfig.json"
NPM_SUFFIX="@vendasta/"
## Version constants
COMPONENT_LOCAL_VERSION_NUMBER=$(cat ${component_path_suffix}/$2/package.json | grep "version" | awk '{gsub(/"/, "", $2); gsub(/,$/,""); print $2}')
COMPONENT_REMOTE_VERSION_NUMBER=$(npm view ${NPM_SUFFIX}$2 | grep 'version:' | awk '{gsub(/[:\47]*/,""); gsub(/,$/,""); print $2}')
## Colors
RED='\033[0;31m'
NoColor='\033[0m'

# Check for arguments
## No arguments supplied
if [ -z "$1" ] 
    then
        echo "No arguments supplied. Run vnpm.sh help for further information"
        exit 1
fi

## Arguments more than 2
if [ $# -gt 2 ] 
    then
        echo "Too many arguments"
        exit 1
fi

# Help messages
if [ $1 == "help" ] 
    then
        echo "For publish SAIL component: vnpm.sh [component] [version number]"
        printf "${RED}ex. vnpm.sh -publish fec-nav or vpm.sh -p fec-nav ${NoColor} \n"
        echo "For check the latest version of SAIL component: vnpm.sh [component]"
        printf "${RED}ex. vnpm.sh -v fec-nav or vpm.sh -version fec-nav ${NoColor} \n"
        exit 0
fi

if [ $1 == "-version" -o $1 == "-v" ]
    then
        echo "$2 version(local): $COMPONENT_LOCAL_VERSION_NUMBER"
        echo "$2 version(npmjs): $COMPONENT_REMOTE_VERSION_NUMBER"
        exit 0
fi


# Version comparison
vercomp () {
    local local_ver=$1
    local remote_ver=$2 
    if [[ $1 == $2 ]]
        then
            return 1
    fi
    if [[ ${local_ver:0:1} > ${remote_ver:0:1} ]]
        then
            return 0
    else
        if [[ ${local_ver:2:1} > ${remote_ver:2:1} ]]
            then 
                return 0
    else
        if [[ ${local_ver:4:1} > ${remote_ver:4:1} ]]
        then 
            return 0
    else
            return 2
    fi 
    fi
    fi
}

if [ $1 == "-publish" -o $1 == "-p" ]
    then
        vercomp $COMPONENT_LOCAL_VERSION_NUMBER $COMPONENT_REMOTE_VERSION_NUMBER
        case $? in
            0)
                printf "${RED}Version check passed. {$NoColor} \n ";;
            1)
                echo "You cannot publish $2! The version number on npmjs is $COMPONENT_REMOTE_VERSION_NUMBER as well."
                exit 1;;
            2)
                echo "You cannot publish $2! The version number on npmjs is $COMPONENT_REMOTE_VERSION_NUMBER, which is larger than current"
                exit 1;;
        esac
        if [ -e ${component_path_suffix}/$2/package.json ] 
            then 
                # Clean JS and CSS files in the components' directory
                echo "====== CLEANING COMPONENTS DIRECTORIES ======"
                npm run clean:components
                echo "====== FINISHED CLEANING ======"
                # Run npm install to avoid `cannot find module` errors
                echo "====== RUNNING NPM INSTALL TO AVOID <CANNOT FIND MODULE ERRORS> ======"
                npm install
                echo "====== FINISHED NPM INSTALL ======"
                # Start compiling ts to js
                echo "====== COMPILING ALL SAIL COMPONENTS  ======"
                echo "Using tsconfig.json from FrontEndCentral/tsconfig.json"
                $(npm bin)/tsc -p ${TSCONFIG}
                # We wanna publish css file so start compiling scss to css
                echo "====== COMPILING STYLESHEETS ======"
                compass compile --force
                echo "====== FINISHED COMPILING ======"
                # Start publishing
                echo "====== START PUBLISHING $2 ======"
                cd ${component_path_suffix}/$2
                npm publish --access public
                echo "====== FINSIHED PUBLISHING ======"
                exit 0
        fi
        echo "The component: $2 is not a valid component!"
        exit 1
fi

echo "Sorry, invalid argument(s). Run vnpm.sh help for further information"
exit 1
