#!/bin/bash

if [ $# -eq 0 ]
  then
    echo "No arguments supplied"
    echo "Usage:
refresh [sme|contribs] 
examples:
# refresh sme 
This script will refresh or create the tree for smeserver (sme) or smecontribs (contribs).

"
        exit
fi
sme=$1
if [[ "$sme" != "sme" && "$sme" != "contribs" ]]
then
  echo "Wrong first parameter should be either 'sme' or 'contribs'"
  exit
fi

packageroot='smecontribs'
if [[ "$sme" == "sme" ]]
then
        packageroot='smeserver'
fi

#create if missing
if [ ! -d  ~/$packageroot ]
        then
	"creating ~/$packageroot and populating..."
	mkdir -p ~/$packageroot
	cd ~/$packageroot
	cvs -Q -z3 -d:ext:shell.koozali.org:/cvs/$packageroot co -P CVSROOT rpms common 1>/dev/null
	exit
fi

if [[ "$2" == "force" ]]
	then
	"Forcing checkout for ~/$packageroot and populating with CVSROOT rpms common..."
        mkdir -p ~/$packageroot
        cvs -Q -z3 -d:ext:shell.koozali.org:/cvs/$packageroot co -P CVSROOT rpms common 1>/dev/null
        exit
fi

# check if module rpms exist and populate it if necessary or update it
if [ -d  ~/$packageroot/rpms ]
        then
        # update current tree
        cd ~/$packageroot/rpms
        cvs -Q update -dPA 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

