#!/bin/sh

# vers=2.6.17.1-plamoUP
arch=noarch
build=P1
pkgbase=kernelsrc

root_chk=`id | grep 'uid=0' | cut -d" " -f1`
echo $root_chk

if [ $root_chk".x" == ".x" ] ; then
  SUDO='/usr/bin/sudo'
else
  SUDO=''
fi

if [ -f Makefile ]; then
  major=`grep "^VERSION" Makefile | cut -d" " -f3`
  minor=`grep "^PATCH" Makefile | cut -d" " -f3`
  rel=`grep "^SUBLEVEL" Makefile | cut -d" " -f3`
  patch=`grep "^EXTRAVERSION" Makefile | cut -d" " -f3`
fi

if [ -f .config ]; then
  local=`grep "^CONFIG_LOCALVERSION=" .config | cut -d"=" -f2 | sed 's/\"//g' | sed 's/-/_/'`
fi

vers=${major}"."${minor}"."${rel}${patch}
fullvers=${major}"."${minor}"."${rel}${patch}${local}

echo $fullvers

pkgsrc=${pkgbase}"-"${fullvers}"-"${arch}"-"${build}".tgz"
pkginc="linuxinc-"${fullvers}"-i386-"${build}".tgz"


H=`pwd`
T=`pwd`/../pkg

if [ -d $T ]; then
  $SUDO rm -rf $T
fi

mkdir -p $T/usr/src
( cd .. ; tar cvf - linux-$vers ) | ( cd $T/usr/src ; tar xf -)

( cd $T/usr/src/ ; ln -sf linux-$vers linux )
pushd $T
  $SUDO chown -R root.root *
  $SUDO /sbin/makepkg ../$pkgsrc
popd
