#!/bin/bash
#
#Script will download a graph image from the search URL
#
#Usage nf_getgraph <SearchURL> <Output file>
#
#


# Options

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:.

#
# Modify URL, set correct host name and id_c and stat to match your configuration
# or use the right URL as program argument
#
DEFAULTURL="http://netflow.yourdomain.org/netflow/login.php?auth=1&u_name=admin&u_pass=nfadmin&redirect=/netflow/src/data/trends.php&id_c=1&stat=conb&search=1"


# wget binary
WGET="/usr/bin/wget"
WGETOPT="--convert-links -nd -e robots=off -A*.php* -p --keep-session-cookies --save-cookies cookie"




# Main Program

CURDIR=`pwd`

URL="$1"
OUTPUT="$2"
if [ -z "$URL" ]; then
  URL="$DEFAULTURL"
fi
if [ -z "$OUTPUT" ]; then
  OUTPUT="/tmp/graph/png"
fi


if [ ! -x "$WGET" ]; then
  echo "Error: wget program not found. Please, use the right path. Now, using: $WGET"
  exit 1
fi

tmpd=$(mktemp -d)
if [ -z "$tmpd" ]; then
  echo "Error: mktemp failed!"
  exit 1
fi
cd "$tmpd"


#Download files and suppress the output
$WGET $WGETOPT $URL &>/dev/null


#Move the graph to the output directory
mv graph.php* "$OUTPUT"

# Drop temporary folder and restore actual directory
rm -rf "$tmpd"
cd "$CURDIR"

# List of files in /tmp
#echo "ls -la /tmp | grep graph"

exit 0
