#!/bin/sh
#
# build_sparc_kernels.sh (based on compile_kern.sh by Patrick Volkerding)
# version 1.8
# by:  David Cantrell <david@slackware.com>
#
# Updated by Chris Newport crn(at)netunix.com 31 Oct 2004 
#
# Updated by Jason on 31 Aug 2005
#
# Builds a tree of directories containing a System.map, .config (renamed
# config), and kernel image.  The input is a tree of directories beneath
# the current one with names matching the wildcard sun*.  At the minimum,
# each directory must contain the file 'config', which is the .config to
# start with when building the kernel.
#

# Be verbose?
if [ ! "$VERBOSE" = "" ]; then
  set -x
fi

######################################
#
# Assumes that you have a 64 bit (UltraSparc) machine to do the build.
# Building a whole set of kernels on a 32 bit machine would take a long
# time and would be tricky because you need the 64 bit compilers and 
# libs to make the sun4u versions.
# 
#######################################

# global variables
CWD=`pwd`
KERNEL=2.4.31
PREVER=
NEWVER=2.4.31
KERNELSRC=$CWD/../source/k/linux-${KERNEL}.tar.bz2
TMP=/tmp
OUTPUT=$TMP/output

if [ ! "`uname -m`" = "sparc64" ]; then
  echo
  echo "You really want to run this on an Ultra in 64-bit mode..."
  echo "Try this:"
  echo "  sparc64 $0"
  echo
  exit
fi


# Extract the kernel source
if [ ! -d $TMP/linux-${NEWVER} ]
then
   ( cd $TMP
     tar xvjf $KERNELSRC
   )
   # Apply any regular source patches
   for p in `(cd $CWD/../source/k/patches-splack/; ls *diff.gz)`
   do
     ( cd $TMP/linux-${NEWVER}
       zcat $CWD/../source/k/patches-splack/$p | patch -p1
     )
   done
   
   # Apply preview release patches, if we've got'em
   if [ ! "$PREVER" = "" ]
   then
     (  cd $TMP/linux-${NEWVER}
        bzip2 -dc $CWD/../source/k/linux/pre-patch-${PREVER}.bz2 | patch -p1
     )
   fi
fi

# make our output directory
mkdir -p $OUTPUT

# backup existing modules directory
if [ -d /lib/modules/$NEWVER ]
then
   rm -rf /lib/modules/$NEWVER.kernels_build
   mv /lib/modules/$NEWVER /lib/modules/$NEWVER.kernels_build
fi

# Make a copy of the original Makefile
( cd $TMP/linux-${NEWVER}
  cat Makefile > Makefile.dist
)


# build the kernels
for d in *.?
do
   if [ ! -r $d/config.in ]
   then
      echo "Can't find a config for $d!  Skipping..."
      continue
   fi

   if [ -r $OUTPUT/$d/$NEWVER/vmlinux ]
   then
      echo "kernel found in $OUTPUT/$d... skipping."
   #   sleep 1
      continue
   fi

   cd $TMP/linux-${NEWVER}

   echo
   echo "------------------------------------------"
   echo "Cleaning up..."
   echo "------------------------------------------"
   make mrproper

   echo
   echo "------------------------------------------"
   echo " BUILDING: $d"
   echo "------------------------------------------"
   echo
   sleep 1

   # If we've got a Makefile we've edited, copy it in.
   if [ -r $d/Makefile.gz ]; then
     zcat $d/Makefile.gz > Makefile
   else
     # Use the default one
     cat Makefile.dist > Makefile
   fi

   # We can get away with using make oldconfig for 2.4.x
   # since there's not really any more feature changes...

   # do we need to build a SPARC or UltraSPARC kernel?
   if [ "`echo $d | grep sun4u`" = "" ]
   then
      # build a SPARC kernel
      sparc32 make mrproper
      rm -rf /lib/modules/$NEWVER
      cp $CWD/$d/config.in .config
      sparc32 make oldconfig
      sparc32 make dep
      sparc32 make clean
      sparc32 make vmlinux
      if [ $d != "javastation.n" ]
       then
        sparc32 make modules
        sparc32 make modules_install
       fi
   elif [ ! "`echo $d | grep sun4u`" = "" ]
   then
      # build an UltraSPARC kernel
      make mrproper
      rm -rf /lib/modules/$NEWVER
      cp $CWD/$d/config.in .config
      make oldconfig
      make dep
      make clean
      make vmlinux
      make modules
      make modules_install
   fi

   if [ $? = 0 ]
   then
      # good build
      echo
      echo "------------------------------------------"
      echo "Saving output in $OUTPUT/$d"
      echo "------------------------------------------"
      echo
      if [ -d $OUTPUT/$d/$NEWVER ]
      then
         rm -rf $OUTPUT/$d/$NEWVER
      fi
      mkdir -p $OUTPUT/$d/$NEWVER/
      strip vmlinux
      cp vmlinux $OUTPUT/$d/$NEWVER/
      # GZipping doesn't matter on SPARCs,
      #  if it doesn't work uncompressed
      #  then it won't even if it is.
      # gzip -9 $OUTPUT/$d/vmlinux
      cp System.map $OUTPUT/$d/$NEWVER/
      cp .config $OUTPUT/$d/$NEWVER/config
      cp .config $OUTPUT/$d/config.in
      if [ $d != "javastation.n" ]
       then
        rm -rf /lib/modules/$NEWVER/build
        ln -s /usr/src/linux-$NEWVER /lib/modules/$NEWVER/build
        cp -a /lib/modules/$NEWVER $OUTPUT/$d/$NEWVER/modules
        tar -cf $OUTPUT/$d/$NEWVER/modules.tar /lib/modules/$NEWVER
        gzip -9 $OUTPUT/$d/$NEWVER/modules.tar
        rm -rf /lib/modules/$NEWVER
       fi
   fi

   cd $CWD
done

# we don't want the build symlink in this tree
find $OUTPUT -type l -exec rm {} \;

# restore existing modules directory
if [ -d /lib/modules/$NEWVER.kernels_build ]
then
   rm -rf /lib/modules/$NEWVER
   mv /lib/modules/$NEWVER.kernels_build /lib/modules/$NEWVER
fi
