#!/bin/bash
#
# Copyright 2013 Red Hat
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

set -eu

SCRIPT_NAME=$(basename $0)

function show_options {
    echo "Usage: $SCRIPT_NAME IRC_CHANNEL IRC_USERNAME MESSAGE..."
    echo
    echo "Send a MESSAGE to a freenode channel IRC_CHANNEL from"
    echo "the user IRC_USERNAME"
    echo "Examples:"
    echo "    $SCRIPT_NAME tripleo toci "WARNING : The build failed""
    exit 1
}

[ $# -lt 3 ] && show_options

exec 3<>/dev/tcp/irc.freenode.net/6667

IRC_CHANNEL=$1
IRC_USERNAME=$2
shift 2
MESSAGE=$@

echo "Nick $IRC_USERNAME" >&3
echo "User $IRC_USERNAME -i * : hi" >&3
sleep 2
echo "JOIN #$IRC_CHANNEL" >&3
echo "PRIVMSG #$IRC_CHANNEL :$@" >&3
echo "QUIT" >&3

cat <&3 > /dev/null

