#!/bin/bash

#
# Purpose: Rebuild gentoo kernel and all modules packages
#          I always forget some package that installs a module
#          like nvidia, or madwifi, and then the system won't boot right.
# By:      john@stilen.com
# License: GPLv2

# 
# Function lets us ask the user if they want to do each step
# Takes Y or y for Yes, and N or n for No
# Returns 1 for yes, 0 for no.
#
function YorN {
  echo -n "Should I do this step? (y|n): "
  read Answer
  if [[ "$Answer" == [y,Y] ]]; then
    echo "Yes it is"
    return 1
  elif [[ "$Answer" == [n,N] ]]; then 
    echo "No it is"
    return 0
  else
    echo "Not Y or N.  Try again (y|n): "
    YorN
  fi
} 

#
# Checks the exit status of the last command
# If exit status was not good, quit program
# A safe script should do this after each set of commands
#
function Check_Exit_Status {
  if [[ $? != "0" ]]; then
    echo "failed."
    exit 1
  else
    echo "success."
  fi
} 

#
#  Find the name of the old verison
#  we use this to search /lib/modules 
#  for pakcages that own modules
#
function GetOldVersion {
  OldVersion=`uname -r`
  echo -n "What version are we upgrading from? [$OldVersion] "
  read TestOld;
  #
  # if 
  #
  if [[ ! "$TestOld" == "" ]]; then
    echo "Checking: $TestOld"
    if [ -d "/usr/src/linux-$TestOld/" ]; then
      echo "using: $OldVersion"
    else
      echo "Not found: /usr/src/linux-$OldVersion"
      GetOldVersion
    fi
  else
    echo "using: $OldVersion"
  fi
}
GetOldVersion

#
# Find the name of the version we upgrade to
# If it doesn't exist, ask again again
#
function GetNewVersion {
  echo -n "What is the rev we are upgrading to (i.e. 2.6.23-gentoo-r8)? "
  read NewVersion
  #
  # if dirrectory eists, go on, 
  # but if not keep asking
  # If some other error occurs, exit
  #
  if [ ! -d /usr/src/linux-$NewVersion ]; then
    echo "Version does not exist."
    ls -ltr /usr/src/
    GetNewVersion
  elif [ -d "/usr/src/linux-$NewVersion" ]; then
    echo "Found $NewVersion"
  else
    echo "Error: problem with testing directory. Exiting."
    exit 1
  fi
}
GetNewVersion

#
#  Copy config from old kernel to new kernel
#
echo -n "Should I copying the current .config from old kernel? "
YorN
if [[ YorN == "1" ]]; then
  cp /usr/src/linux-$OldVersion/.config /usr/src/linux-$NewVersion/ \
  && cd /usr/src/linux-$NewVersion/ \
  && echo "Running make oldconfig" \
  && make oldconfig 
  Check_Exit_Status
fi

#
# Link to new verison
#
echo -n "Should I point /usr/src/linux to the new kernel? " 
if [[ YorN == "1" ]]; then
  pushd /usr/src/ \
  && unlink linux \
  && ln -s linux-$NewVersion linux \
  && popd
  Check_Exit_Status
fi

#
#  Now we are ready to build the kernel
#
echo -n "Should we build the kernel?"
if [[ YorN == "1" ]]; then
  if [[ -d "/etc/splash/livecd-2007.0" ]]; then 
    Splash="--gensplash=livecd-2007.0"
  else
    Splash="--no-splash"
  fi
  genkernel --menuconfig --no-clean $Splash all
  Check_Exit_Status
fi

#
#  Now we rebuild all the modules
#
packages=`equery belongs /lib/modules/$OldVersion`

#
# We need to put an = on the beginning of each line
# Since we are speicfying a rev number
# emerge will require an = prepended to each package
#
Emerge_List=""
for package in $packages; do
  Emerge_List="$Emerge_List =$package"
done
#
# Show what we are going to emerge
#
echo "Going to emerge:"
for i in $Emerge_List; do
  echo "$i"
done

echo -n "Should these modules be built? "
YorN
if [[ $? == "1" ]]; then
  emerge $Emerge_List 
  Check_Exit_Status
fi

echo "All done"
