#!/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 fetch the sourc rpm of the currently installed kernel
# from the repository an try to prepare it for ksplice.

TEXTDOMAIN=fedora-ksplice

usage() {
    echo $"Usage:"
    echo -e "\tfedora-ksplice-create [OPTIONS] <kernelversion> <patch>" 
    echo 
    echo $"Options:"
    echo -e '\t-v, --verbose'
    echo -e -n '\t\t'
    echo $"Verbose output"
    echo -e '\t-c [CONFIGFILE] --config [CONFIGFILE]'
    echo -e -n '\t\t'
    echo $"Define configure file for ksplice"
    echo -e '\t--id [BUILDID]'
    echo $"Define build id for ksplice"
    echo -e -n '\t\t'
    echo -e '\t--patch-opt [OPTIONS]'
    echo -e -n '\t\t'
    echo $"Define options for the patch(1) command"
    echo
    echo -e '\tkernelversion'
    echo -e -n '\t\t'
    echo $"kernelversion"
    echo 
    echo -e '\tpatch'
    echo -e -n '\t\t'
    echo $"kernel patch"
}

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


if [ "$1" = "-v" -o "$1" = "--verbose" ]; then
    cmdline="-v"
    shift
fi

if [ "$1" = "-c" -o "$1" = "--config" ]; then
    if [ $# -lt 2 ]; then
	echo $"Error: Name of config file expected" >&2
	exit 1
    fi
    cmdline="$cmdline --config=$2"
    shift 2
fi

if [ "$1" = "--id" ]; then
   if [ $# -lt 2 ]; then
       echo $"Error: Build id expected as argument" >&2
       exit 1
   fi
   cmdline="$cmdline --id=$2"
   shift 2
fi

    
if [ "$1" = "--patch-opt" ]; then
    if [ $# -lt 2 ]; then
	echo $"Error: No patch options was defined" >&2
	exit 1
    fi
    cmdline="$cmdline --patch-opt=$2"
    shift 2
fi

if [ $# -eq 0 -o $# -gt 2 ]; then
    usage
    exit 1
fi

arch=$(uname -m)
fullvers=$(uname -r)

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

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

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

rpmcache=~/.fedora-ksplice-cache

sourcedir=${rpmcache}/kernel-${kernvers}/linux-${kernvers}.${arch}

if [ ! -d "$sourcedir" ]; then
    echo $"Error: Kernel source directory doesn't exist" >&2
    exit 1
fi

if [ ! -d "$sourcedir/ksplice" ]; then
    echo $"Error: Kernel source was not prepared for ksplice" >&2
    exit 1
fi

if [ ! -r "$sourcedir/ksplice/System.map" ]; then
    echo $"Error: Symbol map for ksplice not exist" >&2
    exit 1
fi

if [ ! -s "$sourcedir/ksplice/build" ]; then
    echo $"Error: Symbol link to module build directory not exist" >&2
    exit 1
fi

if [ ! -r "$sourcedir/ksplice/.config" ]; then
    echo $"Error: Kernel configuration file not exist in ksplice directory" >&2
    exit 1
fi

ksplice-create $cmdline --patch $1 "$sourcedir"
