#!/bin/sh
#
# Author   Carsten Grohmann <carsten@securityenhancedlinux.de>
#
# State    11. June 2003
#
# Licence  GPL
#
VERSION="0.2"
#
# return values
#  1 syntax errors
#  2 nonexistent filesystem entry
#  3 unknown type

# The CVS base directory
CVSDIR="/home/cvs/"

# An example configuration to set the permissions in the CVS directory
# 
# Every entry has follow syntax:
#   file|directory:owner.group:access_permissions:[recursive]:[file|directory]
#
# You can replace "owner.group" and or "access_permission" with "not_set" to 
# ignore some parts.
#
# Hint: It is NOT possible to use wild cards!
#
DIRS="
$CVSDIR/tmp:root.cvsuser:3770 \
$CVSDIR/bin:root.cvsuser:750:recursive \
$CVSDIR/project_p1/:not_set:2770:recursive:directory \
$CVSDIR/project_p1/:not_set:440:recursive:file \
$CVSDIR/project_p1/:root.cvsp1user:750 \
$CVSDIR/project_p1/cvslock:root.cvsp1user:2770 \
$CVSDIR/project_p1/repository:root.cvsp1user:2770 \
$CVSDIR/project_p1/repository/Attic:.cvsp1user:not_set:recursive \
$CVSDIR/project_p1/repository/CVSROOT:root.cvsp1admin:not_set:recursive \
$CVSDIR/project_p1/repository/CVSROOT:not_set:2775 \
$CVSDIR/project_p1/repository/CVSROOT:not_set:664:recursive:file \
$CVSDIR/project_p1/repository/CVSROOT/history:root.cvsp1user:660 \
$CVSDIR/project_p1/repository/CVSROOT/val-tags:root.cvsp1user:660 \
$CVSDIR/project_p1/repository/CVSROOT/.#checkoutlist:not_set:440 \
$CVSDIR/project_p1/repository/CVSROOT/.#commitinfo:not_set:440 \
$CVSDIR/project_p1/repository/CVSROOT/.#config:not_set:440 \
$CVSDIR/project_p1/repository/CVSROOT/.#cvswrappers:not_set:440 \
$CVSDIR/project_p1/repository/CVSROOT/.#editinfo:not_set:440 \
$CVSDIR/project_p1/repository/CVSROOT/.#loginfo:not_set:440 \
$CVSDIR/project_p1/repository/CVSROOT/.#modules:not_set:440 \
$CVSDIR/project_p1/repository/CVSROOT/.#notify:not_set:440 \
$CVSDIR/project_p1/repository/CVSROOT/.#rcsinfo:not_set:440 \
$CVSDIR/project_p1/repository/CVSROOT/.#taginfo:not_set:440 \
$CVSDIR/project_p1/repository/CVSROOT/.#verifymsg:not_set:440 \
$CVSDIR/project_p1/repository/modul1:.cvsp1modul1:not_set:recursive \
$CVSDIR/project_p1/repository/modul2:.cvsp1modul2:not_set:recursive \
"

# This directories will be cleared
RMDIRS="
$CVSDIR/project_p1/cvslock \
$CVSDIR/tmp
"

echo "$setcvsrights version $VERSION"
echo "(c) 2003 Carsten Grohmann"

# set the permissions
#####################

# $1 contains - file or directory
# $2 contains - user.group
# $3 contains - access permissions
# $4 contains - recursive flag
# $5 contains - file or directory flag


for i in $DIRS; do
  # split $i into $1 $2 $3 ...
  set -- ${i//:/ }
  if [ ! -e $1 ]; then
    echo "ERROR: $1 not exists"
    exit 2
  elif [ "$4" != "recursive" ]; then
    # processing non recursive entries
    [ "$3" != "not_set" ] && chmod $3 $1
    [ "$2" != "not_set" ] && chown $2 $1
  elif [ "$4" = "recursive" -a -z "$5" ]; then
    # processing recursive entries without type
    [ "$3" != "not_set" ] && chmod -R $3 $1
    [ "$2" != "not_set" ] && chown -R $2 $1
  elif [ "$4" = "recursive" -a "$5" = "directory" ]; then
    # processing recursive directories
    [ "$3" != "not_set" ] && find $1 -type d -exec chmod $3 {} \;
    [ "$2" != "not_set" ] && find $1 -type d -exec chown $2 {} \;    
  elif [ "$4" = "recursive" -a "$5" = "file" ]; then
    # processing recursive files
    [ "$3" != "not_set" ] && find $1 -type f -exec chmod $3 {} \;
    [ "$2" != "not_set" ] && find $1 -type f -exec chown $2 {} \;
  else 
    echo "can't process $i"
    exit 3
  fi
done

# empty directories
###################
if [ "$RMDIRS" ]; then
  for i in $RMDIRS; do
    if [ ! -e $i ]; then
      echo "ERROR: $i not exists"
      exit 2
    else
      rm -rf $i/*
    fi
  done
fi  
