#!/bin/tcsh -f
# JLdL 03Jul13.
#
# This is for echoing all commands.
#!/bin/tcsh -fx
#
# This script updates the disable-init-param patch to a new version
# _ of the kernel; only changes in the last two kernel version
# _ numbers are supported. 
#
# Check for the necessary command-line parameter.
if ( "$1" == "" ) then
    echo ERROR: need one command-line parameter: the kernel version
    exit 1
endif
#
# Capture the version number to work on from the command line.
set nvers = "$1"
#
# Get the kernel class.
set class = `echo -n $nvers | cut -d. -f1-2`
#
# Fix for the new 3.x directory name.
if ( `echo $class | cut -d. -f1` == 3 ) then
    set class = 3.x
endif
#
# Define the version level that has changed;
# _ if the kernel class is 2, start with 4;
# _ if the kernel class is 3, start with 3.
if ( `echo $class | cut -d. -f1` == 2 ) then
    set vl = "4"
else
if ( `echo $class | cut -d. -f1` == 3 ) then
    set vl = "3"
else
    echo ERROR: only kernel classes 2 and 3 are supported
    exit 1
endif
endif
#
# Get the last version number.
set nv = `echo -n $nvers | cut -s -d. -f$vl`
#
# If that is not there, go to the previous level.
if ( "$nv" == "" ) then
    #
    # Get the next-to-last version number.
    @ vl = $vl - 1
    set nv = `echo -n $nvers | cut -s -d. -f$vl`
    #
endif
#
# Check whether the version level is OK.
if ( "$nv" == "" ) then
    echo ERROR: only the last two kernel version numbers are supported
    exit 1
endif
#
# Calculate the preceding version level.
@ pl = $vl - 1
#
# Calculate the previous last version number.
@ ov = $nv - 1
#
# Define the complete previous version number; however,
# _ if the previous last version number turns out to
# _ be zero, then get the initial version.
if ( $ov == 0 ) then
    set overs = `echo -n $nvers | cut -d. -f1-$pl`
else
    set overs = `echo -n $nvers | cut -d. -f1-$pl`.$ov
endif
#
#
# DEBUG output.
#echo nvers = $nvers
#echo class = $class
#echo vl = $vl
#echo nv = $nv
#echo pl = $pl
#echo ov = $ov
#echo overs = $overs
#
#
# DEBUG: defang the rest of this script.
#cat <<EOF
#
# Open a small part of the new kernel tree.
echo "Extracting the new kernel tree..."
tar -xjf /sft/kernel/v$class/linux-$nvers.tar.bz2 linux-$nvers/init/
#
# If this fails, then exit.
if ( $status != 0 ) then
    echo ERROR: cannot open kernel tree
    exit 1
endif
#
# Make an "orig" copy of the partial kernel tree.
echo "Making the '.orig' copy of the kernel tree..."
cp -a linux-$nvers/ linux-$nvers.orig/
#
# Copy the previous patch file as the new one;
# _ this is for keeping mode and ownership.
echo "Copying the previous patch file..."
cp -p disable-init-param-$overs.diff disable-init-param-$nvers.diff
#
# If this fails, then exit.
if ( $status != 0 ) then
    echo ERROR: cannot copy previous patch file
    exit 1
endif
#
# Edit the previous patch file.
echo "Editing the new patch file..."
cat disable-init-param-$overs.diff | sed -e "s|$overs|$nvers|g" \
	>! disable-init-param-$nvers.diff
#
# Apply the patch.
echo "Applying the patch..."
patch -p0 < disable-init-param-$nvers.diff
#
# If this fails, then exit.
if ( $status != 0 ) then
    echo ERROR: intermediate patch failed
    exit 1
endif
#
# Diff the two directories and create a new patch.
echo "Diffing the two directories..."
diff -ur linux-$nvers.orig/init/ linux-$nvers/init/ \
	>! disable-init-param-$nvers.diff
#
# Remove the modified directory.
echo "Removing the patched tree..."
rm -rf linux-$nvers/
#
# Move the "orig" directory.
echo "Moving the '.orig' tree..."
mv linux-$nvers.orig/ linux-$nvers/
#
# Test the new patch.
echo "Applying the patch..."
patch -p0 < disable-init-param-$nvers.diff
#
# If this fails, then exit.
if ( $status != 0 ) then
    echo ERROR: new patch failed
    exit 1
endif
#
# Remove the modified directory.
echo "Removing the patched tree..."
rm -rf linux-$nvers/
#EOF
echo "done."
