#!/bin/sh
#
# install-lsof <revision> -- install lsof <revision>

DD=/var/ftpd/pub/tools/unix/lsof
# DEBUG DD=/tmp/lsof	# DEBUG
SD=$HOME/src/lsof4/support

# Check for version specification.

if test $# -ne 1
then
  echo "Usage: install-lsof <version>"
  exit 1
fi
R=$1
echo $R | grep '^4\.[0-9]*$' > /dev/null 2>&1
if test $? -ne 0
then
  echo "<revision> must be 4.nn"
  exit 1
fi
SR=`echo $R | sed 's/^4\.\([0-9]*\)$/\1/'`
if test $SR -lt 73
then
  echo "Sub-revision must be 73 or greater."
  exit 1
fi
PR=4.`expr $SR - 1`
PPR=4.`expr $SR - 2`

# Check for the presence of files for the new revision.

err=0
if test ! -f ${SD}/lsof_${R}.man
then
  echo "Creating lsof_${R}.man"
  (cd $SD; ./makeman) > /dev/null
fi
for i in CHECKSUMS_$R ../00FAQ ../00DIST lsof_${R}.man lsof.00INDEX \
	 lsof.README \
	 lsof_${R}.tar.bz2 lsof_${R}.tar.bz2.sig \
	 lsof_${R}.tar.gz lsof_${R}.tar.gz.sig \
	 lsof_${R}.tar.Z  lsof_${R}.tar.Z.sig 
do
  if test ! -f ${SD}/$i
  then
    echo "${SD}/$i not found"
    err=1
  fi
done

# Check for the presence of files for the previous revision.

for i in lsof_${PR}.man \
	 lsof_${PR}.tar.bz2 \
	 lsof_${PR}.tar.bz2.sig \
	 lsof_${PR}.tar.gz \
	 lsof_${PR}.tar.gz.sig \
	 lsof_${PR}.tar.Z \
	 lsof_${PR}.tar.Z.sig 
do
  if test ! -f ${DD}/$i
  then
    echo "${DD}/$i not found"
    err=1
  fi
done

# Quit if there are missing files.

if test $err -ne 0
then
  echo "Quitting because of missing files"
  exit 1
fi

# Archive previous revision files.

for i in CHECKSUMS_$PR lsof_${PR}.man \
	 lsof_${PR}.tar.bz2 lsof_${PR}.tar.bz2.sig \
	 lsof_${PR}.tar.gz  lsof_${PR}.tar.gz.sig  \
	 lsof_${PR}.tar.Z   lsof_${PR}.tar.Z.sig 
do
  if test -f ${DD}/$i
  then
    mv ${DD}/$i ${DD}/OLD
  else
    echo "No ${DD}/$i to archive"
  fi
done

# Remove some previously archived files.  Save the gz archive and signature.

for i in CHECKSUMS_$PPR lsof_${PPR}.man \
	 lsof_${PPR}.tar.bz2  lsof_${PPR}.tar.bz2.sig  \
	 lsof_${PPR}.tar.Z   lsof_${PPR}.tar.Z.sig 
do
  if test -f ${DD}/OLD/$i
  then
    echo "Removing ${DD}/OLD/$i"
    rm -f ${DD}/OLD/$i
  fi
done

# Install new files.

for i in lsof.00INDEX lsof.README 
do
  echo "Installing $i"
  rm -f $DD/$i
  cp ${SD}/$i $DD
  chmod 444 $DD/$i
done
rm -f ${DD}/00INDEX; mv ${DD}/lsof.00INDEX ${DD}/00INDEX
rm -f ${DD}/README; mv ${DD}/lsof.README ${DD}/README
for i in CHECKSUMS_$R lsof_${R}.man \
	 lsof_${R}.tar.bz2 lsof_${R}.tar.bz2.sig \
	 lsof_${R}.tar.gz lsof_${R}.tar.gz.sig \
	 lsof_${R}.tar.Z  lsof_${R}.tar.Z.sig 
do
  echo "Installing $i"
  rm -f $DD/$i
  mv ${SD}/$i $DD
  chmod 444 $DD/$i
done
(cd $DD; rm -f CHECKSUMS; ln -s CHECKSUMS_$R CHECKSUMS; ls -lL CHECKSUMS)
(cd $DD; rm -f lsof_man; ln -s lsof_${R}.man lsof_man; ls -lL lsof_man)
echo "Installing FAQ"
rm -f ${DD}/FAQ; cp ${SD}/../00FAQ ${DD}/FAQ; chmod 644 ${DD}/FAQ
rm -f ${DD}/ChangeLog; cp ${SD}/../00DIST ${DD}/ChangeLog; chmod 644 ${DD}/ChangeLog
for i in bz2 gz Z
do
  rm -f ${DD}/lsof.tar.$i ${DD}/lsof.tar.${i}.sig
  (cd $DD; ln -s lsof_${R}.tar.$i lsof.tar.$i)
  (cd $DD; ln -s lsof_${R}.tar.${i}.sig lsof.tar.${i}.sig)
  (cd $DD; ls -lL lsof.tar.$i lsof.tar.${i}.sig)
done

# Install binaries. (Disabled April 15, 2008.)

#(cd $SD; rdist -f distfile.binaries)
exit 0
