#!/bin/bash

clear

RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

# ##@  _____                                      _____ 
# ##@ ( ___ )                                    ( ___ )
# ##@  |   |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|   | 
# ##@  |   |                                      |   | 
# ##@  |   |       __    __   __   ______         |   | 
# ##@  |   |      /\ "-./  \ /\ \ /\___  \        |   | 
# ##@  |   |      \ \ \-./\ \\ \ \\/_/  /__       |   | 
# ##@  |   |       \ \_\ \ \_\\ \_\ /\_____\      |   | 
# ##@  |   |        \/_/  \/_/ \/_/ \/_____/      |   | 
# ##@  |   |                                      |   | 
# ##@  |   |             easy mizy!               |   | 
# ##@  |___|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|___| 
# ##@ (_____)                                    (_____)

# pattern="##@"
# while IFS= read -r line; do
#     if [[ "$line" == "$pattern"* ]]; then
#         cleanLine="${line#$pattern}"
#         echo "$cleanLine" | sed 's/^/\x1b[32m/'
#     fi
# done < "$0"

cat <<EOL | sed 's/^/\x1b[32m/;s/$/\x1b[0m/'
 _____                                      _____ 
( ___ )                                    ( ___ )
 |   |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|   | 
 |   |                                      |   | 
 |   |       __    __   __   ______         |   | 
 |   |      /\ "-./  \ /\ \ /\___  \        |   | 
 |   |      \ \ \-./\ \\\\ \ \\\\/_/  /__       |   | 
 |   |       \ \_\ \ \_\\\\ \_\ /\_____\      |   | 
 |   |        \/_/  \/_/ \/_/ \/_____/      |   | 
 |   |                                      |   | 
 |   |             easy mizy!               |   | 
 |___|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|___| 
(_____)                                    (_____)
EOL

echo -e "\e[32m"
echo "What is the name of your project?"
read -p "" PROJECT_NAME
mkdir -p "$PROJECT_NAME/assets"
mkdir -p "$PROJECT_NAME/sass"
LOCATION_INSTALLER=$(pwd)
cd "$PROJECT_NAME/sass"
LOCATION_SASS=$(pwd)
cd "$LOCATION_INSTALLER"

if [ $? -eq 0 ]; then
    # nothing to do
    :
else
    echo -e "\e[32m"
    cd "$LOCATION_INSTALLER"
    rm -rf "$PROJECT_NAME/sass"
    rm -rf "$PROJECT_NAME/assets"
    rm -rf "$PROJECT_NAME"

    echo You have a bug in miz installation, please check the following.
	echo 1- Check your internet.
	echo 2- Turn off your proxy or vpn.
    echo 3- Open the installer file again.
    echo 4- If none of this works, you can download the raw file from the link.
    echo Otherwise, email mizframework59@gmail.com
    echo "Do you want open link ?(Y/n)"
    read -p "" CHECK_OPEN_LINK

    if [[ "${CHECK_OPEN_LINK,,}" == "n" ]]; then
        # nothing to do  
        exit
        :
    elif [[ "${CHECK_OPEN_LINK,,}" == "y" ]]; then
        start https://eazymizy.com
        exit
    else
        echo "Please use Y or n"
    fi
fi

cd "$PROJECT_NAME"
package_name="git"

while true; do
    if command -v $package_name > /dev/null 2>&1; then
        echo "$package_name was installed."
        break
    else
        echo "$package_name is not installed."
        
        while true; do
            read -p "Do you want to check again? (Y/n) " user_input
            case $user_input in
                [Nn]* )
                    exit
                    ;;
                [Yy]* )
                    break
                    ;;
                * )
                    echo "Please use Y or n."
                    ;;
            esac
        done
    fi
done

git clone https://github.com/miz59/miz


if [ $? -eq 0 ]; then
    # nothing to do
    :
else
    echo -e "\e[32m"
    cd "$LOCATION_INSTALLER"
    rm -rf "$PROJECT_NAME/miz"
    rm -rf "$PROJECT_NAME/sass"
    rm -rf "$PROJECT_NAME/assets"
    rm -rf "$PROJECT_NAME"
    echo You have a bug in miz installation, please check the following.
	echo 1- Check your internet.
	echo 2- Turn off your proxy or vpn.
    echo 3- Open the installer file again.
    echo 4- If none of this works, you can download the raw file from the link.
    echo Otherwise, email mizframework59@gmail.com
    echo "Do you want open link ?(Y/n)"
    read -p "" CHECK_OPEN_LINK

    if [[ "${CHECK_OPEN_LINK,,}" == "n" ]]; then
        # nothing to do  
        exit
        :
    elif [[ "${CHECK_OPEN_LINK,,}" == "y" ]]; then
        start https://eazymizy.com
        exit
    else
        echo "Please use Y or n"
    fi
