#!/bin/bash


#This script works but it's still a work in progress to merge into the Fldigi
#  update scripts


#program to fetch and compile
PROG="ardopc"


#Where the program is downloaded from
DLPATH="http://www.cantab.net/users/john.wiseman/Downloads/Beta/"
DLURL="http://www.cantab.net/users/john.wiseman/Downloads/Beta/TeensyProjects.zip"
#Where RPM spec files are kept
SPECPATH="/usr/src/redhat/SPECS"

#Use bleeding edge code
USEALPHA=0

# 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=0

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

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

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


echo -e "\nCurrent installed ARDOP version: "
rpm -qa | grep ardop | grep -v ardop-gui

echo -e "\nLOCAL TeensyProjects.zip file dated : "
#ls -lat /usr/src/redhat/SOURCES | grep TeensyProjects | head --lines=1
ls -la --time-style=full-iso /usr/src/redhat/SOURCES/TeensyProjects.zip | awk '{print $6 " "$7}'

echo -e "\nREMOTE TeensyProjects.zip file dated: "
#wget -O - http://www.cantab.net/users/john.wiseman/Downloads/Beta/ 2>&1 | grep TeensyProjects.zip | awk -F\> '{print $10}' | awk '{print $1" "$2}'
wget -O - $DLPATH 2>&1 | grep TeensyProjects.zip | awk '{print $7" "$8}' | awk -F\> '{print $2}'

echo -e "\nDo you want to proceed with the download and build?"
echo -e "Press any key to continue"
read ANYKEY

echo -e "\nGetting the following new code\n"
cd /usr/src/redat/SOURCES
wget http://www.cantab.net/users/john.wiseman/Downloads/Beta/TeensyProjects.zip
echo -e "\nDate stamping new archive"
cp TeensyProjects.zip TeensyProjects-`date +%m%d%y`.zip

echo -e "\nUnzipping raw TeensyProjects-`date +%m%d%y`.zip to extract ARDOP code"
cd /usr/src/redhat/BUILD
if [ -d TeensyProjects ]; then
   echo -e "\nDeleting old TeensyProjects dir\n"
   rm -Rf /usr/src/redhat/BUILD/TeensyProjects
fi

unzip ../SOURCES/TeensyProjects-`date +%m%d%y`.zip

echo -e "\nDetermine the version of ARDOP and make the naming convention compatible with RPMs"
# (-BPQ isn't allowed)
VER=`grep ProductVersion TeensyProjects/ARDOPC/ARDOPC.h | awk -F\" '{print $2}' | awk -F\- '{print $1}'`

#This should look like: 1.0.4.1b
echo -e "\nNew ARDOP version is: $VER"

echo -e "\nCreating versioned ARDOPC archive"
mv TeensyProjects/ARDOPC ardopc-$VER
tar czvf /usr/src/redhat/SOURCES/ardopc-$VER.tgz ardopc-$VER/*

rm -Rf ardopc-$VER

#Leaving the the original download file in place as reference
#rm -f /usr/src/redhat/SOURCES/TeensyProjects.zip
#mv /tmp/TeensyProjects.zip /usr/src/redhat/SOURCES


#echo -e "\nExit here for debugging"
#exit 0


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


##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 "\nDeterming 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="`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}'
#      if [ "`echo -e "$FLPROGNEWVERSION" | awk -F - '{print $2}' | awk -F . '{print $5}'`" = "tar" ]; then
#         CLEARNEWVERSION="`echo -e "$FLPROGNEWVERSION" | awk -F - '{print $2}' | awk -F . '{print $1"."$2"."$3"."$4}'`"
#         echo "$CLEARNEWVERSION"
#        elif [ "`echo -e "$FLPROGNEWVERSION" | awk -F - '{print $2}' | awk -F . '{print $4}'`" = "tar" ]; then
#         CLEARNEWVERSION="`echo -e "$FLPROGNEWVERSION" | awk -F - '{print $2}' | awk -F . '{print $1"."$2"."$3}'`"
#         echo "$CLEARNEWVERSION"
#      fi
#   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
#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
if [ $USEALPHA -eq 1 ]; then
   echo -e "Make a backup of the previous ALPHA SPEC file"
   $CP -vf $PROG-alpha.spec Old/$PROG-alpha-`grep Version $PROG.spec | awk '{print $2}'`.spec
  else
   echo -e "Make a backup of the previous RELEASE SPEC file"
   $CP -vf $PROG.spec Old/$PROG-`grep Version $PROG.spec | awk '{print $2}'`.spec
fi

#This should look like: 1.0.4.1b
echo -e "\nNew ARDOP version is: $VER"

echo -e "\nReady to manually update the spec file with new version: $VER?\n[Press any key]\n"
read ANYKEY

if [ $USEALPHA -eq 1 ]; then
   if [ ! -f $PROG-alpha.spec ]; then
      echo -e "\nERROR: No $PROG alpha spec file found.  Aborting\n"
      exit 1
   fi
   vim $PROG-alpha.spec
  else
   if [ ! -f $PROG.spec ]; then
      echo -e "\nERROR: No $PROG spec file found.  Aborting\n"
      exit 1
   fi
   vim $PROG.spec
fi


echo -e "\nCompiling $PROG RPM"
if [ "`/bin/uname -m`" = "i686" ]; then
   echo -e "Found i686"
   if [ $USEALPHA -eq 1]; then
      rpmbuild -bb --target i686 $PROG-alpha.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
      echo -e "\nAssuming all went well, please manually upgrade the installed RPM with"
      echo -e "\n    rpm -Uvh /usr/src/redhat/RPMS/i686/$FLPROGNEWVERSION"
     else
      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
      echo -e "\nAssuming all went well, please manually upgrade the installed RPM with"
      echo -e "\n    rpm -Uvh /usr/src/redhat/RPMS/i686/$FLPROGNEWVERSION"
   fi
  elif [ "`/bin/uname -m`" = "x86_64" ]; then
   echo -e "Found x86_64"
   if [ $USEALPHA -eq 1 ]; then
      rpmbuild -bb --target=x86_64 $PROG-alpha.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
         echo -e "\nPlease manually upgrade the installed RPM with"
         #echo -e "\n    sudo rpm -Uvh /usr/src/redhat/RPMS/x86_64/$PROG-$VER" | sed s/.tar.gz/-1.el6.x86_64.rpm/
         echo -e "\n    sudo rpm -Uvh `ls /usr/src/redhat/RPMS/x86_64/$PROG-$VER-*`"
      fi
     else
      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
         echo -e "\nPlease manually upgrade the installed RPM with"
         #echo -e "\n    sudo rpm -Uvh /usr/src/redhat/RPMS/x86_64/$PROG-$VER" | sed s/.tar.gz/-1.el6.x86_64.rpm/
         echo -e "\n    sudo rpm -Uvh `ls /usr/src/redhat/RPMS/x86_64/$PROG-$VER-*`"
      fi

   fi
fi

#echo -e "\nMaking a backup of key Fldigi files just in case"
#DATE="`date +%m%d%y`"
#echo -en "$FLPREFFILE "
#cp $FLPREFFILE $FLPREFFILE-$DATE.bak
#echo -en "$FLLOGFILE "
#cp $FLLOGFILE $FLLOGFILE-$DATE.bak
#echo -e "$FLDEFFILE "
#cp $FLDEFFILE $FLDEFFILE-$DATE.bak

#ls -la *-$DATE.bak

echo -e "\n\nDone!\n\n"

