#!/bin/sh
#
# Set file permissions in the system to known defaults.
# options:
#	-r recursively descend directories.
#	-v verbose
# The rest are the same as install.
#

function Chown()
{	if [ ! -d $2 ]; then
		mkdir $2
	fi
	echo -n "."
	chown $1 $2
}

function setallperms()
{
	if [ ! -f sbin/reboot ]; then
		(cd sbin; ln halt reboot)
	fi
	if [ -f etc/shadow -a -f etc/gshadow ]; then
		chown root.shadow etc/shadow etc/gshadow;	
		chmod 660 etc/shadow etc/gshadow
		chown root.shadow etc/passwd etc/group etc/magic etc/inittab
		chmod 664 etc/passwd etc/group etc/magic etc/inittab
	fi
	# the line below makes tetris work but may not be secure...
	# chmod 777 usr/local/etc
	chown root.root /;	chmod 755 /
	chown root.root bin;	chmod 755 bin
	chown root.root sbin;   chmod 755 sbin
	chown root.root usr;	chmod 755 usr
	chown root.root lib;	chmod 755 lib
	chown root.root home;	chmod 755 home
	chown root.root etc;	chmod 755 etc
	# Chown root.root user;	chmod 755 user
	chown root.root lib/*;	chmod 755 lib/*
	Chown root.root tmp;	chmod 1777 tmp
	Chown root.root mnt;	chmod 755 mnt
	Chown root.root usr/tmp;	chmod 1777 usr/tmp
	chown root.root usr/bin;	chmod 755 usr/bin
	chown root.root usr/sbin;	chmod 755 usr/sbin
	chown root.root usr/lib;	chmod 755 usr/lib
	find usr/lib -type f  -exec chmod a+r \{\} \; -or -type d -exec chmod a+rx \{\} \;
	Chown root.root usr/man;	chmod 755 usr/man
	chown root.root usr/man/*/*;	chmod 644 usr/man/*/*
	chown root.root usr/man/cat?;	chmod 755 usr/man/cat?
	chown root.root usr/man/preformat/*;	chmod 755 usr/man/preformat
	chown root.root usr/man/man?;	chmod 755 usr/man/man?
	Chown root.root usr/local; chmod 755 usr/local
	Chown root.root usr/local/bin; chmod 755 usr/local/bin
	Chown root.root usr/spool;	chmod 755 usr/spool
	Chown root.mail usr/spool/mail;	chmod 775 usr/spool/mail
	Chown uucp.uucp usr/spool/uucp;	chmod 1777 usr/spool/uucp
	Chown root.root usr/spool/cron;	chmod 1777 usr/spool/cron
	Chown root.root usr/spool/cron/crontabs;	chmod 1777 usr/spool/cron/crontabs
	Chown root.root usr/adm;	chmod 755 usr/adm
	chown root.root etc/profile;	chmod 755 etc/profile
	find usr/man -type f  -exec chmod a+r \{\} \; -or -type d -exec chmod a+rx \{\} \;
	# ln -sf usr/adm/lastlog .
	Chown root.root proc;  chmod 755 proc
	grep -s "/proc" etc/fstab > /dev/null 2>&1
	if [ $? != 0 ]; then
		echo "/proc	/proc	proc	defaults" >> etc/fstab;
	fi
}

ROOTDIR=/
if [ '-instroot' = "$1" ]; then
	ROOTDIR=$2
	shift 2;
fi
if [ '-install' = "$1" ]; then
	cd $ROOTDIR
	echo
	echo "berprfe und korrigiere Datei-Zugriffsrechte."
	echo "Das kann einige Minuten dauern."
	echo
	echo "     Bitte warten Sie ... "
	setallperms
        echo
	echo "OK. Zugriffsrechte berprft."
	if [ -e fstab.tmp ]; then
		mv fstab.tmp etc/fstab.org
	fi
fi
