#!/bin/bash
#
# (C) Nouveau team. Scripted by KoalaBR
#
# Maybe we should integrate JussiP's check whether
# the dumps is available and current?
echo -ne "Checking for dialog"
type dialog 2>/dev/null 1>/dev/null
if [ $? -eq 0 ] ; then
   echo -e "\t\t[OK]"
else
   echo -e "\t\t[KO]"
   echo "Please install the dialog package"
   exit 0
fi
if [ "x$DISPLAY" != "x" ] ; then
   echo -e "Checking whether running in X\t[OK]"
else
   echo -e "Checking whether running in X\t[KO]"
   echo "Please start X, open a X-terminal and start this script again"
   exit 0
fi
echo -ne "Checking whether NVidia is loaded\t"
cat /proc/driver/nvidia/version 2>/dev/null 1>/dev/null
if [ $? -eq 0 ] ; then
  echo -e "[OK]"
else 
  echo -e "[KO]"
  echo "Please do install and load the nvidia binary driver"
  exit 0
fi
echo -ne "Checking for gcc\t\t"
type gcc 1>/dev/null 2>/dev/null
if [ $? -eq 0 ] ; then
   echo -e "[OK]"
else 
   echo -e "[KO]"
   echo "Please install gcc"
   exit 0
fi
echo -ne "Checking for make\t\t"
type make 1>/dev/null 2>/dev/null
if [ $? -eq 0 ] ; then
   echo -e "[OK]"
else 
   echo -e "[KO]"
   echo "Please install make"
   exit 0
fi
echo -ne "Checking for cvs\t\t"
type cvs 1>/dev/null 2>/dev/null
if [ $? -eq 0 ] ; then
   echo -e "[OK]"
else 
   echo -e "[KO]"
   echo "Please install cvs"
   exit 0
fi
#
# Now a short explanation
#
dialog --yesno "This script will create a directory 'rentmp' in the current directory, download the current\
  renouveau, compile and run it. You'll need a active internet connection. Select 'Yes' to continue, 'No' to abort." 9 50
if [ $? -eq 1 ] ; then 
  exit 0
fi

mkdir rentmp
cd rentmp
dialog --infobox "Downloading current renouveau version via cvs" 9 50
cvs -z3 -d:pserver:anonymous@nouveau.cvs.sourceforge.net:/cvsroot/nouveau co -P renouveau 2>cvs.txt 1>&2
if [ $? -ge 1 ] ; then
   echo "Download of CVS repository failed. Please check whether internet connection is up."
   echo "More information can be found in rentmp/cvs.txt"
   rm -rf rentmp/renouveau
   exit 0
fi
sleep 1 
dialog --infobox "Compiling renouveau. This may take a few minutes depending on your system's speed." 9 50
cd renouveau
make 2>make.txt 1>&2
if [ $? -ne 0 ] ; then
   dialog --yesno "Compiling renouveau failed. Choosing 'YES' will show you the log file of the compilation.\
  'NO' will abort. Please have a look at the trouble shoot section in our FAQ: http://nouveau.freedesktop.org/wiki/FAQ" 9 50
   if [ $? -eq 0 ] ; then 
      dialog --textbox make.txt 10 60
   fi
   exit
fi 

dialog --yesno "Compilation successful. Now starting renouveau. Please make sure that\n\
   - You have compiz or beryl disabled \n\
   - That you don't move the renouveau window \n\
   - That you don't hide the renouveau window behind other windows or menus\n\
 In case of questions look here: http://nouveau.freedesktop.org/wiki/FAQ. \n\
 Choosing 'NO' will abort this script in case you need to deactivate beryl / compiz" 14 60
mkdir cards 
cd cards
../renouveau
if [ $? -gt 0 ] ; then
  echo "Please report the above error. Press return to continue"
  read crlfdummy
  dialog --msgbox "renouveau terminated abnormally. Please to report back to our IRC channel." 9 50 
  exit 
fi
pciid=`grep /dev/nvidia card_stdout.txt | cut -d" " -f10 | head -1 | tr -t : _`
cd ..
tar cvfj "../Nvidia_${pciid}.tar.bz2" cards
dialog --msgbox "Please send the file rentmp/Nvidia_${pciid}.tar.bz2 to  renouveau.dumps@gmail.com \n\
   After that you can safely 'rm -rf rentmp' Thank you for helping us out" 9 50

