#!/bin/bash

# weather: A very simple script to fetch weather from a specific location.
# This shell script was written by Mortuza Ahmmed.

usage () {
    echo "Usage: $(basename $0) location" 2>&1
    exit
}

if [[ ($# -gt 0) && ($# -lt 2) ]]; then

    condition=$(curl -s "wttr.in/$1?format=%C,(%c)\n")
    temperature=$(curl -s "wttr.in/$1?format=%t\n")
    feels=$(curl -s "wttr.in/$1?format=%f\n")
    humidity=$(curl -s "wttr.in/$1?format=%h\n")
    wind=$(curl -s "wttr.in/$1?format=%w\n")
    moon_condition=$(curl -s "wttr.in/$1?format=%m\n")
    moon_days=$(curl -s "wttr.in/$1?format=%M\n")
    precipitation=$(curl -s "wttr.in/$1?format=%p\n")
    pressure=$(curl -s "wttr.in/$1?format=%P\n")
    sunrise=$(curl -s "wttr.in/$1?format=%S\n"|cut -d ":" -f 1,2)
    sunset=$(curl -s "wttr.in/$1?format=%s\n"|cut -d ":" -f 1,2)
    zenith=$(curl -s "wttr.in/$1?format=%z\n" |cut -d ":" -f 1,2)
    time=$(curl -s "wttr.in/$1?format=%T\n" | cut -d ":" -f 1,2)
    printf "\n"
    echo "The weather in '$1' today: "
    echo -e "=============================="
    cat <<- _EOF_
        Time: $time
Condition: $condition
Temperature: $temperature feels like $feels
Humidity: $humidity
Wind: $wind
Moon Condition: $moon_condition, $moon_days days
Precipitation: $precipitation (mm/3hours)
Pressure: $pressure (hPa)
Sunrise: $sunrise
Sunset: $sunset
Zenith: $zenith
_EOF_

else
    usage
fi