fi

# cd "$LOCATION_SASS"
cd miz
LOCATION_MIZ=$(pwd)
cd "$LOCATION_INSTALLER"

LOCATION_ASSETS_MIZ="$LOCATION_MIZ/assets"
cd "$LOCATION_INSTALLER"
cp -r "$LOCATION_ASSETS_MIZ" "$PROJECT_NAME"
rm -rf "$LOCATION_MIZ/assets"
rm -rf "$LOCATION_MIZ/.git"
rm -rf "$LOCATION_MIZ/.github"
rm -f ".gitignore"
rm -f ".gitattributes"
rm -rf "$LOCATION_MIZ/.vscode"
rm -f "%PROJECT-NAME%/assets/css"/*

echo "@forward \"./layout\";" >> "$LOCATION_SASS/style.scss"
echo "@forward \"./components\";" >> "$LOCATION_SASS/style.scss"
echo "@forward \"./pages\";" >> "$LOCATION_SASS/style.scss"
echo "" >> "$LOCATION_SASS/_layout.scss"
echo "" >> "$LOCATION_SASS/_components.scss"
mkdir -p "$LOCATION_SASS/pages"
echo "@forward \"./home\";" >> "$LOCATION_SASS/pages/_index.scss"
echo "" >> "$LOCATION_SASS/pages/_home.scss"
cd "$LOCATION_INSTALLER/$PROJECT_NAME"


if [ $? -eq 0 ]; then
    # nothing to do
    :
else
    cd "$LOCATION_INSTALLER"
    rm -rf "$PROJECT_NAME/miz"
    rm -rf "$PROJECT_NAME/sass"
    rm -rf "$PROJECT_NAME/assets"
    rm -rf "$PROJECT_NAME"
    echo You have a bug in miz installation, please check the following.
	echo 1- Check your internet.
	echo 2- Turn off your proxy or vpn.
    echo 3- Open the installer file again.
    echo 4- If none of this works, you can download the raw file from the link.
    echo Otherwise, email mizframework59@gmail.com
    echo "Do you want open link ?(Y/n)"
    read -p "" CHECK_OPEN_LINK

    if [[ "${CHECK_OPEN_LINK,,}" == "n" ]]; then
        # nothing to do  
        exit
        :
    elif [[ "${CHECK_OPEN_LINK,,}" == "y" ]]; then
        start https://eazymizy.com
        exit
    else
        echo "Please use Y or n"
    fi
fi

touch "$LOCATION_SASS/_layout.scss"
touch "$LOCATION_SASS/_components.scss"
mkdir -p "$LOCATION_SASS/pages"

touch "$LOCATION_SASS/pages/_home.scss"
cd "$LOCATION_INSTALLER/$PROJECT_NAME"


if [ $? -eq 0 ]; then
    # nothing to do
    :
else
    echo -e "\e[32m"
    cd "$LOCATION_INSTALLER"
    rm -rf "$PROJECT_NAME/miz"
    rm -rf "$PROJECT_NAME/sass"
    rm -rf "$PROJECT_NAME/assets"
    rm -rf "$PROJECT_NAME"
    echo You have a bug in miz installation, please check the following.
	echo 1- Check your internet.
	echo 2- Turn off your proxy or vpn.
    echo 3- Open the installer file again.
    echo 4- If none of this works, you can download the raw file from the link.
    echo Otherwise, email mizframework59@gmail.com
    echo "Do you want open link ?(Y/n)"
    read -p "" CHECK_OPEN_LINK

    if [[ "${CHECK_OPEN_LINK,,}" == "n" ]]; then
        # nothing to do  
        exit
        :
    elif [[ "${CHECK_OPEN_LINK,,}" == "y" ]]; then
        start https://eazymizy.com
        exit
    else
        echo "Please use Y or n"
    fi
fi

package_name="npm"

while true; do
    if command -v $package_name > /dev/null 2>&1; then
        echo "$package_name was installed."
        break
    else
        echo "$package_name is not installed."
        
        while true; do
            read -p "Do you want to check again? (Y/n) " user_input
            case $user_input in
                [Nn]* )
                    exit
                    ;;
                [Yy]* )
                    break
                    ;;
                * )
                    echo "Please use Y or n."
                    ;;
            esac
        done
    fi
done

echo -e "\e[32m"
echo "Do you want the MIZBAN? (Y/n)"
read -r MIZBAN_INSTALL

if [[ "${MIZBAN_INSTALL,,}" == "n" ]]; then
    # nothing to do  
    :
elif [[ "${MIZBAN_INSTALL,,}" == "y" ]]; then
    git clone https://github.com/miz59/mizban
    cd "${LOCATION_INSTALLER}"
    cp -r "${LOCATION_INSTALLER}/${PROJECT_NAME}/mizban/assets/vendors/"* "${PROJECT_NAME}/assets/vendors/"
    rm -rf "${PROJECT_NAME}/mizban"
    echo "MIZBAN installed"
else
    echo "Please use Y or n"
fi

cd "${LOCATION_INSTALLER}/${PROJECT_NAME}"


PACKAGE_JSON_PATH="$LOCATION_INSTALLER/$PROJECT_NAME/package.json"

cat <<EOL > "$PACKAGE_JSON_PATH"
{
    "name": "$PROJECT_NAME",
    "version": "1.0.0",
    "description": "null",
    "type": "module",
    "scripts": {
    $(if [[ "${MIZBAN_INSTALL,,}" == "y" ]]; then 
echo '    "watch": "concurrently \"sass --watch --update --style=expanded miz/_index.scss:assets/css/miz.min.css\" \"node miz/pkg/miz-controller.js --watch\" \"sass --watch --update --style=expanded sass/style.scss:assets/css/style.min.css\" \"sass --watch --update --style=expanded miz/_index.scss:assets/vendors/mizban/compile-css/css/miz.min.css\"",';
echo '    "build": "concurrently \"sass --no-source-map --style=compressed miz/_index.scss:assets/css/miz.min.css\" \"node miz/pkg/miz-controller.js --build\" \"sass --no-source-map --style=compressed sass/style.scss:assets/css/style.min.css\" \"node ./assets/vendors/mizban/commands/listFiles.js ^&^& node ./assets/vendors/mizban/commands/extractVariables.js\" \"sass --no-source-map --style=compressed miz/_index.scss:assets/vendors/mizban/compile-css/css/miz.min.css\"",';
echo '    "clean": "sass --no-source-map --style=expanded miz/_index.scss:assets/css/miz-clean.css && node miz/pkg/miz-min.cjs && sass --no-source-map --style=compressed miz/_index.scss:assets/vendors/mizban/compile-css/css/miz.min.css",';
echo '    "dev": "node miz/pkg/server.js"';
    else 
echo '    "watch": "concurrently \"sass --watch --update --style=expanded miz/_index.scss:assets/css/miz.min.css\" \"node miz/pkg/miz-controller.js --watch\" \"sass --watch --update --style=expanded sass/style.scss:assets/css/style.min.css\"",';
echo '    "build": "concurrently \"sass --no-source-map --style=compressed miz/_index.scss:assets/css/miz.min.css\" \"node miz/pkg/miz-controller.js --build\" \"sass --no-source-map --style=compressed sass/style.scss:assets/css/style.min.css\"",';
echo '    "clean": "sass --no-source-map --style=expanded miz/_index.scss:assets/css/miz-clean.css && node miz/pkg/miz-min.cjs",';
echo '    "dev": "node miz/pkg/server.js"';

    fi)
    },
    "author": "",
    "license": "ISC",
    "devDependencies": {
        "sass": "^1.77.8",
        "concurrently": "^9.0.0",
        "terser": "^5.28.1"
    },
    "dependencies": {
        "ncp": "^2.0.0"
    }
}
EOL

MIZIGNORE_PATH="$LOCATION_INSTALLER/$PROJECT_NAME/.mizignore"

cat <<EOL > "$MIZIGNORE_PATH"
./node_modules
./miz
./assets
EOL

if [ $? -eq 0 ]; then
    :
else
    echo -e "\e[32m"
    cd "$LOCATION_INSTALLER"
    rm -f "$PROJECT_NAME/package.json"
    rm -f "$PROJECT_NAME/.mizignore"
    rm -rf "$PROJECT_NAME/miz"
    rm -rf "$PROJECT_NAME/sass"
    rm -rf "$PROJECT_NAME/assets"
    rm -rf "$PROJECT_NAME"
    echo You have a bug in miz installation, please check the following.
	echo 1- Check your internet.
	echo 2- Turn off your proxy or vpn.
    echo 3- Open the installer file again.
    echo 4- If none of this works, you can download the raw file from the link.
    echo Otherwise, email mizframework59@gmail.com
    echo "Do you want open link ?(Y/n)"
    read -p "" CHECK_OPEN_LINK

    if [[ "${CHECK_OPEN_LINK,,}" == "n" ]]; then
        # nothing to do  
        exit
        :
    elif [[ "${CHECK_OPEN_LINK,,}" == "y" ]]; then
        start https://eazymizy.com
        exit
    else
        echo "Please use Y or n"
    fi
fi

npm install

echo -e "\e[32m"
echo -ne "\033]0;INSTALL-miz\007"

if [ $? -eq 0 ]; then
    :
else
    echo -e "\e[32m"
    cd "$LOCATION_INSTALLER"
    rm -rf "$PROJECT_NAME/node_modules"
    rm -f "$PROJECT_NAME/package-lock.json"
    rm -f "$PROJECT_NAME/package.json"
    rm -f "$PROJECT_NAME/.mizignore"
    rm -rf "$PROJECT_NAME/miz"
    rm -rf "$PROJECT_NAME/sass"
    rm -rf "$PROJECT_NAME/assets"
    rm -rf "$PROJECT_NAME"
    echo You have a bug in miz installation, please check the following.
	echo 1- Check your internet.
	echo 2- Turn off your proxy or vpn.
    echo 3- Open the installer file again.
    echo 4- If none of this works, you can download the raw file from the link.
    echo Otherwise, email mizframework59@gmail.com
    echo "Do you want open link ?(Y/n)"
    read -p "" CHECK_OPEN_LINK

    if [[ "${CHECK_OPEN_LINK,,}" == "n" ]]; then
        # nothing to do  
        exit
        :
    elif [[ "${CHECK_OPEN_LINK,,}" == "y" ]]; then
        start https://eazymizy.com
        exit
    else
        echo "Please use Y or n"
    fi
fi

npm run build-miz | sed 's/^/\x1b[32m/;s/$/\x1b[0m/'

echo -e "\e[32m"
echo -ne "\033]0;INSTALL-miz\007"


while true; do
    echo "Is your website 1. ltr or 2. rtl?"
    read -p "Enter your choice: " number

    case "$number" in
        1) direction="ltr"; language="en"; break ;;
        2) direction="rtl"; language="fa"; break ;;
        *) echo "Please use numbers" ;;
    esac
done

cat <<EOL > index.html
<!DOCTYPE html>
<html lang="$language" dir="$direction">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>miz</title>
        <link rel="stylesheet" href="./assets/css/miz.min.css">
        <link rel="stylesheet" href="./assets/css/style.min.css">
        <link rel="stylesheet" href="./assets/icons/fontawesome/css/all.min.css">
EOL

if [[ "${MIZBAN_INSTALL,,}" == "y" ]]; then
cat <<EOL >> index.html
        <script defer src="./assets/vendors/mizban/script.js"></script>
EOL
fi

cat <<EOL >> index.html
        <link rel="icon" type="image/x-icon" href="./assets/media/images/logo.png">
    </head>
    <body>
    </body>
</html>
EOL


cat <<EOL > components.html
<!DOCTYPE html>
<html lang="$language" dir="$direction">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>miz</title>
        <link rel="stylesheet" href="./assets/css/miz.min.css">
        <link rel="stylesheet" href="./assets/css/style.min.css">
	    <link rel="stylesheet" href="./assets/icons/fontawesome/css/all.min.css">
        <link rel="icon" type="image/x-icon" href="./assets/media/images/logo.png">
    </head>
    <body>
    </body>
</html>
EOL

if [ $? -eq 0 ]; then
    :
else
    cd "$LOCATION_INSTALLER"
    rm -f "$PROJECT_NAME/index.html"
    rm -f "$PROJECT_NAME/components.html"
    rm -rf "$PROJECT_NAME/node_modules"
    rm -f "$PROJECT_NAME/package-lock.json"
    rm -f "$PROJECT_NAME/package.json"
    
    rm -f "$PROJECT_NAME/.mizignore"
    rm -rf "$PROJECT_NAME/miz"
    rm -rf "$PROJECT_NAME/sass"
    rm -rf "$PROJECT_NAME/assets"
    rm -rf "$PROJECT_NAME"
    echo You have a bug in miz installation, please check the following.
	echo 1- Check your internet.
	echo 2- Turn off your proxy or vpn.
    echo 3- Open the installer file again.
    echo 4- If none of this works, you can download the raw file from the link.
    echo Otherwise, email mizframework59@gmail.com
    echo "Do you want open link ?(Y/n)"
    read -p "" CHECK_OPEN_LINK

    if [[ "${CHECK_OPEN_LINK,,}" == "n" ]]; then
        # nothing to do  
        exit
        :
    elif [[ "${CHECK_OPEN_LINK,,}" == "y" ]]; then
        start https://eazymizy.com
        exit
    else
        echo "Please use Y or n"
    fi
fi

read -p "Press any key to continue..."
cd ..
NAME_SHELL=$(basename "$0")
rm -f "$NAME_SHELL"

read -p "Press any key to continue..."