#!/bin/bash

if [ $# -eq 0 ]
  then
    echo "No arguments supplied"
fi
if [ $# -lt 3 ]
  then
    echo "Usage:
importNew type version pkgname [rpmpath]
examples:
# importNew contribs 10 smeserver-mycontrib [~/smeserver-mycontribs-1.0-1.src.rpm]
# importNew sme 10 smeserver-package [~/smeserver-package-1.0-1.src.rpm] 
This script is intended to create a new tree, if needed,and a new branch to import a provided srpm.
It will then run cvs-import.sh script for you if you provided a srpm location. If not provided you can populate the branch 
with what you want. If you only intend to copy the content of a branch to another, you might search for newbranch script.

"
	exit
fi

sme=$1
if [[ "$sme" != "sme" && "$sme" != "contribs" ]]
then 
  echo "wrong first parameter should be either 'sme' or 'contribs'"
  exit
fi
re='^[0-9]+$'
if ! [[ $2 =~ $re ]] ; then
   echo "Error: Second argument should be a version number (e.g.: 10) for the branch ((e.g.: contribs10) where you want to put the SRPM content if provided." >&2; exit 1
fi
ver=$2
pkgname=$3
rpmpath=$4
curpwd=`pwd`
packageroot='smecontribs'
CVSROOT=":ext:shell.koozali.org:/cvs/smecontribs"
if [[ "$sme" == "sme" ]]
then
	packageroot='smeserver'
	CVSROOT=":ext:shell.koozali.org:/cvs/smeserver"
fi


#this one is directly from commons/Makefile.common
CreateBranchMakefile() {
    cat >Makefile <<EOF
# Makefile for source rpm: $NAME
# \$Id\$
NAME := $NAME
SPECFILE = \$(firstword \$(wildcard *.spec))

define find-makefile-common
for d in common ../common ../../common ; do if [ -f \$\$d/Makefile.common ] ; then if [ -f \$\$d/CVS/Root -a -w \$\$/Makefile.common ] ; then cd \$\$d ; cvs -Q update ; fi ; echo "\$\$d/Makefile.common" ; break ; fi ; done
endef

MAKEFILE_COMMON := \$(shell \$(find-makefile-common))

ifeq (\$(MAKEFILE_COMMON),)
# attept a checkout
define checkout-makefile-common
test -f CVS/Root && { cvs -Q -d \$\$(cat CVS/Root) checkout common && echo "common/Makefile.common" ; } || { echo "ERROR: I can't figure out how to checkout the 'common' module." ; exit -1 ; } >&2
endef

MAKEFILE_COMMON := \$(shell \$(checkout-makefile-common))
endif

include \$(MAKEFILE_COMMON)
EOF
}



# update modules files
cd ~/$packageroot/CVSROOT
cvs -Q update -dPA 1>/dev/null
if [ -d  ~/$packageroot/rpms ]
	then
	# update current tree
	cd ~/$packageroot/rpms
	cvs -Q update -dPA  $pkgname common 1>/dev/null
else
	# checkout rpms
	cd ~/$packageroot
	cvs -Q -z3 -d:ext:shell.koozali.org:/cvs/$packageroot co -P rpms 1>/dev/null
fi


newmodule=true;
# better test would be to actually grep module file!
#if [ -d ~/$packageroot/rpms/$pkgname ]
cd ~/$packageroot/CVSROOT
cvs -Q update -dPA 1>/dev/null
grep -Eq "^$pkgname\s+rpms/$pkgnames.*" ~/$packageroot/CVSROOT/modules
if (( $? == 0 ))
then
	echo "package exists already"
	newmodule=false; 
fi
change=0
cd ~/$packageroot/rpms
#if [ -d ~/$packageroot/rpms/$pkgname ]
if ( cvs status -l $pkgname 2>/dev/null|grep -q 'Unknown' )
then
  echo "creating $pkgname tree ..."
  mkdir -p $pkgname
  cvs add $pkgname
  change=1
fi
#if [ -d ~/$packageroot/rpms/$pkgname/$sme$ver ]
if ( cvs status -l $pkgname/$sme$ver 2>/dev/null|grep -q 'Unknown' )
then
  echo "creating $pkgname/$sme$ver branch with content..."
  mkdir -p $pkgname/$sme$ver
  cvs add $pkgname/$sme$ver
  change=1
fi
#if [ -f ~/$packageroot/rpms/$pkgname/$sme$ver/.cvsignore ]
if ( cvs status $pkgname/$sme$ver/.cvsignore 2>/dev/null|grep -q 'Unknown' )
then
  touch $pkgname/$sme$ver/.cvsignore
  cvs add $pkgname/$sme$ver/.cvsignore
  change=1
fi
#if [ -f ~/$packageroot/rpms/$pkgname/$sme$ver/import.log ]
if ( cvs status  $pkgname/$sme$ver/import.log 2>/dev/null|grep -q 'Unknown' )
then
  touch $pkgname/$sme$ver/import.log
  cvs add $pkgname/$sme$ver/import.log
  change=1
fi
#create Makefile here
pushd $pkgname/$sme$ver >/dev/null
#if [ ! -f Makefile ] 
if ( cvs status  Makefile 2>/dev/null|grep -q 'Unknown' )
then
    NAME=$pkgname
    CreateBranchMakefile
    cvs -Q add Makefile
   change=1
fi
popd >/dev/null

if [ "$change" == "1" ]
then
echo "commit..."
cvs -Q commit -m "Prep for $pkgname import" $pkgname
fi

if ( $newmodule )
then 
	cd ~/$packageroot/CVSROOT
	cvs -Q update -dPA 1>/dev/null
	echo "$pkgname     rpms/$pkgname &common" >> modules
	linenumb=`cat modules |grep -n "#Start Auto-Maintenance" | grep -Eo '^[^:]+'`
	# "#Start Auto-Maintenance"
	#(head -n $linenumb; sort -nk$linenumb) < sample.txt 1<> sample.txt
	(head -n $linenumb; sort) < modules 1<> modules
	#echo "cvs commit -m \"adding $pkgname to modules\""
	cvs commit -m "adding $pkgname to modules"
fi

if [[ "$HOSTNAME" == "shell.koozali.org" ]]
then
  if [ $# -eq 3 ]
        then
	echo "importing the srpm for you"
	cd ~/$packageroot;
	cd CVSROOT/; 
	cvs -Q update -dPA 1>/dev/null; 
	cd ../rpms/; 
	cvs -Q update -dPA common $pkgname 1>/dev/null ;
	cd ~/$packageroot; 
	./common/cvs-import.sh -b $sme$ver -m 'Initial import' /tmp/$(basename $rpmpath)
  else
	echo "no srpm provided"
  fi

else
  echo "##########################"
  if [[ $rpmpath != "" ]]
        then
	echo "sending $rpmpath to  shell.koozali.org ..."
	echo "scp $rpmpath shell.koozali.org:/tmp/"
	cd $curpwd
	scp $rpmpath shell.koozali.org:~
	echo "now trying to push this on shell.koozali.org and run ./common/cvs-import.sh"
	echo "this could fails if your srpm was not initially on ~/smecontrib/ and if you have not ForwardAgent yes and user set in your .ss/config file for shell.koozali.org"
#	ssh shell.koozali.org "cd ~/$packageroot;cd CVSROOT/; echo 'updating CVSROOT/modules...'; cvs -Q update -dPA modules; cd ../rpms/;echo 'checkout $pkgname ...' cvs  co $pkgname 1>/dev/null ;cd ~/$packageroot; if [[ -f ~/$(basename $rpmpath) ]]; then ./common/cvs-import.sh -b $sme$ver -m 'Initial import' ~/$(basename $rpmpath); fi "
	ssh shell.koozali.org "cd ~/$packageroot;cd CVSROOT/; echo 'updating CVSROOT/modules...'; cvs -Q update -dPA modules && cd ../rpms/ && echo 'checkout $pkgname ...' && cvs  co $pkgname 1>/dev/null && cd ~/$packageroot && if [[ -f ~/$(basename $rpmpath) ]]; then  echo './common/cvs-import.sh -b $sme$ver -m  Initial import ~/$(basename $rpmpath)'; ./common/cvs-import.sh -b $sme$ver -m 'Initial import' ~/$(basename $rpmpath); fi "
    
  echo "in case of failure do:"
  echo "scp $rpmpath shell.koozali.org:/tmp/"
  echo "ssh shell.koozali.org"
  echo "cd ~/$packageroot"
  if [ $# -eq 3 ]
      	then
	echo "./common/cvs-import.sh -b $sme$ver -m 'Initial import' /tmp/$(basename $rpmpath)"
  else
        echo "./common/cvs-import.sh -b $sme$ver -m 'Initial import' YOURSRPM "
  fi        
  echo "exit"
  fi
  cd ~/$packageroot/rpms/$pkgname/$sme$ver
  if [ -f $pkgname}.spec ] 
    then
    ### if there is no spec file this will fail  : make prep 
    cvs update -dPA; make prep
  else
    echo "# now do:"
    echo "cd ~/$packageroot/rpms/$pkgname/$sme$ver ; cvs update -dPA;"
    echo "then create your own spec file and cvs add it."
    echo "then create your own archive and import it to lookaside cache if needed" 
  fi
fi

echo "##########################"
echo "now you can:"
echo "cd ~/$packageroot/rpms/$pkgname/$sme$ver"

unset CVSROOT
