#!/bin/sh
#  Title	: fixp - fixing permissions of ArtCom software
#  Version      : $Id: fixp,v 1.20 2007-05-08 20:57:31 pf Exp $

$DBG_SH # dormant debugging directive as suggested by DDJ 1992

# Auch wenn BITSLIB von aussen definiert sein sollte, ist dieser
# Wert nicht "vertrauenswrdig", da er wahrscheinlich aus einem
# alten mustermann/.-File stammt.  Deshalb hier in jedem Fall mit
# dem kanonisch ermittelten Installationsort operieren, damit das
# "richtige" bitslib (z.B. /opt/combine/bitslib) genommen wird:
CALLED_IN=`dirname $0` # this is bitslib/service
BITSLIB=`cd $CALLED_IN/..; /bin/pwd`; export BITSLIB
# initialize the ArtCom environment and honor any 
# instance configuration files:
. $BITSLIB/bin/.ac-sh-funcs # defines $OS, $LANGUAGE, and 
# more: $AC_PROJECTS, $AC_CUSTOMER, $BITSDAT, $HOST ...

PERMS=$BITSLIB/service/perms.all
FIXPERM=$BITSLIB/service/fixperm.py 

if   [ -n "$1" ]
then OPTS="$*"  # Angegebene Optionen fr fixperm bernehmen
elif [ "$OS" = "Linux" ]
then OPTS="-cF" # zustzlich 'force links'
else OPTS="-c"  # Ansonsten Defaultoptionen
fi

MSG_CAT=$BITSLIB/service/SERVImsg.${LANGUAGE}
. $BITSLIB/bin/.Messages

# Wir sind erstmal optimistisch, da alles klappt:
EXITCODE=0

# Prfen, ob Aufrufer Superuser Rechte besitzt :
if [ -w /etc/passwd ]
then	# remove a possibly existing old file:
	[ -f $BITSDAT/noname.log ] && rm -f $BITSDAT/noname.log
	# Auf Andreas besonderen Wunsch Fonts fr alle schreibbar machen :
        if [ -d $AC_PROJECTS/fonts ]
        then ( cd $AC_PROJECTS/fonts;
               find . -follow -type d -print | xargs chmod 777 2>/dev/null
               find . -follow -type f -print | xargs chmod 666 2>/dev/null
	     )
        fi
	# Rechte in und fr $TEX_DIR allgemein setzen
        cd `dirname $BITSLIB` # CWD == Installation-Root (normally /opt/artcom)
        # (die Rechte fr die Binaries werden in perms.all behandelt)
        TEX_DIR=$BITSLIB/../tex/
        if [ -d $TEX_DIR ]
        then ( find $TEX_DIR -follow -type d -print | xargs chmod 775 2>/dev/null
               find $TEX_DIR -follow -type f -print | xargs chmod 664 2>/dev/null
               chown    256:bits $TEX_DIR  2>/dev/null
               chown -R 256:bits $TEX_DIR* 2>/dev/null
	     )
        fi
	# If for some reason perms.all is incomplete (doesn't contains
	# entries for every file) we should at least make everything 
	# world readable below $BITSLIB:
	chmod -R a+rX $BITSLIB 2>/dev/null
	# and also group writeable for us (group bits):
	chmod -R g+w $BITSLIB 2>/dev/null

	case $OS in
	     SunOS) :
		    # @@SUN
		    # an OPTS=-c wird erkannt, da das "Default"-
		    # Verhalten erwartet wird (mk)
		    if   [ "$OPTS" = "-c" ]
		    then $FIXPERM -c -u A3D -u ASCO  -u OSCO \
				     -u AIRIX -u OIRIX $PERMS || EXITCODE=1
		    else $FIXPERM $OPTS $PERMS || EXITCODE=1
		    fi
		    ;;
	     IRIX) :
		    # @@IRIX
		    # an OPTS=-c wird erkannt, da das "Default"-
		    # Verhalten erwartet wird (mk)
		    if   [ "$OPTS" = "-c" ]
		    then $FIXPERM -c -u A3D -u ASCO  -u OSCO \
				     -u ASUN5 -u OSUN5 $PERMS || EXITCODE=1
		    else $FIXPERM $OPTS $PERMS || EXITCODE=1
		    fi
		    ;;
             Linux) $FIXPERM $OPTS -u A3D -u ASCO  -u OSCO \
				   -u AIRIX -u OIRIX \
				   -u ASUN5 -u OSUN5 $PERMS || EXITCODE=1
	            ;; 
	     *)     $FIXPERM $OPTS $PERMS || EXITCODE=1
		    ;;
        esac
	OSPERMS="$BITSLIB/service/$OS/perms"
	if   [ -r "$OSPERMS" ]
	then for perms in $OSPERMS*; do 
	         $FIXPERM -c $perms || EXITCODE=1
	     done
	fi
        # Da unsere Software die Message-Kataloge schon seit vielen
	# Jahren ohnehin nur noch direkt aus $BITSLIB/env/$LANGUAGE 
	# entnimmt, ist folgendes berflssig und sogar schdlich :
	# if [ "$LANGUAGE" ] && [ x$LANGUAGE != xdeu ] 
	# then : switch back to native language
	#      ln $BITSLIB/env/${LANGUAGE}/* $BITSLIB/env
	# fi
else	echo "Um dieses Kommando anwenden zu koennen, muss man super user sein"
    	echo "To make use of this command you have to become super user"
        EXITCODE=1
fi

exit $EXITCODE
# - EOF -
