#! /bin/sh

create_file ()
{
  touch $1
  chmod 666 $1
}

#
# Start
#

if [ -z "$PROJ" -o -z "$TARGET_DIR" -o -z "$BOOT_DIR" ] ; then
  echo
  echo "At least one of the variables PROJ [$PROJ], TARGET_DIR [$TARGET_DIR],"
       " or BOOT_DIR [$BOOT_DIR] is not defined !!!"
  echo
  exit -1
fi

CYCLADES_DIR=$PROJ/cyclades

if [ ! -d $PROJ ] ; then
  echo
  echo The PROJECT directory $PROJ does not exist !!!
  echo
  exit -1
fi

if [ ! -d $TARGET_DIR ] ; then
  echo 
  echo The target directory $TARGET_DIR does not exist !!!
  echo
  exit -1
fi

if [ ! -d $CYCLADES_DIR ] ; then
  echo 
  echo The source directory $CYCLADES_DIR does not exist !!!
  echo
  exit -1
fi

# creates $BOOT_DIR
cd /
if [ ! -d $BOOT_DIR -a ! -L $BOOT_DIR ] ; then
  install -m 755 -d $BOOT_DIR
fi

cd $TARGET_DIR

# create all directories
TARGET_DIRS="bin dev home home/admin etc/portslave/filters etc/network etc/ppp \
etc/security etc/ssh lib libexec mnt/DB_nfs opt/hardhat/devkit/ppc/8xx \
proc root tmp usr/local usr/share var var/empty var/pmd var/home \
var/lock/subsys var/log var/run/DB var/run/netreport var/state var/spool/cron/crontabs"

for file in  $TARGET_DIRS; do
  install -m 755 -d $file
done

chown 104:104 home/admin

# Bug#1225/1226 chmod -R 777 var
chmod -R 755 var
chmod -R 755 var/home
chmod -R 777 tmp
chmod -R 700 root

echo 00000000 > var/log/radsession.id

# build ttyS
cd $TARGET_DIR/dev
../../mk_devs $TARG


# build especial devices for WDT and LED's
cd $TARGET_DIR/dev
mknod -m 0666 wdt c 10 130
mknod -m 0666 led c 10 131
mknod -m 0666 sm_atx c 10 132
mknod -m 0666 sm_ipmi c 10 133
mknod -m 0666 sm_sensor c 10 134
mknod -m 0666 gpio c 10 136
mknod -m 0666 adc1 c 10 137
mknod -m 0666 adc2 c 10 138

# copy the others divice from original target provided by Montavista
cd $PROJ
for device in `cat dev_files`
do
	cp -af /opt/hardhat/devkit/ppc/8xx/target/dev/$device $TARGET_DIR/dev/ 
done

chmod 766 $TARGET_DIR/dev/console

# creates sbin as a link to bin
cd $TARGET_DIR
ln -sf bin sbin

# creates the path to the runtime libraries !!! 
# WHY ???
cd $TARGET_DIR/opt/hardhat/devkit/ppc/8xx
ln -sf / powerpc-hardhat-linux

# create users directories
cd $TARGET_DIR/usr
ln -sf ../bin sbin
ln -sf ../bin bin
ln -sf ../bin libexec
ln -sf ../lib lib

cd $TARGET_DIR/lib
ln -sf . security

cd $TARGET_DIR/usr/local
ln -sf ../../bin bin
ln -sf ../../bin sbin
ln -sf ../../lib lib

cd $TARGET_DIR/var
create_file run/utmp 
ln -sf /dev/null log/wtmp

# copy all lib's and bin's files from original target
# provided by Montavista also strip them
cd $PROJ

if [ -f lib_files.$TARG ] ; then
  ./cp_files $TARGET_DIR lib_files.$TARG
else
  ./cp_files $TARGET_DIR lib_files
fi

if [ -f bin_files.$TARG ] ; then
  ./cp_files $TARGET_DIR bin_files.$TARG
else
  ./cp_files $TARGET_DIR bin_files
fi

#[RK]Nov/26/02 - W-CAS
mv $TARGET_DIR/bin/w  $TARGET_DIR/bin/w_ori

# Copy the cyclades files

if [ -f cyc_files.$TARG ] ; then
  cyc_files="cyc_files.$TARG"
else
  cyc_files="cyc_files"
fi

cd $CYCLADES_DIR

cat $PROJ/$cyc_files | while read mode owner group file save
do
	if [ -d $file ]
	then
		if [ ! -d $TARGET_DIR/$file ]
		then
			mkdir $TARGET_DIR/$file
			chown $owner:$group $TARGET_DIR/$file
			chmod $mode $TARGET_DIR/$file
		fi
	else
		if [ -f $file.$TARG ] ; then
			FILE="$file.$TARG"
		else
			FILE="$file"
		fi

		if [ "$save" = "" ] ; then
			install -D -o $owner -g $group -m $mode $FILE $TARGET_DIR/$file
		else
			install -D -o $owner -g $group -m $mode $FILE $TARGET_DIR/$save
		fi
	fi
done

cd $PROJ

exit 0
