#!/bin/bash

#define.js파일내용으로 json 파일 생성
#  usage PARSE :: ./definescripter.sh json /host/home/docker/cordova-app-contents/home/mcaller_v2/define/define_common_app_callbanner/js/define.js /host/home/docker/cordova-app-contents/home/mcaller_v2/define/define_common_app_callbanner/js/output.json

# json파일 내용으로  define.js파일 생성
# usage define :: /host/home/docker/cordova-app-contents/home/mcaller_v2/define/define_common_app_callbanner/js/define.js /host/home/docker/cordova-app-contents/home/mcaller_v2/define/define_common_app_callbanner/js/output.json

#or

# json스트링으로 define.js파일생성
# usage define /host/home/docker/cordova-app-contents/home/mcaller_v2/define/define_common_app_callbanner/js/define.js {  "server_host": "'https://ent-common.lgntel.com'",  "server_port": "':443'",  "server_debug_description": "ent-common.lgntel.com",  "company_logo0": "img/logo_callbanner.png",  "agree_title": "CallBanner 알림",  "contents_version": "1.2.0",  "title": "CallBanner",  "front_page": ".gnb_03",  "front_pageId": "address" }
echo $# $1 $2 $3
case $1 in
    "json")
        if [ -f "$2" ]; then
            echo "ITS file"
            if [[ $2 == *define.js ]]; then
                echo "ITS define.js"
                input_file="$2"
                output_file="$3"

                # Initialize the JSON object
                echo "{" > "$output_file"

                # Read the file line by line
                while IFS= read -r line; do
                    # Match variable declaration lines
                    if [[ $line =~ ^var\ ([a-zA-Z0-9_]+)\ =\ \"?(.*?)\"?\;$ ]]; then
                        var_name="${BASH_REMATCH[1]}"
                        var_value="${BASH_REMATCH[2]}"

                        # Remove leading and trailing quotes from string values
                        var_value=$(echo "$var_value" | sed -e 's/^"//' -e 's/"$//')

                        # If the value contains quotes, keep it as a string
                        if [[ $var_value =~ \" ]]; then
                            var_value="\"$var_value\""
                        fi

                        # Print the variable as a JSON field
                        echo "  \"$var_name\": \"$var_value\"," >> "$output_file"
                    fi
                done < "$input_file"

                # Remove the trailing comma and close the JSON object
                if [[ "$(uname)" == "Darwin" ]]; then
                    # macOS
                    sed -i '' -e '$ s/,$//' "$output_file"
                else
                    # Linux
                    sed -i '$ s/,$//' "$output_file"
                fi
                echo "}" >> "$output_file"

                echo "JSON conversion completed. Output written to $output_file."
            else
                echo "ITS NOT define.js"
            fi
        else
            echo "ITS NOT file"
        fi
    ;;
    "define")
        if [ -f "$3" ]; then
            echo  "ITS file $3"
            input_file="$3"
            output_file="$2"

            # Initialize the JS file
            echo "" > "$output_file"

            # Read the JSON file and convert each entry to a JS variable
            while IFS= read -r line; do
                # Match JSON key-value lines
                if [[ $line =~ \"([a-zA-Z0-9_]+)\"\:\ \"?(.*?)\"?\,?$ ]]; then
                    var_name="${BASH_REMATCH[1]}"
                    var_value="${BASH_REMATCH[2]}"

                    # Add quotes around the value if it contains non-numeric characters
                    if [[ ! $var_value =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
                        var_value="\"$var_value\""
                    fi

                    # Print the variable as a JS declaration
                    echo "var $var_name = $var_value;" >> "$output_file"
                fi
            done < <(jq -r 'to_entries[] | "\"\(.key)\": \"\(.value)\","' "$input_file")

            echo "JS conversion completed. Output written to $output_file."
        else if [ -n "$3" ]; then
            echo "ITS JSON STRING"
            input_json="$3"
            output_file="$2"

            # Initialize the JS file
            echo "" > "$output_file"

            # Read the JSON string and convert each entry to a JS variable
            echo "$input_json" | jq -r 'to_entries[] | "var \(.key) = \(.value | if type == "string" then "\"\(. | gsub("\\\\"; "\\\\"); gsub("\""; "\\\""))\"" else . end);" ' >> "$output_file"

            echo "JS conversion completed. Output written to $output_file."
        else
            echo "3rd parameter is not file neither json"
        fi
    ;;
esac

echo "DONE" 

exit
