#!/bin/csh -f

#set echo

# Mise à jour des paquetages d'une RedHat 9.0 (roussel@cril.univ-artois.fr)

set RHVER=9

set ROOT=/cdroms
set FTP=ftp.yourownsite.yourmirror/pub/linux/distributions/redhat/linux/updates/$RHVER/en/os/
set download # download new packages by ftp if set

set NFSROOT=$ROOT/redhat$RHVER
set RHROOT=$ROOT/redhat$RHVER-bycdrom/cdrom\?
set DEFAULTROOT=$ROOT/redhat$RHVER-bycdrom/cdrom3/RedHat/RPMS # where to put new files
set UPDATESROOT=$ROOT/updates$RHVER/$FTP
set LOGFILE=$ROOT/updates$RHVER/logfile
set NEWRPMFILE=$ROOT/updates$RHVER/newRPMs
set genhdlist=$ROOT/updates$RHVER/genhdlist
# genhdlist est dans le package anaconda-runtime

set nonomatch

if (! -x $genhdlist) then
  echo Extracting genhdlist
  set anaconda=`find $RHROOT -name anaconda-runtime\*`
  set tmp=`mktemp -d tmpXXXXXX`
  cd $tmp
  rpm2cpio $anaconda | cpio --extract -d
  mv usr/lib/anaconda-runtime/genhdlist $genhdlist
  cd ..
  rm -rf $tmp
endif

if ($?download) then
  if (-e download.result) rm download.result

  wget -m ftp://$FTP |& tee download.result

  grep 'RETR.*rpm' download.result

  if ($status != 0) then
    cat <<EOF



Nothing new !
EOF
    exit 0
  endif

  echo 'New packages downloaded :'
  grep 'RETR.*rpm' download.result | awk '{print $7}'
  echo 
  echo 'Updating...'
endif

touch $LOGFILE

if (! -d $UPDATESROOT) goto makenfs

echo "Recherche de nouveaux paquetages..."

cat <<EOF >/tmp/compareversion.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int MAX=20;

int main(int argc, char *argv[])
{
  const char *delim=".-#";
  char *v1[MAX],*v2[MAX];
  int i;

  i=0;
  v1[i++]=strtok(argv[1],delim);
  while (i<MAX && (v1[i++]=strtok(NULL,delim))!=NULL);

  i=0;
  v2[i++]=strtok(argv[2],delim);
  while (i<MAX && (v2[i++]=strtok(NULL,delim))!=NULL);

  for(i=0;i<MAX && v1[i]!=NULL && v2[i]!=NULL;i++)
  {
    if (strcmp(v1[i],v2[i])!=0)
    {
      long ver1,ver2;
      char *fin1,*fin2;
      ver1=strtol(v1[i],&fin1,10);
      ver2=strtol(v2[i],&fin2,10);

      if (ver1==ver2)
	printf("%d\n",strcmp(fin1,fin2));
      else
        printf("%d\n",ver1-ver2);

      exit(0);
    }
  }

  if (i<MAX && v1[i]==NULL && v2[i]==NULL)
  {
    printf("0\n");
    exit(0);
  }

  if (i<MAX && v1[i]==NULL)
  {
    printf("-1\n");
    exit(0);
  }

  if (i<MAX && v2[i]==NULL)
  {
    printf("1\n");
    exit(0);
  }

  return 0;
}

EOF

cc -o /tmp/compareversion /tmp/compareversion.c
if ($status != 0 ) exit 1


cd $UPDATESROOT

foreach newfile (`find . -iname \*.rpm`)
  if ($newfile =~ *.src.rpm) continue
  if ($newfile =~ *.alpha.rpm) continue
  if (-e $newfile.done) continue

  echo "Examining $newfile" 

  set newname=`rpm -q -p --queryformat '%{NAME}' $newfile`
  set newsrcname=`rpm -q -p --queryformat '%{SOURCERPM}' $newfile`
  set newpackage=`rpm -q -p --queryformat '%{NAME}-%{ARCH}' $newfile`
  unset oldfile

  foreach f ($RHROOT/RedHat/RPMS/$newname*.rpm)
    set oldpackage=`rpm -q -p --queryformat '%{NAME}-%{ARCH}' $f`
    if ($newpackage == $oldpackage) then
      set oldfile=$f
      break
    endif
  end

  if ($?oldfile) then
    set ver="`rpm -q -p --queryformat '%{VERSION}' $oldfile` `rpm -q -p --queryformat '%{VERSION}' $newfile`"
    set rel="`rpm -q -p --queryformat '%{RELEASE}' $oldfile` `rpm -q -p --queryformat '%{RELEASE}' $newfile`"
    set compver=`/tmp/compareversion $ver`
    set comprel=`/tmp/compareversion $rel`
    if (($compver < 0) || ($compver == 0 && ($comprel < 0))) then
      echo "  New package $newfile replaces $oldfile"
      echo "`date`  New package $newfile replaces $oldfile" >>$LOGFILE
      grep $newsrcname $NEWRPMFILE
      if ($status != 0)  echo "$UPDATESROOT/SRPMS/$newsrcname" >>$NEWRPMFILE
      echo "$UPDATESROOT/$newfile" >>$NEWRPMFILE
      rm $oldfile
      cp $newfile $oldfile:h
    endif
  else
    echo "  New package $newfile"
    echo "`date`  New package $newfile" >>$LOGFILE
    grep $newsrcname $NEWRPMFILE
    if ($status != 0)  echo "$UPDATESROOT/SRPMS/$newsrcname" >>$NEWRPMFILE
    echo "$UPDATESROOT/$newfile" >>$NEWRPMFILE
    cp $newfile $DEFAULTROOT
  endif

  touch $newfile.done
end

rm -f /tmp/compareversion /tmp/compareversion.c

chmod ugo+r $RHROOT/RedHat/RPMS/*
$genhdlist --withnumbers $RHROOT

makenfs:

echo "Creation d'une racine pour installation NFS"

cat <<EOF >/tmp/mklink
#!/bin/csh -f
set rhroot=\$1
set nfsroot=\$2
set file=\$3
ln \$rhroot/\$file \$nfsroot/\$file
#
EOF

rm -rf $NFSROOT/*
foreach cd ($RHROOT)
  cd $cd
  foreach dir (`find . -type d -exec echo {} \;`)
    mkdir -p $NFSROOT/$dir
  end
  find . -type f -exec csh -f /tmp/mklink $cd $NFSROOT {} \;
end

rm -f /tmp/mklink

if (0) then
  foreach cd ($RHROOT)
    if (-e $cd/images/boot.img) then
      mkisofs -r -T -b images/boot.img -c images/boot.cat -o $cd.iso $cd
    else
      mkisofs -r -T -o $cd.iso $cd
    endif
  end
endif
#
