#!/bin/bash

# Automatic download, compile, and install of new Fl* code
#
# 03/24/16 - Added automatic build mode
# 09/30/15 - new entry
# 09/14/15 - minor cleanups for showing right RPM files, etc
# 02/03/14 - Added root compiling checking
# 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="flxmlrpc"

#PATHS
DLPATH="/usr/src/redhat/SOURCES/"
SPECPATH="/usr/src/redhat/SPECS/"

#Log path
FLPREFFILE="/home/dranch/.fldigi/fldigi.prefs"
FLLOGFILE="/home/dranch/.fldigi/logs/logbook.adif"
FLDEFFILE="/home/dranch/.fldigi/fldigi_def.xml"

#For major releases
DLURL="http://www.w1hkj.com/downloads/$PROG/"
#
#For Alpha builds only
#DLURL="http://www.w1hkj.com/alpha/$PROG/"

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

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

# Is this a build from a remote URL or local
# 0 = remote tarball
# 1 = local tarball only
MANUALRUN=0

#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

#override if specified on the command line
if [ -n "`echo $0 | grep manual`" ]; then
   echo -e "\nThis is a manual run"
   MANUALRUN=1
fi

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

if [ $MANUALRUN -eq 0 ]; then
   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="`links --dump $DLURL | grep "tar.gz" | grep \* | grep -iv elp \
   | awk -F ] '{print $2}' | sort -r -n | head -n1`"

   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

   #echo "DEBUG: INSTALLEDRPM: $INSTALLEDRPM"
   #echo "DEBUG: CLEARNEWVERSION: $CLEARNEWVERSION"

   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
fi

cd $DLPATH
if [ -f $FLPROGNEWVERSION ]; then
   echo -e "$FLPROGNEWVERSION already exists.. skipping the re-download"
  else
   if [ $MANUALRUN -eq 1 ]; then
      echo -e "\nERROR: source file $FLPROGNEWVERSION is not present"
      exit 1
   fi
   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
    else
      #New package made, enable flag
      NEWPKG=1
      echo -e "\nPlease 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
fi

echo -e "\nMaking a backup of your log file just in case"
DATE="`date +%m%d%y`"
cp $FLPREFFILE $FLPREFFILE-$DATE.bak
cp $FLLOGFILE $FLLOGFILE-$DATE.bak
cp $FLDEFFILE $FLDEFFILE-$DATE.bak

#ls -la *-$DATE.bak

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