#! /bin/sh
#
# This is a script to generate patches for new kernels
# by using an old patch. If the patch cannot be applied
# correctly, changes need to be made manually. Once that
# has been done, this script can be rerun to complete
# the patches.
#

site_base="ftp://ftp.fi.kernel.org/pub/linux/kernel"
wget_opts="--dot-style=binary"

if [ "$#" == 0 ]; then
  echo "Usage: make-patch X.Y.Z"
  exit 1
fi

# Parse kernel version number from command line
version="$1"
major=${version:0:3}
minor=${version:4}
if [ -z "$major" -o -z "$minor" ]; then
  echo "invalid version number"
  exit 1
fi

newpatch="statistics-$version-full.patch"
if [ -e "$newpatch" ]; then
  echo "a patch for that kernel version already exists"
  exit 1
fi
newpatchomatic="$version-patch-o-matic"
if [ -d "$newpatchomatic" ]; then
  echo "a patch-o-matic directory for that kernel version already exists"
  exit 1
fi

# Find latest patch
patch=`ls statistics-*-full.patch | sort -r | head -1`
if [ -z "$patch" -o ! -e "$patch" ]; then
  echo "could not find a patch file"
  exit 1
fi
patchomatic=`ls -d *-patch-o-matic | sort -r | head -1`
if [ -z "$patchomatic" -o ! -d "$patchomatic" ]; then
  echo "could not find patch-o-matic directory"
  exit 1
fi

# Look for kernel sources
if [ -e "linux-$version.tar.bz2" ]; then
  kernel_src="linux-$version.tar.bz2"
elif [ -e "linux-$version.tar.gz" ]; then
  kernel_src="linux-$version.tar.gz"
elif [ -e "linux-$version.tar" ]; then
  kernel_src="linux-$version.tar"
else
  echo "Attempting to download kernel..."
  if ! wget $wget_opts "$site_base/v$major/linux-$version.tar.bz2"; then
    if ! wget $wget_opts "$site_base/v$major/linux-$version.tar.gz"; then
      echo "could not download kernel"
      exit 1
    else
      kernel_src="linux-$version.tar.gz"
    fi
  else
    kernel_src="linux-$version.tar.bz2"
  fi
fi

if [ ! -e "tmp-make-patch/linux" ]; then
  echo "Unpacking kernel..."
  mkdir -p tmp-make-patch
  cd tmp-make-patch
  case "$kernel_src" in
  *.tar.bz2)
    tar xfj ../$kernel_src
    ;;
  *.tar.gz)
    tar xfz ../$kernel_src
    ;;
  *.tar)
    tar xf ../$kernel_src
    ;;
  esac

  echo "Attempting to patch kernel.."
  kernel_dir=linux
  if [ ! -d $kernel_dir ]; then
    kernel_dir=linux-$version
    if [ ! -d $kernel_dir ]; then
      echo "cannot find the kernel directory in the unpacked sources"
      exit 1
    fi
  fi
  if ! patch -d $kernel_dir -b -p1 <../$patch; then
    echo "patching failed - modify sources manually and try again"
    exit 1
  fi
else
  cd tmp-make-patch
fi

echo "Analyzing kernel changes.."
touch ../$newpatch
mkdir ../$newpatchomatic
cp ../$patchomatic/statistics.patch.help ../$newpatchomatic/
# FIXME: assumes this file is always the same
cp ../$patchomatic/statistics.patch.config.in ../$newpatchomatic/
# FIXME: assumes this file is always the same
cp ../$patchomatic/statistics.patch.configure.help ../$newpatchomatic/

for file in `find -name \*.orig`; do
  newfile=${file%*.orig}

  if cmp -s $newfile $file; then
    echo "no changes made to $newfile - aborting"
    exit 1
  fi
  echo " $newfile"

  echo "diff -u $file $newfile" >>../$newpatch
  diff -u $file $newfile >>../$newpatch

  case "$newfile" in
  linux/Documentation/Configure.help)
    ;; # Ignore this files
  linux/net/ipv4/netfilter/Config.in)
    ;; # Ignore this files
  *)
    echo "diff -u $file $newfile" >>../$newpatchomatic/statistics.patch
    diff -u $file $newfile >>../$newpatchomatic/statistics.patch
    ;;
  esac
done

echo "Removing kernel sources.."
cd ..
rm -rf tmp-make-patch
