#!/bin/bash

# Automatic download, compile, and install of new Fl* code
#
# 03/24/16 - Added automatic build mode
# 11/11/15 - Updated to use SF URLs
# 09/14/15 - updated to display proper RPM filename
# 02/03/14 - Added root compiling check
# 02/03/13 - Added old/new version checking
# 12/20/12 - version sort now using -n to be more accurate for say flamp-1.0.01
#          - Adding duplicate download check
# 11/11/12 - Script made more universal
# 05/15/11

#program to fetch and compile
PROG="flwrap"

#PATHS
DLPATH="/usr/src/redhat/SOURCES/"
SPECPATH="/usr/src/redhat/SPECS/"
CHKURL="https://sourceforge.net/projects/fldigi/rss?path=/$PROG"
DLURL="http://downloads.sourceforge.net/project/fldigi/$PROG/"

#
#For Alpha builds only
#DLURL="http://www.w1hkj.com/alpha/flrig/"
#
#For specials
#DLURL="http://www.w1hkj.com/alpha/test/"
#DLURL="http://www.w1hkj.com/alpha/dranch/"

#Tools
CP="/bin/cp"
RPM="/bin/rpm"

#Automatically download and build if newer found?
AUTOMATIC=1

#--------------------
#Variables

#var to track if there is a new package to install
NEWPKG=0


#--------------------

if [ $UID -eq 0 ]; then
   echo -e "\nYou should not be root to run this command"
   echo -e "Exiting\n"
   exit 1
fi


echo -en "\nDeterming current installed version of program: $PROG - "
INSTALLEDRPM="`rpm -qa | grep $PROG | awk -F - '{print $2}'`"
echo -e "$INSTALLEDRPM"

echo -en "Determing available version of program:         $PROG - "
#determine newest version of Flrig
#FLRIGNEWVERSION="`links --dump $DLURL | grep "tar.gz" | grep \* | \
#grep -iv elp | sort -r | head -n1 | awk -F ] '{print $2}'`"
FLPROGNEWVERSION="`wget -q -O - $CHKURL | grep title | grep $PROG- | grep tar.gz | awk -F/ '{print $3}' | awk -F] '{print $1}'`"

if [ -z $FLPROGNEWVERSION ]; then
   echo -e "\nERROR: couldn't determine current newest version of $PROG"
   exit 1
  else
   echo -e "$FLPROGNEWVERSION" | awk -F - '{print $2}' | awk -F . '{print $1"."$2"."$3}'
   CLEARNEWVERSION="`echo -e "$FLPROGNEWVERSION" | awk -F - '{print $2}' | awk -F . '{print $1"."$2"."$3}'`"
fi

if [ $AUTOMATIC -eq 0 ]; then
   echo -e "Automatic build: Disabled"
   echo -en "\nProceed to download new code version [y|n]: "
   read USERINPUT
   if [ $USERINPUT = "n" ]; then
      echo -e "\nDownload aborted.  Exiting\n"
      exit 0
   fi
  else
      echo -e "\nAutomatic build: Enabled"
      if [ $INSTALLEDRPM == $CLEARNEWVERSION ]; then
         echo -e "   No new version availale.. skipping\n"
         exit 1
        else
         echo -e "   New version available.  Starting..\n"
      fi
fi

cd $DLPATH
if [ -f $FLPROGNEWVERSION ]; then
   echo -e "$FLPROGNEWVERSION already exists.. skipping the re-download"
  else
   echo -e "\nGetting $DLURL/$FLPROGNEWVERSION and putting it in $DLPATH\n------------------------------------------------------------------\n"
   wget $DLURL/$FLPROGNEWVERSION
fi

echo -e "\n"
cd $SPECPATH
#Make a backup of the previous SPEC file
$CP -vf $PROG.spec Old/$PROG-`grep Version $PROG.spec | awk '{print $2}'`.spec

echo -e "\nReady to manually update the spec file?\n[Press any key]\n"
read ANYKEY

vim $PROG.spec


echo -e "\nCompiling $PROG RPM"
if [ "`/bin/uname -m`" = "i686" ]; then
   echo -e "Found i686"
   rpmbuild -bb --target i686 $PROG.spec
   if [ $? -ne 0 ]; then
      echo -e "\n------------------------------------------"
      echo -e "Error: $PROD did not successfully compile!"
      echo -e "\nExiting!"
      echo -e "------------------------------------------\n"
      exit 1
   fi
   #New package made, enable flag
   NEWPKG=1
   echo -e "\nAssuming all went well, please manually upgrade the installed RPM with"
   echo -e "\n    sudo rpm -Uvh /usr/src/redhat/RPMS/i686/$FLPROGNEWVERSION" | sed s/.tar.gz/-1.el6.x86_64.rpm/
   #If run from the update-fl-suite.sh script, provide one file to run and do cleanups
   DATE="`date +%m%d%y`"
   if [ ! -f /tmp/fl-update-$DATE.sh ]; then
      echo -e "sudo rpm -Uvh /usr/src/redhat/RPMS/i686/$FLPROGNEWVERSION" | sed s/.tar.gz/-1.el6.x86_64.rpm/ > /tmp/fl-update-$DATE.sh
     else
      #Append
      echo -e "sudo rpm -Uvh /usr/src/redhat/RPMS/i686/$FLPROGNEWVERSION" | sed s/.tar.gz/-1.el6.x86_64.rpm/ >> /tmp/fl-update-$DATE.sh
   fi
   chmod 700 /tmp/fl-update-$DATE.sh

  elif [ "`/bin/uname -m`" = "x86_64" ]; then
   echo -e "Found x86_64"
   rpmbuild -bb --target=x86_64 $PROG.spec
   if [ $? -ne 0 ]; then
      echo -e "\n------------------------------------------"
      echo -e "Error: $PROD did not successfully compile!"
      echo -e "\nExiting!"
      echo -e "------------------------------------------\n"
      exit 1
   fi
   #New package made, enable flag
   NEWPKG=1
   echo -e "\nAssuming all went well, please manually upgrade the installed RPM with"
   echo -e "\n    sudo rpm -Uvh /usr/src/redhat/RPMS/x86_64/$FLPROGNEWVERSION" | sed s/.tar.gz/-1.el6.x86_64.rpm/
   #If run from the update-fl-suite.sh script, provide one file to run and do cleanups
   DATE="`date +%m%d%y`"
   if [ ! -f /tmp/fl-update-$DATE.sh ]; then
      echo -e "sudo rpm -Uvh /usr/src/redhat/RPMS/x86_64/$FLPROGNEWVERSION" | sed s/.tar.gz/-1.el6.x86_64.rpm/ > /tmp/fl-update-$DATE.sh
     else
      #Append
      echo -e "sudo rpm -Uvh /usr/src/redhat/RPMS/x86_64/$FLPROGNEWVERSION" | sed s/.tar.gz/-1.el6.x86_64.rpm/ >> /tmp/fl-update-$DATE.sh
   fi
   chmod 700 /tmp/fl-update-$DATE.sh
fi

echo -e "\n\nDone!\n\n"
if [ $NEWPKG -eq 1 ]; then
   #indicate we have a new package
   exit 100
  else
   exit 0
fi
