#!/bin/sh

# Copyright (C) 2008  Jochen Schmitt <Jochen@herr-schmitt.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# ksplice fetches the source rpm of the currently installed kernel
# from the repository and try to prepare it for ksplice.

TEXTDOMAIN=fedora-ksplice

rpmcache="$HOME/.fedora-ksplice-cache"

usage() {
    echo $"Usage:" 
    echo -e "\tfedora-ksplice-prepare [--clean|--enablerepo <repository>] [kernelversion]" 
    echo 
    echo $"Options:" 
    echo -e '\t--clean'
    echo -e -n '\t\t'
    echo  $"Clean downloaded source rpm from cache"
    echo -e '\t--enablerepo <repository>' 
    echo -e -n '\t\t'
    echo  $"Enable repository <repository>"
    echo -e '\tkernelversion'
    echo -e -n '\t\t'
    echo  $"Kernelversion"
}

cleancache=0
if [ "$1" = "--clean" ]; then
   cleancache=1
   shift 1
fi

if [ "$1" = "--enablerepo" ]; then
    if [ $# -lt 3 ]; then
	echo $"Error: Expected argument missing" >&2
	echo >&2
	usage
	exit 1
    fi
    cmdline="--enablerepo $2"
    shift 2
fi

if [ "$1" = "-h" -o "$1" = "--help" ]; then
    usage
    exit 0
fi

if [ $# -gt 1 ]; then
    usage
    exit 1
fi


arch=$(uname -m)

if [ $# -eq 1 ]; then
    vers=$1
else
    fullvers=$(uname -r)
    if [ $fullvers != ${fullvers%%.debug} ]; then
	debug='.debug'
    fi
    vers=${fullvers%%.debug}
    vers=${vers%%.$arch}
fi

extravers="$(echo $vers | sed -e 's/^[0-9]*\.[0-9]*\.[0-9]*\(.*\)$/\1/').$(arch)$debug"

kernvers=${vers%-*}
kernvers=${kernvers%.*}

if [ ! -d $rpmcache ]; then
  mkdir -p $rpmcache
  if [ ! -d $rpmcache ]; then
    echo $"Could not create rpm cache" >&2
    exit 1
  fi
fi

cd $rpmcache

if [ $cleancache -eq 1 ]; then
  echo $"Clean RPM Cache"
  rm -rf *
  exit 0
fi

sourcerpm="kernel-${vers}.src.rpm"

if [ -r $sourcerpm ]; then
    echo $"Kernel source rpm exist in rpm cache"
else
    echo $"Downloading kernel source rpm"
    yumdownloader $cmdline --source kernel-$vers
    if [ $? -ne 0 ]; then
	echo $"Error: Could not download kernel source rpm" >&2
	exit 1
    fi
fi

echo $"Kernel rpm will be installed"
rpm -ivh --define "_sourcedir $rpmcache" \
         --define "_specdir $rpmcache" \
         --define "_builddir $rpmcache" \
         kernel-${vers}.src.rpm
if [ $? -ne 0 ]; then
    echo $"Could not install kernel source rpm" >&2
    exit 1
fi

echo $"Kernel rpm will be prepared"

rpmbuild --define "_sourcedir $rpmcache" \
         --define "_specdir $rpmcache" \
         --define "_builddir $rpmcache" \
         --with baseonly -bp --target=$(uname -m) kernel.spec
if [ $? -ne 0 ]; then
    echo $"Error: Could not prepare kernel source rpm" >&2
    exit 1
fi

cd ${rpmcache}/kernel-${kernvers}/linux-${kernvers}.${arch}

sed -i -e "s/^EXTRAVERS.*$/EXTRAVERS = ${extravers}/" Makefile

if [ ! -d ksplice ]; then
    mkdir ksplice
fi

cp /boot/config-${fullvers} ksplice/.config
if [ $? -ne 0 ]; then
    echo $"Error: Could not copy kernel config file" >&2
    exit 1
fi

cp /boot/System.map-${fullvers} ksplice/System.map
if [ $? -ne 0 ]; then
    echo $"Error: Could not copy kernel symbol map" >&2
    exit 1
fi

ln -sf /lib/modules/${fullvers}/build ksplice/build
if [ $? -ne 0 ]; then
    echo $"Error: Could not create symbolic link to module build directory" >&2
    exit 1
fi
