#!/bin/bash

# Unzip the project
unzip scrappy.zip -d scrappy_project
cd scrappy_project

# Create a requirements.txt file if it doesn't exist
if [ ! -f "requirements.txt" ]; then
    echo "requirements.txt not found. Creating a new one..."
    echo "scrapy" > requirements.txt
    echo "pymysql" >> requirements.txt
fi

# Install Python3 if not installed (macOS/Linux)
if ! command -v python3 &> /dev/null
then
    echo "Python3 could not be found. Installing Python3..."
    if [[ "$OSTYPE" == "darwin"* ]]; then
        # macOS
        brew install python3
    elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
        # Linux
        sudo apt-get update
        sudo apt-get install python3 python3-pip -y
    fi
fi

# Install virtualenv if not installed
if ! command -v virtualenv &> /dev/null
then
    echo "virtualenv could not be found. Installing virtualenv..."
    pip3 install virtualenv
fi

# Create virtual environment
virtualenv venv

# Activate virtual environment
source venv/bin/activate

# Install required packages
pip install -r requirements.txt

# Provide instructions to run the Scrapy project
echo "To run the Scrapy spider, use the following commands:"
echo "source venv/bin/activate"
echo "scrapy crawl your_spider_name"

# Deactivate the environment after installation
deactivate

echo "Installation completed successfully."



######################################################################################################################
         Windows
######################################################################################################################

@echo off

:: Unzip the project
powershell -Command "Expand-Archive -Path scrappy.zip -DestinationPath scrappy_project"
cd scrappy_project

:: Create a requirements.txt file if it doesn't exist
if not exist requirements.txt (
    echo requirements.txt not found. Creating a new one...
    echo scrapy > requirements.txt
    echo pymysql >> requirements.txt
)

:: Install Python3 if not installed (Windows)
where /r c:\ python3 >nul 2>nul
if %ERRORLEVEL% neq 0 (
    echo Python3 could not be found. Please install Python3 from https://www.python.org/downloads/
    exit /b
)

:: Install virtualenv if not installed
pip show virtualenv >nul 2>nul
if %ERRORLEVEL% neq 0 (
    echo virtualenv could not be found. Installing virtualenv...
    pip install virtualenv
)

:: Create virtual environment
virtualenv venv

:: Activate virtual environment
call venv\Scripts\activate

:: Install required packages
pip install -r requirements.txt

:: Provide instructions to run the Scrapy project
echo To run the Scrapy spider, use the following commands:
echo venv\Scripts\activate
echo scrapy crawl your_spider_name

:: Deactivate the environment after installation
deactivate

echo Installation completed successfully.
pause




#########################################
instructions
#########################################
1-For macOS/Linux:
bash install.sh

2- For Windows
Double-click install.bat.