#!/bin/bash

if [ "$#" -lt 3 ]; then
    echo "Usage: $0 <url> <local_file> <keyword>"
    exit 1
fi

url=$1
local_file=$2
keyword=$3

temp_file=$(mktemp)

cleanup() {
    rm -f "$temp_file"
}

trap cleanup EXIT

if curl -s -o "$temp_file" "$url"; then
    if grep -q "$keyword" "$temp_file"; then
        mv "$temp_file" "$local_file"
        echo "File successfully downloaded and validated. Local file has been updated."
    else
        echo "Keyword not found in the downloaded file. Local file has not been updated."
    fi
else
    echo "Failed to download the file from the URL. Local file has not been updated."
fi
