#!/bin/sh
#
# 2021/01 g.b.
# OpenAFS 1.8.8 for CentOS 8.x, build of rpms and client installation
#
# Script based on:
# https://wiki.openafs.org/devel/HowToBuildOpenAfsRpmPackages/
#
# when running the script dnf propts for "y" when required.
#
  dnf install rpm-build make perl libtool bzip2 wget epel-release elfutils-devel flex krb5-workstation
#
  mkdir -p openafs-1.8.8
  cd openafs-1.8.8
#
# Download source release.
  wget https://www.openafs.org/dl/openafs/1.8.8/openafs-1.8.8-src.tar.bz2
  wget https://www.openafs.org/dl/openafs/1.8.8/openafs-1.8.8-doc.tar.bz2
  wget https://www.openafs.org/dl/openafs/1.8.8/RELNOTES-1.8.8
  wget https://www.openafs.org/dl/openafs/1.8.8/ChangeLog
#
  getstring=$(tar -xOf openafs-1.8.8-src.tar.bz2 '*/RedHat/openafs.spec.in' | grep 'https://www.central.org')
  celldbname_in_spec="${getstring##*/}"
#
# added for a newer version of CellServDB: the name is modofied to the one in the spec file
# if the file is available it is not retrieved from www.central.org
  wget http://grand.central.org/dl/cellservdb/CellServDB
  mv CellServDB $celldbname_in_spec
#
# Build the source RPM.
tar xf openafs-1.8.8-src.tar.bz2 --strip-components=4 '*/makesrpm.pl'
perl makesrpm.pl openafs-1.8.8-src.tar.bz2 openafs-1.8.8-doc.tar.bz2 RELNOTES-1.8.8 ChangeLog $celldbname_in_spec

# Install build dependencies.
  dnf builddep openafs-1.8.8-1.src.rpm
  dnf install "kernel-devel-uname-r == $(uname -r)"
#
# Build RPMs.
  rpmbuild --rebuild -bc openafs-1.8.8-1.src.rpm
#
# Client packages installation
  list0=$(ls /root/rpmbuild/RPMS/x86_64/| egrep "^openafs-1|openafs-krb5|client|dkms|docs"| grep 1.8.8| grep -v debug)
  mkdir -p essential_rpms
  essential_client_rpms_list=""
  for p in $list0
  do
    rpm=/root/rpmbuild/RPMS/x86_64/$p
    cp -p $rpm essential_rpms
    essential_client_rpms_list="$essential_client_rpms_list $p"
  done
#
  cd essential_rpms
  dnf install $essential_client_rpms_list
  cd ..
#
  echo 
  echo "*** After the client installation:"
  echo " to unset permanently the client data encryption, which reduces drastically the performance"
  echo " Add the line:"
  echo 'ExecStartPost=/bin/bash -c "sleep 2;fs setcryp -crypt off > /dev/null 2>/dev/null"'
  echo " between ExecStart and ExecStop lines" 
  echo " in the file"
  echo "/usr/lib/systemd/system/openafs-client.service"
  echo " and give the command"
  echo " systemctl daemon-reload"
  echo
#
