#!/usr/local/bin/bash

usage() {
    echo "Usage: goesimage [OPTION...]
Download the latest image from a NOAA Geostationary Operational Environment Satellite and set it as the desktop background.

If multiple satellite are specified, the latest image will be downloaded from each and combined into a single, tiled image.

Options:
    -e      GOES-East
    -w      GOES-West
    -s      Specify the desired image size:
                small (678x678),
                medium (1808x1808, default),
                large (5424x5424),
                xlarge (10848x10848)
    -o      output the filename of the image but do not set it as the background
    -v      verbose output
    -h      display usage"
}

log() {
    if [ "$verbose" = true ]; then
        echo "$*"
    fi
}

execute() {
    log "Executing command: $*"
    if ! "$@"; then
        log "Failed to execute command: $*"
        exit 4
    fi;
}

get_size() {
    case $size_opt in
        "small")
            dimensions="678x678"
            ;;
        "large")
            dimensions="5424x5424"
            ;;
        "xlarge")
            dimensions="10848x10848"
            ;;
        *)
            dimensions="1808x1808"
            ;;
    esac
}

get_images() {
    filename_list=()
    for i in "${satellite_list[@]}"; do
        url="https://cdn.star.nesdis.noaa.gov/$i/ABI/FD/GEOCOLOR/$dimensions.jpg"
        filename="$i.jpg"
        filename_list+=("$filename")
        execute curl --silent --output "$filename" "$url"
    done
}

check_images() {
    for file in "${filename_list[@]}"; do
        if [ ! -s "$file" ]; then
            echo "Failed to download $file"
            exit 2
        fi
        filetype=$(file -bi "$file")
        if [[ ! "$filetype" =~ "image/jpeg" ]]; then
            echo "$file is not a jpeg image"
            exit 3
        fi
    done
}

process_images() {
    execute convert -border 20 -bordercolor black -background black +append "${filename_list[@]}" latest.jpg
}

init_dir() {
    if [ -n "$XDG_CACHE_DIR" ]; then
        output_dir="$XDG_CACHE_DIR"/goesimage
    else
        output_dir="$HOME"/.cache/goesimage
    fi
    mkdir -p "$output_dir"
    cd "$output_dir"
}

set_bg() {
    if [ "$output_only" = true ]; then
        echo "$output_dir/latest.jpg"
    else
        execute feh --bg-max "$output_dir/latest.jpg"
    fi
}

satellite_list=()
while getopts "s:ewovh" opt; do
    case $opt in
        s)
            size_opt=$OPTARG
            ;;
        w)
            satellite_list+=("GOES17")
            ;;
        e)
            satellite_list+=("GOES16")
            ;;
        o)
            output_only=true
            ;;
        v)
            verbose=true
            ;;
        h)
            usage
            exit
            ;;
        *)
            usage
            exit 1
            ;;
    esac
done

if [ ${#satellite_list[@]} -eq 0 ]; then
    echo 'Please specify at least one satellite.'
    exit 1
fi

init_dir
get_size
get_images
check_images
process_images
set_bg
