######################################################################
#
# Daemon Sub package 
# 
%package -n root-daemon
Group: ROOT
Summary: @short@
%description -n root-daemon
@long@

#---------------------------------------------------------------------
# Files that go into this package 
%files -n root-daemon -f @files@

#---------------------------------------------------------------------
# Pre installation script - only body
%pre -n root-daemon
#
# Now we have to ensure the following state:
#   /etc/passwd: rootd:x:Anonymous rootd:/var/spool/rootd:/bin/false
#   /etc/group:  rootd:x:72:rootd
#
if ! grep -q ^rootd: /etc/group; then 
  groupadd rootd 
fi
if ! grep -q ^rootd: /etc/passwd; then 
  useradd -c "Anonymous rootd" -d "/var/spool/rootd" -s /bin/false \
   -g rootd rootd 
fi

#
# Make home directory 
#
if [ ! -d /var/spool/rootd ] ; then 
  mkdir -p /var/spool/rootd
fi

#
# If user already exist, modify it. 
#
usermod -c "Anonymous rootd"  rootd
usermod -d /var/spool/rootd   rootd
usermod -g rootd              rootd
usermod -s /bin/false         rootd


# Since the home directory was created before putting the user
# into the rootd group and moreover we cannot guarantee that
# the permissions were correctly *before* calling this script,
# we fix them now. 
#echo 
#echo "SECURITY: Fixing permission of /var/spool/rootd !"
#echo "(I.e. replacing GIDs other than root and rootd with rootd.)"
#echo
chown rootd.rootd /var/spool/rootd
find /var/spool/rootd \
    -not \( -group root -or -group rootd \) \
    -exec chgrp rootd {} \;

# Create the tmp and pub directories in /var/spool/rootd and
# make them world read- and writeable. 
if [ ! -d /var/spool/rootd/tmp ] ; then 
  mkdir -p /var/spool/rootd/tmp
  chmod 777 /var/spool/rootd/tmp
fi
if [ ! -d /var/spool/rootd/pub ] ; then 
    mkdir -p /var/spool/rootd/pub
    chmod 777 /var/spool/rootd/pub
fi

# end of script
#---------------------------------------------------------------------
# Post installation script - only body
%post -n root-daemon
#
# Add INETD lines to configuration file, if not already present 
#
if ! grep -qs ^rootd /etc/inetd.conf ; then 
  echo 'rootd stream tcp nowait root %{_prefix}/bin/rootd rootd -i' \
    >> /etc/inetd.conf
fi
if ! grep -qs ^proofd /etc/inetd.conf ; then 
  echo 'proofd stream tcp nowait root %{_prefix}/bin/proofd proofd %{_prefix}/share/root' \
    >> /etc/inetd.conf
fi
# Install lines in /etc/services 
if ! grep -qs ^proofd\$ /etc/services ; then
    echo "proofd          1093/tcp # ROOT"   >> /etc/services
fi
if ! grep -qs ^rootd\$ /etc/services ; then
    echo "rootd           1094/tcp # ROOT"  >> /etc/services
fi

# end of script
#---------------------------------------------------------------------
# Pre removal - only body
%preun -n root-daemon
#
# Remove entries from /etc/inetd.conf
#
if ! grep -qs ^rootd /etc/inetd.conf ; then 
  sed '/^rootd.*/d' < /etc/inetd.conf > /etc/inetd.conf.rpmnew
  mv /etc/inetd.conf.rpmnew /etc/inetd.conf
fi
if ! grep -qs ^proofd /etc/inetd.conf ; then 
  sed '/^proofd.*/d' < /etc/inetd.conf > /etc/inetd.conf.rpmnew
  mv /etc/inetd.conf.rpmnew /etc/inetd.conf
fi
#
# Do not remove antries from /etc/services, since the numbers _are_ 
# assigned IANA, so there's no need 
#
# end of script
#---------------------------------------------------------------------
# Post removal - only body
%postun -n root-daemon
# rm -rf /var/spool/rootd
#
# Remove the user rootd and the group if possible 
#
userdel -r rootd
reason=$?
if [ $reason -ne 0 ] ; then 
    # Could not remove user. Ignore. 
    echo "Couldn't remove user rootd, check it. Reason"
    case $reason in 
    1)  echo "can't update password file"    ;;
    2)  echo "bad command syntax"            ;;
    6)  echo "specified user doesn't exist"  ;;
    8)  echo "user currently logged in"      ;;
    10) echo "can't update group file"       ;; 
    12) echo "can't remove home directory"   ;;
    *)  echo "Unknown"                       ;;
    esac
fi
# end of script        

