#!/bin/sh
#This script creates  a root disk for double boot disks.
#This script has been tested on Vine Linux 2.6r1 only.

PATH=$PATH:/sbin:/usr/sbin
mkrd_pwd=`pwd`
version=0.2.5
org_version=$version
nickname=kotori
rd_count=8192
auto_dyn_lib=1

#definition of functions.

jp_echo()       {
		 if [ "$LANG" = "ja_JP.eucJP" ];then

			echo "$*"

		 fi
		 }


eng_echo()      {
		 if [ "$LANG" != "ja_JP.eucJP" ];then

			    echo "$*"

		 fi
		 }


jp_verb_echo()    {
		   if [ "$LANG" = "ja_JP.eucJP" ];then

			if [ "$verbose" = 1 ];then

			    echo "$*"

			fi

		    fi
		   }
		    

eng_verb_echo()    {
		    if [ "$LANG" != "ja_JP.eucJP" ];then

			if [ "$verbose" = 1 ];then

			    echo "$*"

			fi

		    fi
		   }

db_echo()         {
		    if [ "$db" = 1 ];then
			echo "$*"

			if [ "$2" ];then    
			    echo -n stoped for debug
			    read db_val
			    echo restarts
			fi
			echo
		    fi
		   }

rd_exit()          {
		    echo "$2"
		    if [ "$3" ];then

			if [ "$LANG" = "ja_JP.eucJP" ];then
			    echo "$2行目で、$3の実行に失敗しました。終了します。"

			else
			    echo "On line $2,$3 error occured.Aborted."

			fi

		    fi
		    cd "$mkrd_pwd"

		    if [ "$db" = 1 ];then
			jp_echo "デバッグモードなので、$rd_rootはumount、削除されません。"
			eng_echo "In debug mode,$rd_root was NOT umounted and deleted."

		    else
			umount "$rd_root"
			rm -rf "$rd_root"
		    fi
		    exit "$1"
		   }

ldd_lib_out() { ldd_lib_1=`ldd $1`;ldd_ev=$?
    for t in $ldd_lib_1 ;do
	if [ "$ldd_ev" != 0 ];then
	    echo not_elf
	    return 1
	fi
	
	if [ ${t:0:1} = / ];then
	    if [ -e $t ];then
		echo $t
	    else
		continue
	    fi
	else
	    continue
	fi
    done
}

#permission starts.

if [ `id -u` != 0 ];then

    jp_echo "このスクリプトの実行にはroot権限が必要です。"
    jp_echo "終了します。"
    eng_echo "You need the privilege of root to execute `basename $0`."
    eng_echo "Aborted."
    exit 1

fi

#permission ends.

if [ `pwd` = "/" ];then

    jp_echo "このスクリプトを/で実行するのは危険です。"
    jp_echo "PWDを変更してください。終了します。"
    eng_echo "this script is too dangerous to execute in root directory."
    eng_echo "Change PWD,aborted."
    exit 1

fi 

if [ -x /sbin/getty ];then
    getty_com=/sbin/getty
elif [ -x /sbin/mingetty ];then
    getty_com=/sbin/mingetty
elif [ -x /sbin/getty ];then
    getty_com=/sbin/mingetty
else
    getty_com=/sbin/getty
fi
#value validation starts.
default_commands="/bin/ash /sbin/badblocks /bin/cat /bin/chmod /usr/sbin/chroot /bin/cp /bin/dd \
/bin/df /sbin/e2fsck /bin/ed /sbin/fdisk /usr/bin/ftp /bin/gzip /sbin/ifconfig /sbin/insmod /bin/kill \
/bin/ln /bin/ls /sbin/lsmod /bin/mkdir /sbin/mke2fs /bin/mknod /sbin/modprobe /bin/more /bin/mount \
/bin/mv /bin/ping /bin/ps /bin/rm /sbin/rmmod /sbin/route /sbin/swapon /sbin/swapoff /bin/sync \
/bin/tar /bin/umount /bin/dmesg /usr/bin/free /usr/bin/ldd"

#default commands for router.
default_commands_for_router="/bin/ash /bin/cat /bin/chmod /bin/cp /bin/df /bin/ed /usr/sbin/chroot \
/usr/bin/ftp /sbin/ifconfig /sbin/insmod /bin/kill /bin/ln /bin/ls /sbin/lsmod /bin/mkdir \
/bin/mknod /sbin/modprobe /bin/mount /bin/ping /bin/ps /bin/rm /sbin/rmmod /sbin/route /sbin/swapon \
/sbin/swapoff /bin/sync /bin/umount /usr/sbin/in.telnetd $getty_com /bin/login /usr/bin/passwd \
/bin/chown /sbin/init /usr/sbin/inetd /usr/sbin/tcpd /bin/su /sbin/pidof /sbin/halt /bin/dmesg"

#default commands for iso.img.
default_commands_for_iso="/bin/bash /bin/cat /bin/chmod /bin/cp /bin/df /bin/ed /usr/sbin/chroot \
/usr/bin/ftp /sbin/ifconfig /sbin/insmod /bin/kill /bin/ln /bin/ls /sbin/lsmod /bin/mkdir /bin/mknod \
/sbin/modprobe /bin/mount /bin/ping /bin/ps /bin/rm /bin/mv /sbin/rmmod /sbin/route /sbin/swapon \
/sbin/swapoff /bin/sync /bin/umount /usr/sbin/in.telnetd $getty_com /bin/login /usr/bin/passwd \
/bin/chown /sbin/init /usr/sbin/inetd /sbin/depmod /usr/sbin/tcpd /bin/su /sbin/pidof /sbin/halt \
/sbin/lilo /usr/bin/less /bin/rmdir /usr/bin/telnet /usr/bin/du /usr/bin/w3m /sbin/fdisk /bin/dd \
/sbin/e2fsck /sbin/mke2fs /bin/gzip /bin/tar /bin/grep /usr/bin/expr /sbin/iptables /sbin/syslogd \
/sbin/badblocks /usr/bin/find /bin/touch /bin/dmesg /bin/date /sbin/hdparm /sbin/hwclock /sbin/lspci \
/sbin/klogd /usr/bin/nslookup /sbin/ldconfig /sbin/initlog /sbin/sulogin /sbin/change_console /bin/loadkeys"

#default commands for jail
default_commands_for_jail="/bin/bash /bin/cat /bin/cp /bin/df /bin/ed /usr/bin/ftp /bin/kill \
/bin/ln /bin/ls /bin/mkdir /bin/ping /bin/ps /bin/rm /bin/rmdir /bin/su /usr/bin/w3m /bin/jvim \
/usr/bin/telnet /usr/bin/tee /bin/grep /bin/tar /bin/gzip /usr/bin/less /bin/sed /bin/awk /bin/chmod \
/bin/date /sbin/route /bin/dd /bin/mv"

###obsolete
default_libs="/lib/libc-2.2.4.so /lib/ld-2.2.4.so /lib/libtermcap.so.2.0.8 \
/lib/libresolv-2.2.4.so /lib/libproc.so.2.0.6 /lib/libuuid.so.1.2 /lib/libe2p.so.2.3 \
/lib/libcom_err.so.2.0 /lib/libext2fs.so.2.4 /usr/lib/libreadline.so.4.1 /usr/lib/libncurses.so.5.2 \
/lib/libnss_files-2.2.4.so"

if [ -x /sbin/udevd ]&&[ ! "$no_udev" ];then
    default_udev_bin="/sbin/start_udev /sbin/udevcontrol /sbin/udevd /sbin/udevd.static /sbin/udevsettle \
/sbin/udevstart /sbin/udevtrigger /usr/bin/udevinfo /usr/bin/udevtest /usr/sbin/udevmonitor \
/usr/bin/awk /sbin/pam_console_apply /bin/mktemp"

    test_udev_bin=""
    for t in $default_udev_bin ;do
	if [ -f $t ];then
	    test_udev_bin="$test_udev_bin $t"
	fi
    done

    default_udev_bin="$test_udev_bin"
    for t in `find /lib/udev ! -type d` ;do
	default_udev_bin="$default_udev_bin $t"
    done

    for t in `find /lib/firmware ! -type d` ;do
	default_udev_bin="$default_udev_bin $t"
    done
fi

if [ `echo $*|grep -c -e -J` != 0 ]&&[ `echo $*|grep -c -e -R` != 0 ];then

    jp_echo "-R と -J は、同時に指定できません。"
    eng_echo "-R and -J,only the one can be defined."
    exit 1

fi

if [ `echo $*|grep -c -e -R` != 0 ];then

    router_val=1
    jail_val=0
    default_commands=$default_commands_for_router
    version="$version for router"
    jp_rd_form="2FDルータ"
    eng_rd_form="2FD router"

elif [ `echo $*|grep -c -e -I` != 0 ];then

    jail_val=0
    router_val=1
    iso_val=1
    rd_count=$((8192*16))
    default_commands=$default_commands_for_iso
    version="$version for iso.img"
    jp_rd_form="isoイメージ用"
    eng_rd_form="root.iso.img"

elif [ `echo $*|grep -c -e -J` != 0 ];then

    jail_val=1
    router_val=1
    default_commands=$default_commands_for_jail
    version="$version for Jail"
    jp_rd_form="jailルートシステム"
    eng_rd_form="jail root system"

else

    jp_rd_form="レスキューディスク"
    eng_rd_form="rescue disk"
    def_res=1
fi

if [ "$nickname" ];then
    version="$version ($nickname)"

fi


if [ `echo $*|grep -c -e -P` != 0 ];then

    Private=1
else

    Private=0
fi

if [ `echo $*|grep -c -e -F` != 0 ];then

    conf_fval=1
    Private=1 
else
     conf_fval=0
fi

if [ `echo $*|grep -c -e -ln` != 0 ];then

    auto_dyn_lib=0
fi


#default_commands_2=`echo $default_commands|sed 's/sbin//g'|sed 's/usr//g'|sed 's/bin//g'|sed 's/\///g'`

for com_name in $default_commands ;do

    com_name=`basename $com_name`
    default_commands_2="$default_commands_2 $com_name"

done

#default_libs_2=`echo $default_libs|sed 's/lib\///g'|sed 's/usr//g'|sed 's/\///g'`

for lib_name in $default_libs ;do

    lib_name=`basename $lib_name`
    default_libs_2="$default_libs_2 $lib_name"

done

help="
mkrd-$version
-v   verbose mode.
-lc  list default commands set.
-ca  add commands, example -ca '/bin/grep /bin/tar',
     use only absolute path.
-cd  delete commands from default commands set,
     example -cd 'ping e2fsck', only file name.
-rs  a size of root file system(KB), example -rs 16384,
     it means 16MB. default size is 4MB.
-s   size calculation only.
-R   make root file system for router.
-I   make root file system for iso.img.
-F   a private configuration file defined, example -F mkrd.conf.
"

if [ $# != 0 ];then

    mkrd_args=$*

    while [ "$1" != "" ];do

	case $1 in

	    "-v"  ) shift
		verbose=1
		;;

	    "-lc" | "list-c" )

		if [ `echo $mkrd_args|grep -c -e -v` != 0 ];then

		    echo $default_commands
		    
		else
    
		    echo $default_commands_2
    
		fi

		exit 0

		;;

	    "list-l" )
		if [ $auto_dyn_lib = 0 ];then

		    if [ `echo $mkrd_args|grep -c -e -v` != 0 ];then

			echo $default_libs
		
		    else

			echo $default_libs_2
		
		    fi

		else

		    jp_echo "list-lのかわりに現在のオプションに -s -vを加えて実行して下さい。"
		    eng_echo "Replace from list-l to -s -v,and retry." 		    

		fi

		exit 0
		
		;;

	    "list-all" )
		if [ $auto_dyn_lib = 0 ];then

		    if [ `echo $mkrd_args|grep -c -e -v` != 0 ];then

			echo $default_commands
			echo	
			echo $default_libs
		
		    else

			echo $default_commands_2
			echo
			echo $default_libs_2
		
		    fi

		else

		    jp_echo "list-allのかわりに現在のオプションに -s -vを加えて実行して下さい。"
		    eng_echo "Replace from list-all to -s -v,and retry." 		    
		    
		fi
		
		exit 0

		;;

	    "-s"  ) shift
		t_size_val=1
		;;

	    "-ca" ) shift

		if [ "$1" = "" ];then

		    jp_echo "追加するコマンドのリストがありません。"
		    eng_echo "commnds list for add is none."
		    
		    exit 1
		    
		fi
 

		c_add=$1
		shift
		;;
	    
	    "-cd" ) shift

		if [ "$1" = "" ];then

		    jp_echo "削除するコマンドのリストがありません。"
		    eng_echo "commnds list for delete is none."
			
		    exit 1
			
		fi
 
		c_del=$1
		shift
	    ;;
	    
	    "-la" ) shift

		if [ "$1" = "" ];then

		    jp_echo "追加するライブラリのリストがありません。"
		    eng_echo "library list for add is none."

		    exit 1
		fi

		for ld in $1
		  do
		  l_add="$l_add ${ld%%/}"
		done
		shift
		;;

	    "-ld" ) shift

		if [ "$1" = "" ];then

		    jp_echo "削除するライブラリのリストがありません。"
		    eng_echo "library list for delete is none."

		    exit 1
		    
		fi
		
		l_del=$1
		shift
		;;

	    "-ln" ) shift
		auto_dyn_lib=0
		;;

	    "-dp" ) shift
		depend=1
		;;

	    "-cs" ) shift
		c_size_val=1
		;;

	    "-ls" ) shift
		l_size_val=1
		;;
	    "-fa" ) shift

		if [ ! "$1" ];then
		    echo "-fa value is not specified." 
		    exit 1
		fi

		for tt in $1 ;do

		    if [ -f "$1" ];then
			echo "$1,such file or directory is not found." 
			exit 1
		    fi

		    if [ `echo $tt|cut -c 1` != '/' ];then
			echo "$tt,-fa value must be absolute path."
			exit 1
		    fi
		    
		    done 
		fa_val="$1"
		shift
		;;
	    
	    "-rs" ) shift

		if [ "$1" = "" ];then

		    jp_echo "値が設定されていません。"
		    eng_echo "Root file system size is not given."

		    exit 1

		fi
		
		rd_p_size=$1
		
		shift
		;;

	    "-V" ) echo $version
		   exit 0
		   ;;

	    "-i" ) shift
		   mkrd_l_rc=1
		   ;;

	    "-R" ) shift
		;;

	    "-I" ) shift
		;;

	    "-F" ) shift

		if [  "$1" = "" ];then

		    jp_echo "設定ファイルが指定されていません。"
		    eng_echo "your private conf file is none."

		    exit 1

		fi

		if [ ! -f $1 ];then

		    jp_echo "$1のようなファイルはありません。"
		    eng_echo "Such file is none."

		    exit 1			

		fi

		if [ `echo $1|grep -c ^/` = 0 ];then

		    prv_file=`pwd`/$1
		    
		else
		    
		    prv_file=$1

		    fi
		shift
		;;
	    
	    "-P" ) shift
		;;
	    
	    "-D" ) shift
		db=1
		;;
	    
	    "-no-udev" ) shift
		no_udev=1
		;;

	    "-no-686" ) shift
		no_686=1
		;;
	    * )
		jp_echo "$1は正しくない引数、もしくは正しくない位置にあります。"
		eng_echo "$1 is invalid parameter or value."
		echo "$help"
		exit 1
		;;
	esac

    done

fi

if [ ! "$rd_p_size" ];then

    if [ "$jail_val" = 1 ];then
	rd_count=$(($rd_count*4))
    fi

else
    rd_count=$(($rd_p_size*2))
fi

rd_img_size=$(($rd_count*512))
if [ "$rd_img_size" = 0 ];then

    jp_echo "$rd_p_size は不正な値です。"
    eng_echo "$rd_p_size is false value."

    exit 1

fi
db_echo "rd_count is $rd_count,rd_img_size is $rd_img_size."

if [ -x /sbin/udevd ]&&[ "$no_udev" != 1 ];then
    ok_udev=1
    default_commands="$default_commands $default_udev_bin"
else
    ok_udev=0
fi

############################################################
#############     privat configuration      ################
############################################################
db_echo "private configuration starts."
if [ "$Private" = 1 ]&&[ "$conf_fval" = 1 ];then

    opt_list="HostName HostIpAddr DefaultGateWay Search NameServer HostNetAddr HostNetMask NetDev Broadcast \
BootProto OnBoot Networking EtcHostsFile RcLocalFile"

    for pp_val in $opt_list ;do

	hh=`cut -d"#" -f1 $prv_file |grep -v OverWrite |grep $pp_val|tail -n 1`

	if [ ! "$hh" ];then
	   jp_echo "設定ファイルに不正な値が含まれています。"
	   eng_echo "There are some invalid value in config file."
	   exit 1
	fi
	    set $hh


	if [ "$2" ]&&[ `echo $1 |grep -c File` = 1 ];then

	    if [ `echo $2|grep -c ^/` = 0 ];then

		jp_echo "$1の$2は絶対パスで指定してください。"
		eng_echo "$2 for $1 must be absolute path."
		exit 1

	    fi

	    if [ ! -f "$2" ];then

		jp_echo "$2のようなファイルは存在しません。"
		eng_echo "$2,Such file is not found."
		exit 1

	    fi

	    if [ "$3" ];then
		jp_echo "$pp_valは一つしか指定できません。"
		eng_echo "$pp_val must be one file."
		exit 1
 
	    fi


	fi

	o_val=$1
	shift
	eval $pp_val=\"$*\"
	eval l_val='$'$pp_val
	jp_verb_echo $o_val is "$l_val"
	eng_verb_echo $o_val is "$l_val"

    done

fi

#jp_verb_echo start OVERWRITE detection.

if [ "$Private" = 1 ]&&[ "$conf_fval" = 1 ];then

grep -v \# "$prv_file" |grep OverWrite >/tmp/ow.tmp   
tl_num=`cat /tmp/ow.tmp|wc -l`

i=1

    while [ $i -le "$tl_num" ];do

	ow_f=`sed -n "$i"p /tmp/ow.tmp`
	set `sed -n "$i"p /tmp/ow.tmp` 
	jp_verb_echo $1 $2 $3
	eng_verb_echo $1 $2 $3

	if [ "$2" = command ];then

	    i=$(($i+1))
	    continue

	fi

	if [ `echo $2|grep -c ^/` = 0 ]&&[ "$2" ];then

	    jp_echo "$2は絶対パスで指定してください。"
	    eng_echo "$2 must be absolute path."
	    exit 1

	fi

	if [ `echo $3|grep -c ^/` = 0 ]&&[ "$3" ];then

	    jp_echo "$3は絶対パスで指定してください。"
	    eng_echo "$3 must be absolute path."
	    exit 1

	fi

	if [ ! -e "$2" ]&&[ "$2" ];then

	    jp_verb_echo "$2のようなファイルやディレクトリは見つかりません。"
	    eng_verb_echo "$2,Such file or directory is not found."
	    exit 1

	fi


	i=$(($i+1))

    done

fi

db_echo end setting of variables. on line $LINENO
###################################################
######### create total list.             ##########
######### added value validation starts. ##########
###################################################

if [ "$c_add" ];then

    for a_command in $c_add
    do

	if [ ! -f "$a_command" ];then

	    jp_echo "$a_command、そのようなファイルは存在しません。"
	    jp_echo "終了します。"
	    eng_echo "$a_command,such a file does not exist."
	    eng_echo "aborted."
	    exit 1

	fi

	if [ ! `echo $a_command|grep -e '^/'` ];then

	    jp_echo "$a_commandは絶対パスでなくてはなりません。"
	    jp_echo "終了します。"
	    eng_echo "$a_command must be abosolute path."
	    eng_echo "aborted."
	    exit 1

	fi

	if [ `echo $default_commands|grep -c $a_command` = 1 ];then

	    jp_echo "$a_commandはすでにデフォルトのリストに存在します。"
	    jp_echo "終了します。"
	    eng_echo "$a_command is already exists in default commands."
	    eng_echo "aborted."
	    exit 1

	fi
	
    done

fi

if [ "$l_add" ];then

    for a_lib in $l_add
    do

	if [ ! -f $a_lib ]&&[ ! -d $a_lib ];then

	    jp_echo "$a_lib、そのようなファイル、またはディレクトリは存在しません。"
	    jp_echo "終了します。"
	    eng_echo "$a_lib,such a file or a directory does not exist."
	    eng_echo "aborted."
	    exit 1

	fi
	
	if [ ! `echo $a_lib|grep -e '^/'` ];then

	    jp_echo "$a_libは絶対パスでなくてはなりません。"
	    jp_echo "終了します。"
	    eng_echo "$a_lib must be abosolute path."
	    eng_echo "aborted."
	    exit 1

	 fi

	 if [ `echo $default_libs|grep -c $a_lib` = 1 ];then

	    jp_echo "$a_libはすでにデフォルトのリストに存在します。"
	    jp_echo "終了します。"
	    eng_echo "$a_lib is already exists in default libraries."
	    eng_echo "aborted."
	    exit 1

	 fi
	
    done

fi

#delete value validation

if [ "$c_del" ];then

    for d_command in $c_del
    do

	if [ ! `echo $default_commands_2|grep -w -c ${d_command}` != 0 ];then

	    jp_echo "$d_command、そのようなコマンドはデフォルトのリストに存在しません。"
	    jp_echo "終了します。"
	    eng_echo "$d_command,such a command does not exist in default commands."
	    eng_echo "aborted."
	    exit 1
	    
	 fi

    done

fi

if [ "$auto_dyn_lib" != 1 ];then 

    if [ "$l_del" ];then

	for d_lib in $l_del
	do

	    if [ `echo $default_libs_2|grep  -w -c ${d_lib}` = 0 ];then

		jp_echo "$d_lib、そのようなライブラリはデフォルトのリストに存在しません。"
		jp_echo "終了します。"
		eng_echo "$d_lib,such a file does not exist in default library."
		eng_echo "aborted."
		exit 1

	    fi

	done

    fi

fi

# create new_c_list.
# first deleted.
#
#new_c_list=$default_commands
tt_commands=$default_commands
if [ "$c_del" ];then

    for p_d_command in $c_del
    do
	new_c_list=""
	for tt in $tt_commands
	do

	    if [ `basename $tt` = $p_d_command ];then
		continue  
	    fi
	    new_c_list="$new_c_list $tt"

	done
	tt_commands=$new_c_list
    done

else
    new_c_list=$default_commands

fi

for c_exist in $new_c_list
do
    if [ ! -f "$c_exist" ];then

	jp_echo "$c_existは、このシステムに存在しません。"
	jp_echo "終了します。"
	eng_echo "$c_exist does not exist in your system."
	eng_echo "aborted."
	exit 1

    fi

done

#creating c_add list.
if [ "$c_add" ];then

    for p_a_command in $c_add
    do

	if [ `echo $new_c_list|grep -c ^$p_a_command'$'` != 0 ];then
	    continue
	fi
	new_c_list="$new_c_list $p_a_command"
	    
    done

fi

if [ "$auto_dyn_lib" != 1 ];then
	
    new_l_list=$default_libs

    for p_d_lib in $l_del
    do

	new_l_list=`echo $new_l_list|awk 'BEGIN{RS=" "}{print}'|grep -v $p_d_lib|awk 'BEGIN{RS="%"}{print}'`

    done


#create l_add.

    if [ "$l_add" ];then
	
	for p_a_lib in $l_add
	do

	    if [ `echo $new_l_list|grep $l_add` ];then
	    continue
	    fi
    
	    new_l_list="$new_l_list $p_a_lib"

	done

    fi

	    #created new_l_list.
	    #for auto detection starts. 
else
    if [ "$router_val" = 1 ];then

	if [ -d /lib/security ];then

	    lib_security_ls="/lib/security/pam_deny.so
 /lib/security/pam_permit.so
 /lib/security/pam_unix.so
 /lib/security/pam_limits.so
 /lib/security/pam_access.so
"

#	lib_security_ls="/lib/security/*"

	    for t in $lib_security_ls ;do

		if [ -d $t ];then
		    
		    pam_d_list=$t
		    while [ "$pam_d_list" ];do

			pam_d_list_f=$pam_d_list
			pam_d_list=""

			for t1 in $pam_d_list_f ;do

			    for t2 in `ls -d $t1/*` ;do
			    if [ -d $t2 ];then
				pam_d_list="$pam_d_list $t2"
			    elif [ -f $t2 ];then
				required_libs_list="$required_libs_list $t2"
			    fi
			    done

			done

		    done

		elif [ -f $t ];then
		    required_libs_list="$required_libs_list $t"
		fi
	    done
	else

	    jp_echo "このシステムには、/lib/securityがありません。PAMをサポートしているならば、"
	    jp_echo "任意のディレクトリから/lib/securityにリンクをはって下さい。PAMをサポートし"
	    jp_echo "ていなければこのまま続行して下さい。続行しますか？(Y or N)"

	    eng_echo "On this system,/lib/security does not exist.If PAM support is implemented,"
	    eng_echo "link  from the PAM library directory to /lib/security,and retry.If PAM support"
	    eng_echo "is not implemented,continue simply.continue?(Y or N)."

	    read dummy

	    case "$dummy" in
		
		"Y" | "y" ) jp_echo "作業を再開します。"
		    eng_echo "the operation resumed."
		;;
	    
		"N" | "n" ) jp_echo "終了します。"
		    eng_echo "aborted."
		    exit 0
		    ;;
	    
		* ) jp_echo "予期せぬ値が与えられました、終了します。"
		    eng_echo "unexpected value was set,aborted."
		    exit 1
		    ;;

	    esac
		
	    required_libs_list=`echo $required_libs_list|awk 'BEGIN{RS=" "}{print}'|grep -v -e /lib/security`
	    required_libs_list=`echo $required_libs_list`
	    
	fi
    fi

    for ldd_test in $new_c_list ;do

	tmp_ldd=`ldd_lib_out $ldd_test`
	if [ "$tmp_ldd" = not_elf ];then
	    continue
	fi

	required_libs_list="$required_libs_list $tmp_ldd"
	required_libs_list=`echo $required_libs_list|awk 'BEGIN{RS=" "}{print}'|sort -u|uniq`
		
    done

    required_libs_list_old="$required_libs_list /lib/libnss_files.so.2 /lib/libnss_dns.so.2"
    required_libs_list=$required_libs_list_old
    l_def_val=0
    while [ "$l_def_val" ];do
	
	for ldd_test in $required_libs_list_old
	do

	    tmp_ldd=`ldd_lib_out $ldd_test`
	    if [ "$tmp_ldd" = not_elf ]||[ -d $ldd_test ];then
		continue
	    fi

	    required_libs_list="$required_libs_list $tmp_ldd"
	    required_libs_list=`echo $required_libs_list|awk 'BEGIN{RS=" "}{print}'|sort -u|uniq`

	done

	if [ "$required_libs_list" = "$required_libs_list_old" ];then

	    l_def_val=""

	else

	    required_libs_list_old=$required_libs_list
	    l_def_val=$(($l_def_val+1))

	fi
	
    done #l_def_val loop ends

    required_libs_list="$required_libs_list"
    if [ "$router_val" = 1 ];then

	if [ ! -d /lib/iptables ]&&[ "$jail_val" != 1 ];then

	    jp_echo "このシステムには、/lib/iptablesが存在しません。終了します。"
	    eng_echo "On this system, /lib/iptables does not exist, aborted."
	    exit 1

	fi
    fi

    if [ "$no_686" = 1 ];then

	for l_686 in $required_libs_list ;do

	    l_686_low=${l_686#*/i686/}
	    l_686_up=${l_686%/i686/*}
#echo if [ $l_686_low != $l_686 ]
	    if [ $l_686_low != $l_686 ];then

		no_l_686=$l_686_up/$l_686_low
#echo if [ -f $no_l_686 ]||[ -d $no_l_686 ]
#read
#echo
		if [ -f $no_l_686 ]||[ -d $no_l_686 ];then
		    df_ad_l_list="$df_ad_l_list $no_l_686"
		else
		    df_ad_l_list="$df_ad_l_list $l_686"
		fi
	    else
		df_ad_l_list="$df_ad_l_list $l_686"
	    fi
	done
	required_libs_list=$df_ad_l_list
    else
	df_ad_l_list=$required_libs_list
    fi
#echo $df_ad_l_list
#exit 0
    if [ "$auto_dyn_lib" = 1 ];then #delete lib,is it in ll_l_list?

	new_l_3=$required_libs_list

	for exist_a_lib in $l_del
	do

	    new_l_3=`echo $new_l_3|sed 's/\// /'|awk 'BEGIN{RS=" "}{print $NF}'|awk '{FS="/"}{print $NF}'`

	    if [ `echo $new_l_3|grep -c $exist_a_lib` != 0 ];then
	    continue
	    fi

	    jp_echo "$exist_a_libは共有ライブラリの中に存在しません。"
	    jp_echo "削除することは無意味です。終了します。"
	    eng_echo "$exist_a_lib dose not exist,-cd is meaningless.aborted."
	    exit 1
 
	done

    fi
	    #delete lib,is it in ll_l_list?ends.

    if [ "$l_del" ];then

	for ad_lib in $l_del
	do
	    required_libs_list=`echo $required_libs_list|awk 'BEGIN{RS=" "}{print}'|grep -v $ad_lib`

	done

    fi
	#tmp_req_list is "auto detect library" - l_del." 
	tmp_req_list=$required_libs_list

    if [ "$l_add" ];then 

	for aa_lib in $l_add
	do

	    if [ ! -f $aa_lib ]&&[ ! -d $aa_lib ];then

		jp_echo "$aa_lib、そのようなファイル、またはディレクトリは存在しません。終了します。"
		eng_echo "$aa_lib, such a file or a diredtory does not exist, aborted."
		exit 0
		    
	    fi

	  
		required_libs_list="$required_libs_list $aa_lib"

	done

   fi

    required_libs_list=`echo $required_libs_list|awk 'BEGIN{RS=" "}{print}'|sort -u|uniq`
    required_libs_list=`echo $required_libs_list`

    jp_verb_echo
    eng_verb_echo
    jp_verb_echo "自動判定によるライブラリの構成は以下のとおりです。"
    eng_verb_echo "auto detected library is"
    jp_verb_echo $df_ad_l_list
    eng_verb_echo $df_ad_l_list
    jp_verb_echo
    eng_verb_echo

    new_l_list=$required_libs_list
    req_all_l_list=$tmp_req_list

    for aa_lib in $required_libs_list
    do

	if [ -d $aa_lib ];then

	    for file_name in `ls $aa_lib`
	    do

		req_all_l_list="$req_all_l_list ${aa_lib}/${file_name}"

	    done

	    req_all_l_list=`echo $req_all_l_list|awk 'BEGIN{RS=" "}{print}'|grep -v $aa_lib\$`
	    req_all_l_list=`echo $req_all_l_list`

	else

	    req_all_l_list="$req_all_l_list $aa_lib"

	fi

    done

fi 

#for auto detection ends.

new_c_list=`echo $new_c_list`
req_all_l_list=`echo $req_all_l_list`

#created new_l_list.

jp_verb_echo "最終的なコマンドのリストは、 ${new_c_list}です。"
eng_verb_echo "new commands list is ${new_c_list}."
jp_verb_echo
eng_verb_echo
jp_verb_echo "最終的なライブラリのリストは、${req_all_l_list}です。"
eng_verb_echo "new library list is ${req_all_l_list}."
jp_verb_echo
eng_verb_echo

###echo created new lists.check system validation.
###echo $new_c_list

for c_exist in $new_c_list
do
    if [ ! -f "$c_exist" ];then

	jp_echo "$c_existは、このシステムに存在しません。"
	jp_echo "終了します。"
	eng_echo "$c_exist does not exist in your system."
	eng_echo "aborted."
	exit 1

    fi

done

for l_exist in $new_l_list
do

    if [ ! -f "$l_exist" ]&&[ ! -d "$l_exist" ];then
	
	jp_echo "$l_existは、このシステムに存在しません。"
	jp_echo "終了します。"
	eng_echo "$l_exist does not exist in your system."
	eng_echo "aborted."
	exit 1

    fi

done

#check /sbin/e2fsck
if [ $rd_count -le 8192 ]&&[ `echo $c_del|grep -c e2fsck` = 0 ];then

    e2_v1=`echo $new_c_list|grep -c -w /sbin/e2fsck`
    e2_v2=`ldd /sbin/e2fsck|grep -c -w executable`

    if [ $e2_v1 = 1 ]&&[ $e2_v2 = 1 ];then

	jp_echo "このシステムの/sbin/e2fsckは静的にコンパイルされています。"
	jp_echo "おそらく、この/sbin/e2fsckは大き過ぎて $jp_rd_formに入れるのは困難でしょう。"
	jp_echo "続行しますか？(Y/N)"
	eng_echo "/sbin/e2fsck in this system is not a dynamic executable."
	eng_echo "Maybe,/sbin/e2fsck is too large to install in this $eng_rd_form."
	eng_echo "continue ?(Y/N)"

	read dummy

	case $dummy in

	    "Y" | "y" ) jp_echo "再開します。"
			eng_echo "restarted."
	    ;;

	    "N" | "n" ) jp_echo "終了します。"
			eng_echo "aborted."
			exit 0
	    ;;

		    * ) jp_echo "予期せぬ値が与えられました。"
			jp_echo "終了します。"
			eng_echo "unexpected value?"
			eng_echo "aborted"
			exit 1
	    ;;

	esac
	echo
    fi
fi

#system requirement.

if [ `echo $new_c_list|awk 'BEGIN{RS=" "}{print}'|grep -c sh\$` = 0 ];then

    jp_echo "この$jp_rd_formには、シェルがありません。続行しますか？(Y or N)"
    eng_echo "there is no sh on this $eng_rd_form,continue?(Y or N)"

    read dummy

    case $dummy in

	    "Y" | "y" ) jp_echo "再開します。"
			eng_echo "restarted."
	    ;;

	    "N" | "n" ) jp_echo "終了します。"
			eng_echo "aborted."
			exit 0
	    ;;

		    * ) jp_echo "予期せぬ値が与えられました。"
			jp_echo "終了します。"
			eng_echo "unexpected value?"
			eng_echo "aborted"
			exit 1
	    ;;

    esac

fi

#if [ "$jail_val" != 1 ]&&[ `echo $new_c_list|grep -c open` = 0 ];then
#
#    jp_echo "$jp_rd_formのinitは、openに依存しています。続行しますか？(Y or N)"
#    eng_echo "The init of this $eng_rd_form can not work without \"open\",continue?(Y or N)"
#
#
#    read dummy
#
#    case $dummy in
#
#	    "Y" | "y" ) jp_echo "再開します。"
#			eng_echo "restarted."
#	    ;;
#
#	    "N" | "n" ) jp_echo "終了します。"
#			eng_echo "aborted."
#			exit 0
#	    ;;
#
#		    * ) jp_echo "予期せぬ値が与えられました。"
#			jp_echo "終了します。"
#			eng_echo "unexpected value?"
#			eng_echo "aborted"
#			exit 1
#	    ;;
#
#    esac
#
#
#fi

	
	    
#validation ends.

#search dependency.

require="$new_l_list $new_c_list"

if [ "$auto_dyn_lib" != 1 ];then 

    sup_libs=$new_l_list

    if [ `echo $sup_libs|grep -c libc-2.2.4.so` != 0 ];then

		sup_libs="$sup_libs /lib/libc.so.6"

    fi

    if [ `echo $sup_libs|grep -c ld-2.2.4.so` != 0  ];then

		sup_libs="$sup_libs /lib/ld-linux.so.2"

    fi

    if [ `echo $sup_libs|grep -c libdl-2.2.4.so` != 0  ];then

		sup_libs="$sup_libs|grep /lib/libdl.so.2"

    fi

    if [ `echo $sup_libs|grep -c libcom_err.so.2.0` != 0 ];then 

		sup_libs="$sup_libs /lib/libcom_err.so.2"

    fi

    if [ `echo $sup_libs|grep -c libnss_files-2.2.4.so` != 0 ];then

		sup_libs="$sup_libs /lib/libnss_files.so.2"

    fi

    if [ `echo $sup_libs|grep -c libresolv-2.2.4.so` != 0 ];then

		sup_libs="$sup_libs /lib/libresolv.so.2"

    fi

    if [ `echo $sup_libs|grep -c libtermcap.so.2.0.8` != 0 ];then

		sup_libs="$sup_libs /lib/libtermcap.so.2"

    fi

    if [ `echo $sup_libs|grep -c libuuid.so.1.2` != 0 ];then
 
		sup_libs="$sup_libs /lib/libuuid.so.1"

    fi

    if [ `echo $sup_libs|grep -c libext2fs.so.2.4` != 0 ];then

		sup_libs="$sup_libs /lib/libext2fs.so.2"

    fi

    if [ `echo $sup_libs|grep -c libe2p.so.2.3` != 0  ];then

		sup_libs="$sup_libs /lib/libe2p.so.2"

    fi

    if [ `echo $sup_libs|grep -c libreadline.so.4.1` != 0 ];then

	for  libreadline in libreadline.so libreadline.so.3 libreadline.so.4
	do

	    sup_libs="$sup_libs /usr/lib/$libreadline"

	done

    fi

    if [ `echo $sup_libs|grep -c libncurses.so.5.2` != 0 ];then

	    sup_libs="$sup_libs /usr/lib/libncurses.so.5 /usr/lib/libncurses.so"

    fi

    jp_verb_echo "含まれる共有ライブラリは、以下のとおりです。"
    eng_verb_echo "supported libraries are"
    jp_verb_echo $sup_libs
    eng_verb_echo $sup_libs

#dependency check starts.


    for ldd_test in $require
    do

	if [ -d $ldd_test ];then
	    continue
	fi

	required_libs=`ldd_lib_out $ldd_test`

	if [ "$required_libs" != not_elf ];then

	    for lib_exist in $required_libs
	    do
		if [ `echo $sup_libs|grep -c $lib_exist` != 0 ];then
		    continue
		fi

		if [ "$no_686" = 1 ];then

		    l_686_low=${lib_exist#*/i686/}
		    l_686_up=${lib_exist%/i686/*}

		    if [ $l_686_low != $l_686 ];then

			no_l_686=$l_686_up/$l_686_low
			if [ -f $no_l_686 ]||[ -d $no_l_686 ];then
			    continue
			fi
		    fi
		    
		fi
		jp_echo "$ldd_testは、${lib_exist}を必要としています。"
		eng_echo "$ldd_test requires ${lib_exist}."
		def_libs_val=1

	    done

	fi

    done

#dependency check ends.


    if [ "$def_libs_val" = "" ]&&[ "$depend" = 1 ];then

	echo
	jp_echo "共有ライブラリに依存性の問題は見つかりませんでした。"
	eng_echo "shared librariy has no problem."
    
	if [ "$c_size_val" != 1 ]&&[ "$l_size_val" != 1 ]&&[ "$t_size_val" != 1 ];then

	    jp_echo "ライブラリの検証は終了しました。"
	    eng_echo "veryfication of library ended."
	    exit 0

	fi

    fi



    if [ "$def_libs_val" = 1 ];then

	if [ "$depend" = 1 ];then

	    echo
	    jp_echo "ライブラリの検証は終了しました。"
	    eng_echo "veryfication of library finished."
		
	    if [ "$c_size_val" != 1 ]&&[ "$l_size_val" != 1 ]&&[ "$t_size_val" != 1 ];then
	    
		jp_echo "ライブラリの検証は終了しました。作業を完了します。"
		eng_echo "veryfication of library ended,the operation finished."
		exit 0

	    fi

	fi


	if [ "$c_size_val" != 1 ]&&[ "$l_size_val" != 1 ]&&[ "$t_size_val" != 1 ];then

	    jp_echo "共有ライブラリの依存性に問題があります。続行しますか？(Y or N)"
	    eng_echo "Do you continue?(Y or N)"

	    read dummy

	    case "$dummy" in

		"Y" | "y" ) jp_echo "作業を再開します。"
			    eng_echo "the operation resumed."
		;;
		
		"N" | "n" ) jp_echo "終了します。"
			    eng_echo "aborted."
			    exit 0
		;;

			* ) jp_echo "予期せぬ値が与えられました、終了します。"
			    eng_echo "unexpected value was set,aborted."
			    exit 1
		;;

	    esac

	fi


    fi

else

    for ldd_test in $require
    do

	if [ -d $ldd_test ];then
	    continue
	fi

	required_libs=`ldd_lib_out $ldd_test`

	if [ "$required_libs" != not_elf ];then

	    for lib_exist in $required_libs
	    do
	
		if [ `echo $new_l_list|grep -c $lib_exist` != 0 ];then
		    continue
		fi

		if [ "$no_686" = 1 ];then

		    l_686_low=${lib_exist#*/i686/}
		    l_686_up=${lib_exist%/i686/*}

		    if [ $l_686_low != $lib_exist ];then

			no_l_686=$l_686_up/$l_686_low
			if [ -f $no_l_686 ]||[ -d $no_l_686 ];then
			    continue
			fi
		    fi
		    
		fi
	
		jp_echo "$ldd_testは、${lib_exist}を必要としています。"
		eng_echo "$ldd_test requires ${lib_exist}."
		def_libs_val=1

	    done

	fi

    done

#auto detected library validation ends.

    if [ "$def_libs_val" = "" ]&&[ "$depend" = 1 ];then

	jp_echo "共有ライブラリに依存性の問題は見つかりませんでした。"
	eng_echo "shared librariy has no problem."
    
	if [ "$c_size_val" != 1 ]&&[ "$l_size_val" != 1 ]&&[ "$t_size_val" != 1 ];then

	    jp_echo "ライブラリの検証は終了しました。"
	    eng_echo "veryfication of library ended."
	    exit 0

	fi

    fi



    if [ "$def_libs_val" = 1 ];then

	if [ "$depend" = 1 ];then

	    echo
	    jp_echo "ライブラリの検証は終了しました。"
	    eng_echo "veryfication of library finished."
		
	    if [ "$c_size_val" != 1 ]&&[ "$l_size_val" != 1 ]&&[ "$t_size_val" != 1 ];then
	    
		jp_echo "ライブラリの検証は終了しました。作業を完了します。"
		eng_echo "veryfication of library ended,the operation finished."
		exit 0

	    fi

	fi


	if [ "$c_size_val" != 1 ]&&[ "$l_size_val" != 1 ]&&[ "$t_size_val" != 1 ];then

	    jp_echo "共有ライブラリの依存性に問題があります。続行しますか？(Y or N)"
	    eng_echo "Do you continue?(Y or N)"

	    read dummy

	    case "$dummy" in

		"Y" | "y" ) jp_echo "作業を再開します。"
			    eng_echo "the operation resumed."
		;;
		
		"N" | "n" ) jp_echo "終了します。"
			    eng_echo "aborted."
			    exit 0
		;;

			* ) jp_echo "予期せぬ値が与えられました、終了します。"
			    eng_echo "unexpected value was set,aborted."
			    exit 1
		;;

	    esac

	fi


    fi


fi

#calculate size. 

if [ "$t_size_val" = 1 ];then

    c_size_val=1
    l_size_val=1 

fi


if [ "$c_size_val" = 1 ];then 

    c_size=0
    c_a_size=0
    c_d_size=0

    rm -fr /tmp/bin
    mkdir /tmp/bin
    cd /tmp/bin

    for created_com in $default_commands
    do

	    if [ -L "$created_com" ];then

		link_com_name=`echo $created_com|awk 'BEGIN{FS="/"}{print $NF}'`
		true_com_name=`readlink $created_com`
		
		if [ `echo $true_com_name|grep -c ^/` != 0 ];then

		    ap_true_com_name=$true_com_name

		else

		    p_tmp_com=`echo $created_com|awk 'BEGIN{RS="/"}{print}'|grep -v $link_com_name`
		    link_com_dir=/`echo $p_tmp_com|sed 's/ /\//g'`
		    ap_true_com_name=${link_com_dir}/${true_com_name}

		fi

		if [ ! -f $true_com_name ];then

		    cp $ap_true_com_name . || { jp_echo "$created_comのコピーに失敗しました。"
						jp_echo "終了します。"
						eng_echo "copying $created_com failed."
						eng_echo "aborted."
						exit 1
						}

		fi
		
		    ln -s $true_com_name $link_com_name

	    else

		cp $created_com . ||  { jp_echo "$created_comのコピーに失敗しました。"
					jp_echo "終了します。"
					eng_echo "copying $created_com failed."
					eng_echo "aborted."
					exit 1
					}

	    fi

    done

    strip `ls .` 2>/dev/null

    jp_verb_echo
    eng_verb_echo

    for p_command in `ls .`
    do

	c_size=$(($c_size+`ls -l $p_command|awk '{print $5}'`))
	jp_verb_echo "$p_commandの大きさは`ls -l $p_command|awk '{print $5}'`B、総容量は${c_size}Bです。"
	eng_verb_echo "size of $p_command is `ls -l $p_command|awk '{print $5}'`B,total is ${c_size}B." 

    done

    rm -f /tmp/bin/*

    if [ "$c_del" ];then

	for deleted_com in $c_del
	do

	    ap_command=`echo $default_commands|awk 'BEGIN{RS=" "}{print}'|grep $deleted_com\$` 

	    if [ -L "$ap_command" ];then

		link_com_name=`echo $ap_command|awk 'BEGIN{FS="/"}{print $NF}'`
		true_com_name=`readlink $ap_command`
		
		if [ `echo $true_com_name|grep -c ^/` != 0 ];then

		    ap_true_com_name=$true_com_name

		else

		    p_tmp_com=`echo $ap_command|awk 'BEGIN{RS="/"}{print}'|grep -v $link_com_name`
		    link_com_dir=/`echo $p_tmp_com|sed 's/ /\//g'`
		    ap_true_com_name=${link_com_dir}/${true_com_name}

		fi

		if [ ! -f $true_com_name ];then

		    cp $ap_true_com_name . || { jp_echo "$deleted_comのテストコピーに失敗しました。"
						jp_echo "終了します。"
						eng_echo "test copy of $deleted_com failed."
						eng_echo "aborted."
						exit 1
						}

		fi
		
		ln -s $true_com_name $link_com_name

	    else

	    cp $ap_command . ||{ jp_echo "$deleted_comのテストコピーに失敗しました。"
				 jp_echo "終了します。"
				 eng_echo "copying $deleted_com failed."
				 eng_echo "aborted."
				 exit 1
				}

	    fi

	done

	strip `ls .` >/dev/null
	echo

	for p_d_command in $c_del
	do

	    c_d_size=$(($c_d_size+`ls -l $p_d_command|awk '{print $5}'`))

	    jp_verb_echo "削除するコマンド、$p_d_commandの大きさは`ls -l $p_d_command|awk '{print $5}'`Bです。"
	    eng_verb_echo "size of $p_d_command ,deleted command,is `ls -l $p_d_command|awk '{print $5}'`B."

	done

	rm -f /tmp/bin/*

    fi #$c_del

    if [ "$c_add" ];then
    echo
	for created_com in $c_add
	do 

	    if [ -L "$created_com" ];then

		link_com_name=`echo $created_com|awk 'BEGIN{FS="/"}{print $NF}'`
		true_com_name=`readlink $created_com`

		if [ `echo $true_com_name|grep -c ^/` != 0 ];then

		    if [  -L "$true_com_name" ];then
			
			while [ -L "$true_com_name" ]
			do

			    true_com_name=`readlink $true_com_name`

				if [ `echo $true_com_name|grep -c ^/` != 0 ];then

				    true_com_file_name=`echo $true_com_name|awk 'BEGIN{FS="/"}{print $NF}'`
				    p_tmp_com=`echo $true_com_name|awk 'BEGIN{RS="/"}{print}'|grep -v $link_com_name`
				    link_com_dir=/`echo $p_tmp_com|sed 's/ /\//g'`
				    true_com_name=${link_com_dir}/${true_com_file_name}

				fi

	
			done

			    ap_true_com_name=$true_com_name
			    true_com_name=$true_com_file_name

		    else

			ap_true_com_name=$true_com_name

		    fi

		else

		    p_tmp_com=`echo $created_com|awk 'BEGIN{RS="/"}{print}'|grep -v $link_com_name`
		    link_com_dir=/`echo $p_tmp_com|sed 's/ /\//g'`
		    ap_true_com_name=${link_com_dir}/${true_com_name}

		fi

		if [ ! -f $true_com_name ];then

		    cp $ap_true_com_name .
		fi
		
		    ln -s $true_com_name $link_com_name

	    else

		cp $created_com .

	    fi

	done
	#strip
	strip `ls .` 2>/dev/null

	for p_a_command in `ls .`
	do

	c_a_size=$(($c_a_size+`ls -l $p_a_command|awk '{print $5}'`))
	jp_verb_echo "加えられるコマンド、$p_a_commandの大きさは`ls -l $p_a_command|awk '{print $5}'`Bです。"
	eng_verb_echo "size of $p_a_command,added command, is `ls -l $p_a_command|awk '{print $5}'`B." 

	done

	rm -f /tmp/bin/*

    fi

    cd $mkrd_pwd
    rm -fr /tmp/bin/

    t_c_size=$(($c_size-$c_d_size+$c_a_size))

    echo
    jp_echo "デフォルトのコマンドの総容量は${c_size}Bです。"
    jp_echo "削除するコマンドの総容量は${c_d_size}Bです。"
    jp_echo "加えるコマンドの総容量は${c_a_size}Bです。"

    eng_echo "default commands size is ${c_size}B."
    eng_echo "deleted size is ${c_d_size}B."
    eng_echo "added size is ${c_a_size}B."
    echo 

    jp_echo "最終的なコマンドの総容量は${t_c_size}Bです。"
    eng_echo "total command size is ${t_c_size}B."
 
    if [ "$l_size_val" != 1 ]&&[ "$t_size_val" != 1 ];then
	exit 0
    fi

fi

if [ "$l_size_val" = 1 ];then


    if [ "$auto_dyn_lib" != 1 ];then

	l_size=0
	l_a_size=0
	l_d_size=0

	rm -fr /tmp/lib
	mkdir /tmp/lib
	cd /tmp/lib

	for p_lib in $default_libs
	do
	    cp -f $p_lib /tmp/lib ||{ jp_echo "$p_libのコピーに失敗しました。"
				      jp_echo "終了します。"
				      eng_echo "copying $p_lib failed."
				      eng_echo "aborted."
				      rm -rf /tmp/lib
				      exit 1
				     } 
	done

	strip `ls .`  2>/dev/null

	for p_lib in `ls .`
	do

	    l_size=$(($l_size+`ls -l $p_lib|awk '{print $5}'`))
	    jp_verb_echo "$p_libの大きさは`ls -l $p_lib|awk '{print $5}'`B、総容量は${l_size}Bです。"
	    eng_verb_echo "size of $p_lib is `ls -l $p_lib|awk '{print $5}'`B,total is ${l_size}B." 

	done

	rm -f /tmp/lib/*

	if [ "$l_del" ];then
	echo
	    for p_d_lib in $l_del
	    do

		ap_lib=`echo $default_libs|awk 'BEGIN{RS=" "}{print}'|grep $p_d_lib\$`
		cp -f $ap_lib /tmp/lib/ ||{ jp_echo "$p_d_libのコピーに失敗しました。"
					    jp_echo "終了します。"
					    eng_echo "copying $p_d_lib failed."
					    eng_echo "aborted."
					    rm -rf /tmp/lib
					    exit 1
					    }

	    done	

	    strip `ls .` 2>/dev/null

	    for p_d_lib in `ls .`
	    do

	    l_d_size=$(($l_d_size+`ls -l $p_d_lib|awk '{print $5}'`))
	    jp_verb_echo "削除されるライブラリ、$p_d_libの大きさは`ls -l $p_d_lib|awk '{print $5}'`Bです。"
	    eng_verb_echo "size of ${p_d_lib},deleted library, is `ls -l $p_d_lib|awk '{print $5}'`B." 

	    done

	    rm -f /tmp/lib/*

	fi

	if [ "$l_add" ];then 
	echo
	    for p_a_lib in $l_add
	    do
		  cp -r $p_a_lib /tmp/lib/ ||{ jp_echo "$p_a_libのコピーに失敗しました。"
					    jp_echo "終了します。"
					    eng_echo "copying $p_a_lib failed."
					    eng_echo "aborted."
					    rm -rf /tmp/lib
					    exit 1
					    }

	    done

	    strip /tmp/lib/*  2>/dev/null

	    for p_a_lib in `ls .`
	    do

		l_a_size=$(($l_a_size+`ls -l $p_a_lib|awk '{print $5}'`))
	jp_verb_echo "加えられるライブラリ、$p_a_libの大きさは`ls -l $p_a_lib|awk '{print $5}'`Bです。"
	eng_verb_echo "size of $p_a_lib,added library, is `ls -l $p_a_lib|awk '{print $5}'`B." 
		
	    done

	    rm -f /tmp/lib/*

	fi

	    cd $mkrd_pwd
	    rm -fr /tmp/lib

	t_l_size=$(($l_size-$l_d_size+$l_a_size))

	echo
	jp_echo "デフォルトのライブラリの総容量は${l_size}Bです。"
	jp_echo "削除されるライブラリの総容量は${l_d_size}Bです。"
	jp_echo "加えられるライブラリの総容量は${l_a_size}Bです。"
	eng_echo "default libraries size is ${l_size}B."
	eng_echo "deleted size is ${l_d_size}B."
	eng_echo "added size is ${l_a_size}B."
	echo
	jp_echo "最終的なライブラリの総容量は${t_l_size}Bです。"
	eng_echo "total libraries size is ${t_l_size}B."
 
	if [ "$t_size_val" != 1 ];then
	exit 0
	fi


	if [ "$t_size_val" = 1 ];then

	    echo
	    jp_echo "コマンドおよびライブラリの総容量は $(($t_c_size+$t_l_size))Bです。"
	    eng_echo "total size of commands and libraries is $(($t_c_size+$t_l_size))B."

	    if [ $(($t_c_size+$t_l_size)) -gt "$rd_img_size" ];then

jp_echo "コマンドおよびライブラリの総容量がルートファイルシステムの最大値${rd_img_size}Bを越えています。"
eng_echo "the amount of commands and libraries is over the capacity of root file system,${rd_img_size}B."

	    echo
	    exit 0

	    fi

	fi

    else
    #size calculation for auto detected library. auto_dyn_lib=1
	rm -rf /tmp/lib
	mkdir /tmp/lib
	
	cd /tmp/lib
	echo

	ll_libs_size=0
	ll_libs_d_size=0
	ll_libs_a_size=0

	for created_lib in $df_ad_l_list
	do


	  if [ -d "$created_lib" ];then
		
		    lib_c_dir=`echo $created_lib|awk 'BEGIN{FS="/"}{print $NF}'`
		    rm -fr $lib_c_dir
		    mkdir $lib_c_dir
		    cd $lib_c_dir
		    cp -a $created_lib/* .
		    cd ..
 

	    elif [ -L "$created_lib" ];then

		link_lib_name=`echo $created_lib|awk 'BEGIN{FS="/"}{print $NF}'`
		true_lib_name=`readlink $created_lib`
		
		if [ `echo $true_lib_name|grep -c ^/` != 0 ];then

		    ap_true_lib_name=$true_lib_name

		else

		    p_tmp_lib=`echo $created_lib|awk 'BEGIN{RS="/"}{print}'|grep -v $link_lib_name`
		    link_lib_dir=/`echo $p_tmp_lib|sed 's/ /\//g'`
		    ap_true_lib_name=${link_lib_dir}/${true_lib_name}

		fi

		if [ ! -f $true_lib_name ];then

		    cp -r $ap_true_lib_name .

		fi
		
		    ln -s $true_lib_name $link_lib_name

	    else

		cp -r $created_lib .

	    fi

	done

	strip `ls .` 2>/dev/null
    
	for test_lib in *
	  do

	  if [ -d $test_lib ];then
	      cd $test_lib

	      for dir_test_lib in *
		do

		test_lib_size=`ls -l $dir_test_lib|awk '{print $5}'`
		ll_libs_size=$(($test_lib_size+$ll_libs_size))
		jp_verb_echo "$dir_test_libの大きさは${test_lib_size}B、総容量は${ll_libs_size}Bです。"
		eng_verb_echo "$dir_test_lib is ${test_lib_size}B,the amout of library is ${ll_libs_size}B."

	      done

	      cd ..
	      continue
	  fi
	  
	  test_lib_size=`ls -l $test_lib|awk '{print $5}'`
	  ll_libs_size=$(($test_lib_size+$ll_libs_size))
	  jp_verb_echo "$test_libの大きさは${test_lib_size}B、総容量は${ll_libs_size}Bです。"
	  eng_verb_echo "$test_lib is ${test_lib_size}B,the amout of library is ${ll_libs_size}B."
	  
	done

	    jp_verb_echo "自動判定によるライブラリの総容量は${ll_libs_size}Bです。"
	    eng_verb_echo "the amount of auto detected library is ${ll_libs_size}B."
	    
	    rm -rf ./*

	    if [ "$l_del" ];then
		echo
		for ad_d_lib in $l_del
		  do
		  ap_d_lib=`echo $df_ad_l_list|awk 'BEGIN{RS=" "}{print}'|grep -w $ad_d_lib` 
		  ad_d_libs_list="$ad_d_libs_list $ap_d_lib"

		done

		for created_lib in $ad_d_libs_list
		  do 
		  if [ -L "$created_lib" ];then
		      

		      link_lib_name=`echo $created_lib|awk 'BEGIN{FS="/"}{print $NF}'`
		      true_lib_name=`readlink $created_lib`
		
		      if [ `echo $true_lib_name|grep -c ^/` != 0 ];then

			  ap_true_lib_name=$true_lib_name

		      else

			  p_tmp_lib=`echo $created_lib|awk 'BEGIN{RS="/"}{print}'|grep -v $link_lib_name`
			  link_lib_dir=/`echo $p_tmp_lib|sed 's/ /\//g'`
			  ap_true_lib_name=${link_lib_dir}/${true_lib_name}
			
		      fi

		      if [ ! -f $true_lib_name ];then

			cp $ap_true_lib_name .
			
		      fi
		    
		      ln -s $true_lib_name $link_lib_name

		  else

		      cp $created_lib .

		  fi

		done
		
		strip `ls .` 2>/dev/null

		for test_lib in *
		  do
		  test_lib_size=`ls -l $test_lib|awk '{print $5}'`
		  ll_libs_d_size=$(($test_lib_size+$ll_libs_d_size))
		  jp_verb_echo "削除される$test_libの大きさは${test_lib_size}B、総容量は${ll_libs_d_size}Bです。"
		  eng_verb_echo "deleted $test_lib is ${test_lib_size}B,the amout of library is ${ll_libs_d_size}B."

		done

		jp_verb_echo "削除されるライブラリの総容量は${ll_libs_d_size}Bです。"
		eng_verb_echo "the amount of deletedlibrary is ${ll_libs_d_size}B."
		rm -rf ./*

	    fi #for auto detection l_del ends.
	    
	    if [ "$l_add" ];then
		echo
		for created_lib in $l_add
		  do
		  
		  if [ -d "$created_lib" ];then
		
		    lib_c_dir=`echo $created_lib|awk 'BEGIN{FS="/"}{print $NF}'`
		    mkdir $lib_c_dir
		    cd $lib_c_dir
		    cp -a $created_lib/* .
		    cd ..

		  elif [ -L "$created_lib" ];then

		    link_lib_name=`echo $created_lib|awk 'BEGIN{FS="/"}{print $NF}'`
		    true_lib_name=`readlink $created_lib`
		
		    if [ `echo $true_lib_name|grep -c ^/` != 0 ];then

			ap_true_lib_name=$true_lib_name

		    else

			p_tmp_lib=`echo $created_lib|awk 'BEGIN{RS="/"}{print}'|grep -v $link_lib_name`
			link_lib_dir=/`echo $p_tmp_lib|sed 's/ /\//g'`
			ap_true_lib_name=${link_lib_dir}/${true_lib_name}

		    fi

		    if [ ! -f $true_lib_name ];then

			cp -r $ap_true_lib_name .

		    fi
	    
		    ln -s $true_lib_name $link_lib_name

		  else

		      cp -r $created_lib .
		      
		  fi
		    
		done
		
		for file_name in `ls .`
		  do
		  if [ -d $file_name ];then
		      
		      cd $file_name

		      strip *
		      
		      cd ..

		  fi

		  strip $file_name 2>/dev/null

		done

	    
		for test_lib in *
		  do
		  
		  if [ -d $test_lib ];then
		      cd $test_lib

		      for dir_test_lib in *
			do

			test_lib_size=`ls -l $dir_test_lib|awk '{print $5}'`
			ll_libs_a_size=$(($test_lib_size+$ll_libs_a_size))
			jp_verb_echo "加えられる$dir_test_libの大きさは${test_lib_size}B、総容量は${ll_libs_a_size}Bです。"
			eng_verb_echo "added $dir_test_lib is ${test_lib_size}B,the amout of library is ${ll_libs_a_size}B."

		      done

		      cd ..
		      continue
		  fi

		  test_lib_size=`ls -l $test_lib|awk '{print $5}'`
		  ll_libs_a_size=$(($test_lib_size+$ll_libs_a_size))
		  jp_verb_echo "加えられる$test_libの大きさは${test_lib_size}B、総容量は${ll_libs_a_size}Bです。"
		  eng_verb_echo "added $test_lib is ${test_lib_size}B,the amout of library is ${ll_libs_a_size}B."
		  
		done

		jp_verb_echo " 加えられるライブラリの総容量は${ll_libs_a_size}Bです。"
		eng_verb_echo "the amount of  added library is ${ll_libs_a_size}B."
	    
		rm -rf ./*   

	    fi

	    cd $mkrd_pwd
	    rm -rf /tmp/lib
	    echo
	    jp_echo "自動判定によるライブラリの総容量は${ll_libs_size}Bです。"
	    eng_echo "the amout of auto detected library is ${ll_libs_size}B."
	    jp_echo "削除されるライブラリの総容量は${ll_libs_d_size}Bです。"
	    eng_echo "the amount of deletedlibrary is ${ll_libs_d_size}B."
	    jp_echo " 加えられるライブラリの総容量は${ll_libs_a_size}Bです。"
	    eng_echo "the amount of  added library is ${ll_libs_a_size}B."

	    ll_libs_SIZE=$(($ll_libs_size+$ll_libs_a_size-$ll_libs_d_size))
	    
	    if [ -d /tmp/secutity ];then
		rm -fr /tmp/security
	    fi

	    echo
	    jp_echo " 最終的なライブラリの総容量は${ll_libs_SIZE}Bです。"
	    eng_echo "the amount of  added library is ${ll_libs_SIZE}B."


	    if [ "$t_size_val" = 1 ];then

		echo
		jp_echo "コマンドおよびライブラリの総容量は $(($t_c_size+$ll_libs_SIZE))Bです。"
		eng_echo "total size of commands and libraries is $(($t_c_size+$ll_libs_SIZE))B."
		echo
		exit 0
		
	    fi

	    exit 0

    fi
    
fi


cd $mkrd_pwd

#####################
##calculation ends.##
#####################

####################################################################################################
### operation starts ###############################################################################
####################################################################################################
####################################################################################################

rd_root=rd_root.$$.dir
ap_rd_root=`pwd`/$rd_root
    

if [ `mount |grep $ap_rd_root|cut -d" " -f1` ];then
   
    umount rd_root/||{ jp_echo "umountに失敗しました。作業を終了します。"
	eng_echo "umount failed.The operation was aborted."
	exit 1
    }
fi
rd_img=rd_root.$$.img

rm -f $rd_img
dd if=/dev/zero of=$rd_img  bs=512 count=$rd_count >/dev/null 2>&1 
mke2fs -m 0 -F $rd_img >/dev/null 2>&1

rm -rf $rd_root
mkdir $rd_root
mount $rd_img $rd_root -o loop 


#####################################################################################
#####################################################################################
#########                              create /.                           ##########
#####################################################################################
#####################################################################################
db_echo start create /.

rd_root=$ap_rd_root
cd $rd_root

mkdir .dev.static
mkdir boot
mkdir bin
mkdir sbin
mkdir dev
mkdir etc
mkdir -p etc/sysconfig/modules
mkdir root
mkdir -p home/user1
chown 501:501 home/user1

if [ "$jail_val" = 1 ];then

    mkdir home/jail{1,2,3}
    chown 1001:1000 home/jail1
    chown 1002:1000 home/jail2
    chown 1003:1000 home/jail3

fi

mkdir initrd
mkdir lib
mkdir lib/modules
mkdir mnt
mkdir proc
mkdir -p var/run/console
mkdir -p var/lock/subsys
chmod 775 var/lock/subsys
mkdir var/log
mkdir tmp
chmod 1777 tmp
cd var
ln -s ../tmp .
cd ..
mkdir usr
mkdir usr/lib
mkdir sys
mkdir -p usr/lib/kbd/keymaps/
mkdir usr/bin
mkdir usr/sbin
mkdir -p usr/share/terminfo/{c,k,l,v,x}

if [  "$def_res" != 1 ];then
    cp -a /usr/lib/kbd/keymaps/i386 usr/lib/kbd/keymaps/
fi
cp /usr/share/terminfo/c/cygwin usr/share/terminfo/c
cp /usr/share/terminfo/k/kterm usr/share/terminfo/k
cp /usr/share/terminfo/l/linux usr/share/terminfo/l
cp /usr/share/terminfo/v/vt100 usr/share/terminfo/v
cp /usr/share/terminfo/x/xterm usr/share/terminfo/x

#create /bin
#cd bin

#echo create root
#echo  $new_c_list

for created_com in $new_c_list ;do

    if [ -L "$created_com" ];then

	link_com_name=`basename $created_com`
	true_com_name=`readlink $created_com`

	    if [ `echo $true_com_name|grep -c ^/` != 0 ];then

		if [  -L "$true_com_name" ];then
			
		    while [ -L "$true_com_name" ];do

		      true_com_name=`readlink $created_com`

		      if [ `echo $true_com_name|grep -c ^/` != 0 ];then

			  true_com_file_name=`echo $true_com_name|awk 'BEGIN{FS="/"}{print $NF}'`
			  p_tmp_com=`echo $true_com_name|awk 'BEGIN{RS="/"}{print}'|grep -v $link_com_name`
			  link_com_dir=/`echo $p_tmp_com|sed 's/ /\//g'`
			  true_com_name=${link_com_dir}/${true_com_file_name}

		      fi

		    done

		    ap_true_com_name=$true_com_name
		    true_com_name=$true_com_file_name

		else
		    ap_true_com_name=$true_com_name
		fi

	    else
		ap_true_com_name=`dirname $created_com`/${true_com_name}
#echo 	ap_true_com_name=$ap_true_com_name
	    fi

	    if [ ! -f $rd_root$ap_true_com_name ];then

		if [ ! -d `dirname $rd_root$ap_true_com_name` ];then
		    mkdir -p `dirname $rd_root$ap_true_com_name`
		fi
		cp $ap_true_com_name $rd_root$ap_true_com_name || {  jp_echo "$created_comのコピーに失敗しました。"
					    jp_echo "この作業は終了しました。"
					    eng_echo "Copy of $binary was failed in creating root file system."
					    eng_echo "This operation was aborted."
					    rd_exit 1 $LINENO
					    }

	    fi

#$created_comif [ `dirname $created_com` = `dirname $ap_true_com_name` ];then

	    if [ `dirname $created_com` = `dirname $ap_true_com_name` ];then
		cd $rd_root`dirname $created_com`
		ln -s `basename $true_com_name` $link_com_name || rd_exit 1 $LINENO "ln -s `basename $true_com_name` $link_com_name"
	    	cd $rd_root

	    else
		cd $rd_root`dirname $created_com`
		ln -s $ap_true_com_name $link_com_name || rd_exit 1 $LINENO "ln -s $ap_true_com_name $link_com_name"
		cd $rd_root
		
	    fi
	    
    else

	if [ ! -d `dirname $rd_root$created_com` ];then
	    mkdir -p `dirname $rd_root$created_com`
	fi

	cp $created_com $rd_root$created_com || { jp_echo "$created_comのコピーに失敗しました。"
				  jp_echo "この作業は終了しました。"
				   eng_echo "Copy of $binary was failed in creating root file system."
				   eng_echo "This operation was aborted."
				   rd_exit 1 $LINENO
				   }

    fi

done

cd bin
for mkrd_sh in *
do

    if [ "$mkrd_sh" = ash ];then

	continue

    fi

    if [ "`echo $mkrd_sh|grep sh\$`" ];then
		
	ln -s $mkrd_sh sh
	break

    fi

done

if [ ! -x sh ]&&[ ! -x ash ];then

    jp_echo "この$jp_rd_formには、シェルがありません。続行しますか？(Y or N)"
    eng_echo "there is no sh on this $eng_rd_form,continue?(Y or N)"

    read dummy

    case "$dummy" in

		"Y" | "y" ) jp_echo "作業を再開します。"
			    eng_echo "the operation resumed."
		;;
		
		"N" | "n" ) jp_echo "終了します。"
			    eng_echo "aborted."
			    rd_exit 0
		;;

			* ) jp_echo "予期せぬ値が与えられました、終了します。"
			    eng_echo "unexpected value was set,aborted."
			    rd_exit 1
		;;

    esac

elif [ ! -x sh ];then

    ln -fs ash sh

fi

#if [ -x insmod ];then
#
#    ln -fs insmod lsmod
#    ln -fs insmod rmmod
#    ln -fs insmod modprobe
#
#fi

if [ -x swapon ]&&[ ! -x swapoff ];then
    ln -fs swapon swapoff
fi

if [ -x gzip ]&&[ ! -x gunzip ];then
    ln -f gzip gunzip
fi

strip `ls .` 2>/dev/null

cd ..

### now pwd is $rd_root
#library
#new_l_list=`echo $new_l_list |awk 'BEGIN{RS=" "}{print}'|sort -u|uniq`

if [ "$auto_dyn_lib" = 1 ];then

    tlr=/tmp/rd_root.$$.dir
    rm -rf $tlr
    mkdir -p $tlr/usr/lib
    mkdir $tlr/lib
    cd $tlr

    for created_lib in $new_l_list ;do

	if [ -L "$created_lib" ];then
#echo created_lib=$created_lib

	    link_lib_name=${created_lib##*/}
	    created_lib_dir=${created_lib%/*}

	    true_lib_name=`readlink $created_lib`
	    true_lib_dir=`dirname $true_lib_name`

	    if [ `echo $true_lib_name|grep -c ^/` != 0 ];then
		ap_true_lib_name=$true_lib_name
	    else
		p_tmp_lib=`echo $created_lib|awk 'BEGIN{RS="/"}{print}'|grep -v $link_lib_name`
		link_lib_dir=$created_lib_dir

		if [ "${true_lib_dir}" = "." ];then
		    link_lib_dir=$link_lib_dir
		elif [ `echo $true_lib_name|grep -c ^..` = 1 ];then

		    for tt in $p_tmp_lib
		    do
			if [ $tt = ".." ];then
			    link_lib_dir=`dirname $link_lib_dir`	
			fi
		    done
		    link_lib_dir=${link_lib_dir}/${true_lib_dir##*../}
		
		elif [ `echo $true_lib_name|grep -c ^./` = 1 ];then
		    link_lib_dir=${link_lib_dir}/${true_lib_dir##*./}
		fi
		ap_true_lib_name=${link_lib_dir%/}/${true_lib_name}

	    fi

	    ap_true_lib_dir=${ap_true_lib_name%/*}
	    if [ ! -f $true_lib_name ];then

		if [ ! -d $tlr$ap_true_lib_dir ];then
		    mkdir -p $tlr$ap_true_lib_dir
		fi
		cp -a $ap_true_lib_name $tlr$ap_true_lib_name || rd_exit 1 $LINENO "cp $ap_true_lib_name"
	    fi

	    if [ $created_lib_dir = $ap_true_lib_dir ];then
		cd $tlr$created_lib_dir
		ln -s ${ap_true_lib_name##*/} $link_lib_name
		cd $tlr

	    else
		cd $tlr$created_lib_dir
		ln -s $ap_true_lib_name $link_lib_name
		cd $tlr
	    fi

	elif [ $created_lib = /tmp/security ]&&[ -d $created_lib ];then
	    mv /tmp/security $tlr/lib || rd_exit 1 $LINENO

	elif [ -d $created_lib ];then

	    lib_c_dir=${created_lib_dir}
	    if [ ! -d $tlr$lib_c_dir ];then
		mkdir -p $tlr$lib_c_dir
	    fi

	    cd $tlr$lib_c_dir
	    cp -a $created_lib . || rd_exit 1 $LINENO
	    strip $tlr$lib_c_dir/* >/dev/null 2>&1
	    cd $tlr
	else
	    lib_c_dir=${created_lib%/*}
	    if [ ! -d $tlr$lib_c_dir ];then
		mkdir -p $tlr$lib_c_dir
	    fi
	    cp -a $created_lib $tlr$created_lib || rd_exit 1 $LINENO "cp $created_lib" 
	    strip $tlr$created_lib >/dev/null 2>&1

	fi

    done
    cd $mkrd_pwd
    cd $rd_root
	
    for ll in $tlr/*
    do

	  for lll in `find $ll -type f`
	  do
	    strip $lll
	  done

	  cp -a $ll $rd_root ||{ jp_echo "ライブラリのコピーに失敗しました。"
				 jp_echo "終了します。"
			   	 eng_echo "copying library failed."
				 eng_echo "aborted."
				 rd_exit 1 $LINENO
				}
    done

    rm -fr $tlr

else
#### MAYBE OBSOLETE ####
    if [ -d /tmp/rd_lib ]||[ -f /tmp/rd_lib ];then

	rm -fr /tmp/rd_lib

    fi


    mkdir /tmp/rd_lib

    for cp_lib in $new_l_list
      do

      cp $cp_lib /tmp/rd_lib/ ||{ jp_echo "$cp_libのコピーに失敗しました。"
	  jp_echo "終了します。"
	  eng_echo "copying $cp_lib failed."
	  eng_echo "aborted."
	  rd_exit 1 $LINENO
      }

    done

    strip /tmp/rd_lib/* 2>/dev/null
    cp /tmp/rd_lib/* lib

    cd lib

    if [ -f libc-2.2.4.so ];then
	
	ln -is libc-2.2.4.so libc.so.6
	
    fi

    if [ -f ld-2.2.4.so ];then
	
	ln -is ld-2.2.4.so ld-linux.so.2

    fi
    
    if [ -f libdl-2.2.4.so ];then

	ln -is libdl-2.2.4.so libdl.so.2
	
    fi
	    
    if [ -f libcom_err.so.2.0 ];then 

	ln -is libcom_err.so.2.0 libcom_err.so.2
	
    fi
    
    if [ -f libnss_files-2.2.4.so ];then

	ln -is libnss_files-2.2.4.so libnss_files.so.2
	
    fi

    if [ -f libresolv-2.2.4.so ];then

	ln -is libresolv-2.2.4.so libresolv.so.2
	
    fi

    if [ -f libtermcap.so.2.0.8 ];then

	ln -is libtermcap.so.2.0.8 libtermcap.so.2

    fi

    if [ -f libuuid.so.1.2 ];then
 
	ln -is libuuid.so.1.2 libuuid.so.1
	
    fi

    if [ -f libext2fs.so.2.4 ];then

	ln -is libext2fs.so.2.4 libext2fs.so.2

    fi

    if [ -f libe2p.so.2.3 ];then
	
	ln -is libe2p.so.2.3 libe2p.so.2

    fi

    if [ -f libreadline.so.4.1 ];then
		    
	ln -is libreadline.so.4.1 libreadline.so
	ln -is libreadline.so.4.1 libreadline.so.3
	ln -is libreadline.so.4.1 libreadline.so.4

    fi

    if [ -f libncurses.so.5.2 ];then

	ln -is libncurses.so.5.2 libncurses.so.5
	ln -is libncurses.so.5.2 libncurses.so

    fi

    cd ..
    
    rm -fr /tmp/rd_lib

fi

#device files
cp -a /dev/console dev
cp -a /dev/null dev
mkdir dev/pts
mkdir dev/shm

#cp -a /dev/initctl dev
if [ "$ok_udev" != 1 ];then

    if [ -b /.dev.static/sdd10 ];then
	dev_dir=/.dev.static
    else
	dev_dir=/dev
    fi 
    cp -a $dev_dir/fd dev
    cp -a $dev_dir/fd[01] dev
    cp -a $dev_dir/fd[01]D720 dev
    cp -a $dev_dir/fd[01]H1440 dev
    cp -a $dev_dir/fd[01]u2880 dev
    cp -a $dev_dir/initctl dev
    cp -a $dev_dir/hd[abcd]* dev
    cp -a $dev_dir/systty dev

    cp -a $dev_dir/tty dev
    cp -a $dev_dir/tty[0-7] dev
    cp -a $dev_dir/ttyS[0-3] dev
    cp -a $dev_dir/ttyp[0-7] dev
    cp -a $dev_dir/pty[pqrs][0-9] dev
    cp -a $dev_dir/pty[pqrs][a-f] dev
    cp -a $dev_dir/lp[0-2] dev
    cp -a $dev_dir/log dev
    cp -a $dev_dir/loop[0-7] dev
    cp -a $dev_dir/dsp dev
    cp -a $dev_dir/mem dev
    cp -a $dev_dir/net/tun dev
    cp -a $dev_dir/zero dev

    cp -a $dev_dir/ram* dev
    cp -a $dev_dir/std* dev
    cp -a $dev_dir/scd[0-4] dev
    cp -a $dev_dir/sd[abcd] dev
    cp -a $dev_dir/sd[abcd][0-9] dev
    cp -a $dev_dir/sd[abcd]1[0-5] dev
    cp -a $dev_dir/vcs dev
    cp -a $dev_dir/vcs[0-7] dev

    cd dev
    ln -s ram0 root.old
    ln -s ram1 root
    cd ..

    chown root:root dev/tty*
    chmod 666 dev/tty*
else
    cp -a /etc/dev.d etc
    cp -a /etc/scsi_id.config etc
    cp -a /etc/sysconfig/modules/udev-stw.modules etc/sysconfig/modules/
    cp -a /etc/udev etc
    if [ ! -d lib/firmware ];then
	mkdir lib/firmware
    fi
    mkdir etc/sysconfig/network-scripts
    echo "#!/bin/sh
exit 0" >etc/sysconfig/network-scripts/net.hotplug
    chmod 755 etc/sysconfig/network-scripts/net.hotplug
fi

# echo etc files

for etc_file in /etc/services /etc/protocols /etc/nsswitch.conf
do

    if [ -f "$etc_file" ];then

	cp $etc_file etc ||{ jp_echo "ルートファイルシステム作成中、$etc_fileのコピーに失敗しました。"
			     jp_echo "終了します。"
			     eng_echo "copying $etc_file failed in creating root file system."
			     eng_echo "aborted."
			     rd_exit 1
			     }

    fi

done


####################################################
###########    echo  start ROUTER    ###############
####################################################

if [ "$router_val" = 1 ];then
    db_echo start ROUTER
    rm -rf /tmp/security

    if [ -x sbin/inetd ];then

	cd usr/sbin
	ln -s ../../sbin/inetd . || rd_exit 1 $LINENO "ln -s ../../sbin/inetd"
	cd $rd_root

    fi

    if [ -x sbin/in.telnetd ];then

	cd usr/sbin
	ln -s ../../sbin/in.telnetd . || rd_exit 1 $LINENOln "ln -s ../../sbin/in.telnetd"
	cd $rd_root

    fi

    if [ -x sbin/tcpd ];then

	cd usr/sbin
	ln -s ../../sbin/tcpd . || rd_exit 1 $LINENO "ln -s ../../sbin/tcpd"
	cd $rd_root

    fi

    if [ -x sbin/iptables ];then
	cp -a /lib/iptables lib
    fi

##############################
## echo  Creat init system. ##
## pwd                      ##
## echo  Creat init system. ##
##############################

    mkdir -p etc/rc.d/init.d || rd_exit 1 $LINENO mkdir
    mkdir etc/rc.d/rc{0,1,2,3,4,5,6}.d || rd_exit 1 $LINENO mkdir

    cd etc
    ln -s rc.d/init.d . || rd_exit 1 $LINENO ln
    ln -s rc.d/rc{0,1,2,3,4,5,6}.d . || rd_exit 1 $LINENO ln
    cd ..


    if [ -x sbin/init ];then

	touch var/log/wtmp || rd_exit 1 $LINENO "touch var/log/wtmp"
	touch var/run/utmp || rd_exit 1 $LINENO "touch var/run/utmp"

    fi

### create rc.local ###

    ed >/dev/null 2>&1 <<RC_LOCAL
a
#!/bin/sh
#For ROUTER.
#create issue
echo " " >/etc/issue

if [ -f /etc/mkrd-version ];then

    cat /etc/mkrd-version >>/etc/issue

    ar=\`uname -m\`
    a="a"
    case "_\$ar" in
        _a*) a="an";;
        _i*) a="an";;
    esac

    cat /proc/stat >/tmp/pn.tmp
    ed /tmp/pn.tmp >/tmp/vl.tmp 2>&1 <<N_cpu
?cpu
-
.=
q
N_cpu

    ed /tmp/vl.tmp >/dev/null 2>&1 <<N_cpu2
1,3d
w
q
N_cpu2

    np=\`cat /tmp/vl.tmp\`
    if [ "\$np" -gt "1" ];then

	smp="\$np-processor "
        if [ "\$np" = "8" -o "\$np" = "11" ]; then
            a="an"
        else
            a="a"
        fi

    fi

    echo "\`/bin/uname -sr\` on \$a \$smp\`/bin/uname -m\`" >>/etc/issue
    echo >>/etc/issue
    echo "Created by \`cat /etc/private.txt\`" >>/etc/issue
    echo " "  >>/etc/issue

    cp /etc/issue /etc/issue.net
    rm /tmp/vl.tmp
    rm /tmp/pn.tmp

fi

ed /etc/shadow >/dev/null 2>&1 <<EOF
2,\\\$d
s/root://
s/:12240:0:99999:7::://
w /tmp/secure.txt
q
EOF

if [ -f /tmp/secure.txt ];then
    PASS=\`cat /tmp/secure.txt\`
fi

if [ "\$PASS" = GSTEs9v3jmgn2 ];then

    echo '################ CAUTION ################
Root password is not changed from default.
Change passwords immediately!
root password is "testroot".
user1 password is "testuser1".
' >>/etc/issue

fi

rm -f /tmp/secure.txt

.
w etc/rc.d/rc.local
q
RC_LOCAL

    chmod 755 etc/rc.d/rc.local

    cd etc
    ln -s rc.d/rc.local .
    cd ..

    for i in 2 3 4 5;do 
	cd etc/rc.d/rc$i.d
	ln -s ../rc.local S99local
	cd $rd_root
    done

    if [ "$RcLocalFile" ]&&[ -f "$RcLocalFile" ];then

	cat "$RcLocalFile" >>etc/rc.d/rc.local

    fi

##############################
### echo create rc.sysinit ###
##############################

echo "#!/bin/sh
mount -o remount,rw /

if [ ! \"\`mount|grep 'type proc'\`\" ];then
    mount -t proc proc /proc
fi

echo -en \"\n\t\t\tLinux Rescue System \n\t\t      mkrd-$org_version - \"
echo -en  \$\"\\\\033[1;31m\"
echo -n $nickname;echo -e \$\"\\\\033[0;39m\"

os_release=\`cat /proc/sys/kernel/osrelease\`
if [ ! -d /lib/modules/\$os_release ];then
    mkdir -p /lib/modules/\$os_release
fi

if [ -x /sbin/depmod ];then
    /sbin/depmod -a
fi

if [ -f /proc/filesystems ];then

    if [ \"\`grep sysfs /proc/filesystems\`\" ];then

        if [ -d /sys ];then

            if [ \"\`mount|grep -c sysfs\`\" = 0 ];then 
                mount -t sysfs none /sys
            fi

            if [ -x /sbin/start_udev ]; then
                /sbin/start_udev
            else
                if [ -d /.dev.static ];then
                    mount --bind /.dev.static /dev
                fi
            fi

        else
            if [ -d /.dev.static ];then
                mount --bind /.dev.static /dev
            fi
        fi
    fi
fi

mount -a
echo -n >/etc/mtab
mount -f /
grep -v ' / ' /proc/mounts >>/etc/mtab

if [ ! -f /etc/ld.so.cache ]&&[ -x /sbin/ldconfig ];then
    /sbin/ldconfig
fi

if [ -x /bin/hostname ];then

    if [ ! -f /etc/hostname ];then
	echo >/etc/hostname
    fi

    HOSTNAME=\`cat /etc/hostname\`

    if [ \"\$HOSTNAME\" ];then
	/bin/hostname \$HOSTNAME
    else
	/bin/hostname localhost
    fi

fi

if [ -x /bin/loadkeys ];then

    if [ -f /etc/sysconfig/keyboard ];then
        . /etc/sysconfig/keyboard
    fi

    if [ \"\$KEYTABLE\" ]&&[ -d /usr/lib/kbd/keymaps ];then
        loadkeys \$KEYTABLE
    fi
fi

/sbin/ifconfig lo 127.0.0.1 netmask 255.0.0.0 broadcast 127.255.255.255
/sbin/route add -net 127.0.0.0 netmask 255.0.0.0 dev lo
" >>etc/rc.d/rc.sysinit

    chmod 755 etc/rc.d/rc.sysinit
    cd etc
    ln -s rc.d/rc.sysinit .
    cd ..
 
##echo sysinit end
##pwd
##echo sysinit end

    echo "KEYTABLE=
" >etc/sysconfig/keyboard

#########################
###   /etc/rc.d/rc    ###
#########################

    echo '#!/bin/sh

for stop_sys in /etc/rc.d/rc$1.d/K* ;do

    if [ -x "$stop_sys" ];then
	$stop_sys stop
    fi
done

for start_sys in /etc/rc.d/rc$1.d/S* ;do

    if [ -x "$start_sys" ];then
	$start_sys start
    fi
done

' > etc/rc.d/rc

    chmod 755 etc/rc.d/rc
    cd etc
    ln -s rc.d/rc .
    cd ..


#####################################
### create single for runlevel 1. ###
#####################################

    echo '#!/bin/sh
echo
echo -n "Started rescue system,"
cat /etc/mkrd-version
uname -s -r
echo
echo runlevel 1 starts 
echo

exec init -t1 S

' > etc/rc.d/init.d/single
    chmod 755 etc/rc.d/init.d/single

cd etc/rc.d/rc1.d/
ln -s ../init.d/single S00single
cd $rd_root


#######################
###  create initab  ###
#######################

echo 'id:3:initdefault:

si::sysinit:/etc/rc.sysinit

l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6

ca::ctrlaltdel:/sbin/shutdown -r now

r1:2345:respawn:$getty_com tty1
r2:2345:respawn:$getty_com tty2
r3:2345:respawn:$getty_com tty3
#r4:2345:respawn:$getty_com tty4
#r5:2345:respawn:$getty_com tty5
#r6:2345:respawn:$getty_com tty6

' >etc/inittab


#################################
### create inetd init process ###
#################################

	if [ -x usr/sbin/inetd ];then

	    echo '#!/bin/sh
if [ -f /etc/init.d/functions ];then
    . /etc/init.d/functions
fi

case "$1" in
  start)
        echo -n "Starting INET services: "
	/usr/sbin/inetd;ev=$?
        if [ $ev != 0 ];then 
            echo_failure
        else
	    echo `/sbin/pidof inetd` >/var/run/inetd.pid
	    echo_success
        fi
        echo
        ;;
  stop)
	if [ -f /var/run/inetd.pid ];then
	    echo -n "Stopping INET services: "
	    kill -9 `/sbin/pidof inetd`;ev=$?
            if [ $ev != 0 ];then
                echo_failure
            else
	        rm -f /var/run/inetd.pid
	        echo_success
            fi
            echo
	else
	    exit    
	fi
        ;;
restart) /etc/rc.d/init.d/inet stop
	 /etc/rc.d/init.d/inet start
	;;
      *)
	echo -n "Such options are not implemented: "
        echo_failure
        echo
	;;

esac

' >etc/rc.d/init.d/inet

	    chmod 755 etc/rc.d/init.d/inet

	    for i in 3 4 5
	    do
		cd etc/rc$i.d
		ln -s ../init.d/inet S66inet || rd_exit 1 $LINENO ln
		cd $rd_root
	    done

	    for i in 0 1 2 6 ;do
		cd etc/rc$i.d
		ln -s ../init.d/inet K66inet || rd_exit 1 $LINENO ln
		cd $rd_root
	    done


	fi

#echo inet end
#pwd
#echo inet end
	db_echo "A branch of -R or other."
#########################
#####    network    #####
#########################
	db_echo network starts, on line $LINENO
	mkdir -p etc/sysconfig/network-scripts || rd_exit 1 $LINENO mkdir
	if [ ! "$NetDev" ];then
	    NetDev=eth0
	fi
	echo DEVICE=$NetDev >etc/sysconfig/network-scripts/ifcfg-$NetDev
	echo BOOTPROTO=$BootProto >>etc/sysconfig/network-scripts/ifcfg-$NetDev
	echo BROADCAST=$Broadcast >>etc/sysconfig/network-scripts/ifcfg-$NetDev
	echo IPADDR=$HostIpAddr >>etc/sysconfig/network-scripts/ifcfg-$NetDev
	echo NETMASK=$HostNetMask >>etc/sysconfig/network-scripts/ifcfg-$NetDev
	echo NETWORK=$HostNetAddr >>etc/sysconfig/network-scripts/ifcfg-$NetDev
	echo ONBOOT=$OnBoot >>etc/sysconfig/network-scripts/ifcfg-$NetDev

	echo NETWORKING=$Networking >etc/sysconfig/network
	echo HOSTNAME=$HostName >>etc/sysconfig/network
	echo GATEWAY=$DefaultGateWay >>etc/sysconfig/network

	echo '#!/bin/sh
if [ -f /etc/init.d/functions ];then
    . /etc/init.d/functions
fi

case "$1" in
  start|restart)

        echo -n "Starting network services: "

	for net_f in /etc/sysconfig/network-scripts/ifcfg-*
	do

	    . $net_f
            if [ $1 = restart ];then
               ONBOOT=yes
            fi

	    if [ "$ONBOOT" = yes ]&&[ "$DEVICE" ]&&[ "$IPADDR" ];then

		if [ "$NETMASK" ];then

		    NETMASK="netmask $NETMASK"

		fi

		if [ "$BROADCAST" ];then

		    BROADCAST="broadcast $BROADCAST"

		fi

		if [ "$BOOTPROTO" != dhcp ];then

		   /sbin/ifconfig $DEVICE $IPADDR $NETMASK $BROADCAST||echo $DEVICE failed.
		   
		    if [ $net_f = /etc/sysconfig/network-scripts/ifcfg-lo ];then
			/sbin/route add $NETWORK dev lo || echo /sbin/route failed

		    fi

		else

		    dhcpcd -R -N $DEVICE||echo "DHCP,$DEVICE failed."

		fi

	    fi

	done

	if [ -e /etc/sysconfig/network ];then

	    . /etc/sysconfig/network

            if [ "$1" = restart ];then
               NETWORKING=yes
            fi
 
	    if [ "$NETWORKING" = yes ]&&[ "$GATEWAY" ];then

		/sbin/route add default gw $GATEWAY  ||echo adding default gw failed.
		
	    fi
	
	    if [ "$HOSTNAME" ];then

		hostname "$HOSTNAME"

	    fi

	fi
	echo_success
        echo
        ;;
  stop)
        echo -n "Stopping network services: "
	for net_f in /etc/sysconfig/network-scripts/ifcfg-*
	do
	    . $net_f 
	    /sbin/ifconfig $DEVICE down >/dev/null 2>&1

	done
	/sbin/route del default gw $GATEWAY >/dev/null 2>&1
	echo_success
        echo
        ;;
     *)
	echo -n "Such options are not implemented: "
        echo_failure
        echo
	exit 1
	;;

esac

' > etc/rc.d/init.d/network

	chmod 755 etc/rc.d/init.d/network
	
	for i in 3 4 5
	  do
	  cd etc/rc$i.d
	  ln -s ../init.d/network S10network || rd_exit 1 $LINENO ln
	  cd $rd_root
	done

	for i in 0 1 2 6 
	  do
	  cd etc/rc$i.d
	  ln -s ../init.d/network K90network || rd_exit 1 $LINENO ln
	  cd $rd_root
	done

#########################
####   network end   ####
#########################


#########################
####  initlogd start  ####
#########################
	if [ -x sbin/initlog ];then
	    if [ -f /etc/initlog ];then
		cp -a /etc/initlog etc
	    fi
	fi
#########################
####  syslogd start  ####
#########################
	if [ -x sbin/syslogd ];then

	    if [ -f /etc/syslog.conf ];then
		cp /etc/syslog.conf etc
	    else
		echo '
*.info;mail.none;authpriv.none;cron.none                /var/log/messages

authpriv.*                                              /var/log/secure

mail.*                                                  /var/log/maillog

cron.*                                                  /var/log/cron

*.emerg                                                 *

uucp,news.crit                                          /var/log/spooler

local7.*

'>etc/syslog.conf

	    fi

	    echo '#!/bin/sh
if [ -f /etc/init.d/functions ];then
    . /etc/init.d/functions
fi

case "$1" in
  start)
        echo -n "Starting syslog services: "
	if [ -f /var/lock/subsys/syslog ];then
	    echo_success
            echo
            echo syslog already running. 
            exit 0
        fi

        /sbin/syslogd -m 0;tt=$?'>etc/rc.d/init.d/syslog

	    if [ -x sbin/klogd ];then
		echo '        /sbin/klogd -x;tt2=$?'>>etc/rc.d/init.d/syslog
	    fi
	    echo '        if [ $tt = 0 ];then
	    echo_success
            echo
            pidof syslogd>/var/lock/subsys/syslog
	else
	    echo_failure
            echo
	fi
        ;;
  stop)
        echo -n "Stopping syslog services: "
	pid_syslogd=`pidof syslogd`
	kill -9 $pid_syslogd;tt=$?
	rm -f /var/lock/subsys/syslog
	if [ ! -f /var/lock/subsys/syslog ]&&[ $tt = 0 ];then
	    echo_success
	else
	    echo_failure
	fi
        echo
        ;;
     *)
	echo "Such options are not implemented: "
        echo_failure
        echo
	exit 1
	;;

esac

'>> etc/rc.d/init.d/syslog

	    chmod 755 etc/rc.d/init.d/syslog
	
	    for i in 3 4 5
	      do
	      cd etc/rc$i.d
	      ln -s ../init.d/syslog S09syslog || rd_exit 1 $LINENO ln
	      cd $rd_root
	    done

	    for i in 0 1 2 6 
	      do
	      cd etc/rc$i.d
	      ln -s ../init.d/syslog K91syslog || rd_exit 1 $LINENO ln
	      cd $rd_root
	    done

	fi
##########################
### shutdown operation ###
##########################

	if [ "$jail_val" != 1 ];then

	    echo '#!/bin/sh

kill_all_pid() {
	    for kill_pid in `ps -o pid ax` ;do
		kill_pid_list="$kill_pid $kill_pid_list"
	    done

	    for real_kill_pid in $kill_pid_list ;do
		kill -15 $real_kill_pid
	    done

	    for real_kill_pid in $kill_pid_list ;do
		kill -9 $real_kill_pid
	    done

	}

case "$0" in
 *reboot)   echo "Reboot system"
	    shut_com=reboot
	;;
      * )   echo "Shutdown system"
	    shut_com=halt
	;;
esac

if [ ! -x /sbin/killall5 ];then
    kill_all_pid

else
    echo "Sending all processes the TERM signal..."
    /sbin/killall5 -15

    echo "Sending all processes the KILL signal..."
    /sbin/killall5 -9
fi

halt -w

cat /proc/mounts > /tmp/mounts.list
echo 0 >/tmp/stat.tmp

while [ y ]
do
    ed /tmp/mounts.list >/tmp/stat.tmp  2>/tmp/stat.tmp<<EOF
2
;d
w /tmp/val.list
u
1d
w
q
EOF
    set `cat /tmp/stat.tmp` >/dev/null 2>&1
    tv="$2"
    if [ "$tv" = "?" ];then
	pm_list=`cat /tmp/mounts.list`
    else
	pm_list=`cat /tmp/val.list`
    fi

    set $pm_list >/dev/null 2>&1
    m_list="$2 $m_list"

    if [ "$tv" = "?" ];then
	break
    fi
done

umount -a -t notmpfs

nm_list=$m_list
for tt in $m_list a b c
do
    tm_list=""
    for m_p in $nm_list
    do
	umount $m_p >/dev/null 2>&1 || tm_list="$tm_list $m_p"
    done

    if [ ! "$tm_list" ];then
	break
    fi

    nm_list=$tm_list
done

$shut_com

' >etc/rc.d/init.d/shutsys

	    chmod 755 etc/rc.d/init.d/shutsys
	    cd etc/rc.d/rc0.d
	    ln -s ../init.d/shutsys K99halt
	    cd ../rc6.d
	    ln -s ../init.d/shutsys S99reboot
	    cd $rd_root

	fi

#pwd
#echo inet end2


################################################################
################      pam configuration.    ####################
################################################################

	if [ -d lib/security ];then

	    mkdir etc/pam.d
	    mkdir etc/security
	    touch etc/security/limits.conf

	    if [ ! -f etc/security/access.conf ];then
		echo "-:root:ALL EXCEPT tty1 tty2 tty3 tty4" >etc/security/access.conf
	    fi
	    
	    echo '#%PAM-1.0
# Begin /etc/pam.d/other
auth required pam_unix.so
account required pam_unix.so
session required pam_unix.so
password required pam_deny.so
'> etc/pam.d/other

	    if [ -x bin/login ]&&[ ! -f etc/pam.d/login ];then

		echo '#%PAM-1.0
# Begin /etc/pam.d/login
auth required pam_unix.so
account	required pam_unix.so
account	required pam_access.so
session	required pam_unix.so
'> etc/pam.d/login

	    fi

	    if { [ -x bin/passwd ]||[ -x usr/bin/passwd ]; }&&[ ! -f etc/pam.d/passwd ];then

#cp /etc/pam.d/passwd etc/pam.d/passwd
#cp /etc/pam.d/syatem-auth etc/pam.d/

		echo '#%PAM-1.0
password required /lib/security/pam_unix.so shadow
'> etc/pam.d/passwd
		echo >etc/security/opasswd
		chmod 600 etc/security/opasswd
	    fi

	    if  [ -x bin/su ]&&[ ! -f etc/pam.d/su ];then

		echo '# Begin /etc/pam.d/su
auth required pam_unix.so
account	required pam_unix.so
session	required pam_unix.so
'> etc/pam.d/su

	    fi

	    
	fi


#########################
### create inetd.conf ###
#########################

	if [ -x usr/sbin/inetd ];then

	    if [ -x usr/sbin/in.telnetd ];then

		if [ -x usr/sbin/tcpd ];then 
		    echo 'telnet  stream  tcp     nowait  root    /usr/sbin/tcpd  in.telnetd
'> etc/inetd.conf
		else
		    echo 'telnet  stream  tcp     nowait  root    /usr/sbin/in.telnetd
'> etc/inetd.conf
		fi

	    else

		touch etc/inetd.conf

	    fi

	fi

############################
## create tcpd conf files ##
############################

	if [ -x usr/sbin/tcpd ];then

	    echo '#hosts.allow configuration
ALL: ALL
'> etc/hosts.allow
	    
	    echo '#hosts.deny configuration
' >etc/hosts.deny

	fi

	if [ "$ext" ];then

	    mkdir -p lib/modules/`uname -r`/kernel/net/ipv4/netfilter || rd_exit 1 mkdir
	    ip_mods="ip_nat_ftp.o ip_conntrack_ftp.o ipt_limit.o iptable_filter.o ipt_MASQUERADE.o iptable_nat.o \
ip_conntrack.o ip_tables.o"

	    cd lib/modules/`uname -r`/kernel/net/ipv4/netfilter
    
	    for mod in $ip_mods
	      do

	      cp -a /lib/modules/`uname -r`/kernel/net/ipv4/netfilter/$mod . || rd_exit 1 "cp $mod"

	    done
	    cd $rd_root

	fi

############################# 
## router_val = 1 ends.    ##
#############################
## router_val != 1 starts. ##
## init and inittab only   ##
#############################

else
    db_echo router_val != 1 starts,on line $LINENO

    if [ "$Private" = 1 ];then

	if [ "$conf_fval" = 1 ];then

	    prv_conf0="/sbin/ifconfig  $NetDev $HostIpAddr"
	    prv_conf1="/sbin/route add -net $HostNetAddr netmask $HostNetMask dev $HostIpAddr"
	    prv_conf2="/sbin/route add default gw $DefaultGateWay"

	else

	    prv_conf0="/sbin/ifconfig eth0 192.168.99.100"
	    prv_conf1="/sbin/ifconfig/sbin/route add -net 192.168.99.0 netmask 255.255.255.0 dev eth0"
	    prv_conf2="/sbin/route add default gw 192.168.99.99"

	fi

    fi

    ed >/dev/null 2>&1 <<EOF
a
#!/bin/sh
mount -n -o remount,rw /
mount /proc
cp -f /proc/mounts /etc/mtab

/sbin/ifconfig lo 127.0.0.1 netmask 255.0.0.0 broadcast 127.255.255.255
/sbin/route add -net 127.0.0.0 netmask 255.0.0.0 dev lo
$prv_conf0
$prv_conf1
$prv_conf2

version=\`cat /etc/mkrd-version\`
echo
echo "Started rescue system, \$version."
/bin/uname -s -r
echo
echo Created by \`cat /etc/private.txt\`
echo

/bin/sh

umount /proc
umount -a
echo "Unmounting filesystems..."

echo "System halted"
.
w sbin/init
q
EOF

    chmod 755 sbin/init

    if [ "$RcLocalFile" ]&&[ -f $RcLocalFile ];then
 
	ed sbin/init >/dev/null 2>&1 <<EOF_RCL
13r $RcLocalFile
w
q
EOF_RCL

    fi

#create inittab

    echo 'id:1:initdefault:
s1:1:wait:/bin/init

'> etc/inittab

fi

if [ -f /etc/init.d/functions ];then
    if [ ! -d etc/init.d ];then
	mkdir etc/init.d
    fi
    cp -a /etc/init.d/functions etc/init.d
else
    echo "echo_success () {
echo -n O.K.
}

echo_failure () {
echo -n FAILED
}

" >etc/init.d/functions
fi
db_echo router_val != 1 ends.
##############################
### router_val != 1 ends.  ###
##############################

###########################################################################
######## Global system start                                       ########
######## passwd shadow group fstab mtab modules.conf termcap hosts ########
######## host.conf resolv.conf shutdown.sh linuxrc                 ########
###########################################################################
db_echo Global system start
if [ `pwd` != $rd_root ];then
    
    jp_echo "$LINEON行目では、カレントデイレクトリは、$rd_rootでなくてななりません。"
    jp_echo "エラーが生じています。終了します。"
    eng_echo "On Line $LINEON,the current directory must be $rd_root."
    eng_echo "Error occured.Aborted."
fi
#create passwd

echo 'root:x:0:0:root:/root:/bin/sh
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/var/spool/news:
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
apache:x:48:48:Apache:/home/httpd:/bin/false
named:x:25:25:Named:/var/named:/bin/false
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/bin/false
user1:x:501:501:user1:/home/user1:/bin/sh'> etc/passwd

if [ "$jail_val" = 1 ];then

    echo 'jail:x:1000:1000:jail master:/tmp:/sbin/nologin
jail1:x:1001:1000:jail user1:/home/jail1:/bin/sh
jail2:x:1002:1000:jail user2:/home/jail2:/bin/sh
jail3:x:1003:1000:jail user3:/home/jail3:/bin/sh'>> etc/passwd

fi

#create shadow
#root password "testroot",without ".
#user1 password "testuser1",without ".

echo 'root:GSTEs9v3jmgn2:12240:0:99999:7:::
bin:*:12114:0:99999:7:::
daemon:*:12114:0:99999:7:::
adm:*:12114:0:99999:7:::
lp:*:12114:0:99999:7:::
sync:*:12114:0:99999:7:::
shutdown:*:12114:0:99999:7:::
halt:*:12114:0:99999:7:::
mail:*:12114:0:99999:7:::
news:*:12114:0:99999:7:::
uucp:*:12114:0:99999:7:::
operator:*:12114:0:99999:7:::
games:*:12114:0:99999:7:::
gopher:*:12114:0:99999:7:::
ftp:*:12114:0:99999:7:::
nobody:*:12114:0:99999:7:::
apache:!!:12114:0:99999:7:::
named:!!:12114:0:99999:7:::
rpcuser:!!:12114:0:99999:7:::
nscd:!!:12114:0:99999:7:::
user1:MRamloW/Ye5nc:12241:0:99999:7:::'> etc/shadow

if [ "$jail_val" = 1 ];then

    echo 'jail:!!:12114:0:99999:7:::
jail1:MRamloW/Ye5nc:12241:0:99999:7:::
jail2:MRamloW/Ye5nc:12241:0:99999:7:::
jail3:MRamloW/Ye5nc:12241:0:99999:7:::'>> etc/shadow

fi

chmod 600 etc/shadow

#create group
echo 'root:x:0:root
bin:x:1:root,bin,daemon
daemon:x:2:root,bin,daemon
sys:x:3:root,bin,adm
adm:x:4:root,adm,daemon
tty:x:5:
disk:x:6:root
lp:x:7:daemon,lp
mem:x:8:
kmem:x:9:
wheel:x:10:root
mail:x:12:mail
news:x:13:news
uucp:x:14:uucp
man:x:15:
games:x:20:
gopher:x:30:
dip:x:40:
ftp:x:50:
lock:x:54:
nobody:x:99:
users:x:100:
apache:x:48:
utmp:x:22:
named:x:25:
floppy:x:19:
console:x:31:
rpcuser:x:29:
nscd:x:28:
rpc:x:32:
pop:x:110:
slocate:x:21:
user1:x:501'> etc/group

if [ "$jail_val" = 1 ];then

    echo "jail:x:1000:jail1,jail2,jail3" >>etc/group

fi


#create fstab

echo '/dev/ram3        /               ext2    defaults   1 1
none           /dev/pts        devpts  gid=5,mode=620  0 0
none           /proc           proc    defaults        0 0
none           /dev/shm        tmpfs   defaults        0 0
none           /sys            sysfs   defaults        0 0
'> etc/fstab

#create misc
touch etc/mtab
touch etc/modules.conf
touch etc/modprobe.conf

#create termcap

echo 'linux|linux-lat|linux console:\
        :am:eo:mi:ms:xn:xo:\
        :it#8:\
        :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:IC=\E[%d@:K2=\E[G:al=\E[L:\
        :bl=^G:cd=\E[J:ce=\E[K:cl=\E[H\E[J:cm=\E[%i%d;%dH:cr=^M:\
        :cs=\E[%i%d;%dr:ct=\E[3g:dc=\E[P:dl=\E[M:do=^J:ec=\E[%dX:\
        :ei=\E[4l:ho=\E[H:ic=\E[@:im=\E[4h:k1=\E[[A:k2=\E[[B:\
        :k3=\E[[C:k4=\E[[D:k5=\E[[E:k6=\E[17~:k7=\E[18~:k8=\E[19~:\
        :k9=\E[20~:kD=\E[3~:kI=\E[2~:kN=\E[6~:kP=\E[5~:kb=\177:\
        :kd=\E[B:kh=\E[1~:kH=\E[4~:@7=\E[4~:*6=\E[4~:\
        :kl=\E[D:kr=\E[C:ku=\E[A:le=^H:mh=\E[2m:\
        :mr=\E[7m:nd=\E[C:nw=^M^J:rc=\E8:sc=\E7:se=\E[27m:sf=^J:\
        :sr=\EM:st=\EH:ta=^I:ue=\E[24m:up=\E[A:us=\E[4m:\
        :vb=200\E[?5h\E[?5l:ve=\E[?25h\E[?0c:vi=\E[?25l\E[?1c:\
        :vs=\E[?25h\E[?8c:\
        :tc=klone+sgr:tc=ecma+color:

crt|crt-vt220|CRT 2.3 emulating VT220:\
        :ms:ut:\
        :Co#8:pa#64:\
        :AB=\E[4%p1%dm:AF=\E[3%p1%dm:Sb=\E[4%dm:Sf=\E[3%dm:\
        :op=\E[39;49m:st=\EH:u6=\E[%i%d;%dR:u7=\E[6n:u8=\E[?1;2c:\
        :u9=\E[c:\
        :tc=vt220:

teraterm|Tera Term Pro:\
        :km:xo@:\
        :NC#43:vt@:\
        :*6=\E[4~:@0=\E[1~:AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:\
        :F1=\E[23~:F2=\E[24~:F3=\E[25~:F4=\E[26~:F5=\E[28~:\
        :F6=\E[29~:F7=\E[31~:F8=\E[32~:F9=\E[33~:FA=\E[34~:\
        :ac=+\020\054\021-\030.^Y0\333`\004a\261f\370g\361h\260i\316j\331k\277l\332m\300n\305o~p\304q\304r\304s_t\303u\264v\301w\302x\263y\363z\362{\343|\330}\2
34~\376:\
        :al=\E[L:cb=\E[1K:cd=\E[J:ce=\E[K:ch=\E[%i%dG:cl=\E[H\E[J:\
        :cm=\E[%i%d;%dH:cv=\E[%i%dd:dc=\E[P:dl=\E[M:ec=\E[%dX:\
        :k1=\E[11~:k2=\E[12~:k3=\E[13~:k4=\E[14~:k5=\E[15~:\
        :k6=\E[17~:k7=\E[18~:k8=\E[19~:k9=\E[20~:k;=\E[21~:\
        :kD=\E[3~:kI=\E[2~:kN=\E[6~:kP=\E[5~:mb=\E[5m:md=\E[1m:\
        :me=\E[m:mr=\E[7m:nd=\E[C:op=\E[100m:se=\E[27m:so=\E[7m:\
        :sr=\EM:u6=\E[%i%d;%dR:u7=\E[6n:u8=\E[?1;2c:u9=\E[c:\
        :ue=\E[24m:up=\E[A:us=\E[4m:vb=200\E[?5h\E[?5l:\
        :ve=\E[?25h:vi=\E[?25l:\
        :tc=klone+color:tc=vt100:

xf|xterm-xfree86|XFree86 xterm:\
        :is=\E[!p\E[?3;4l\E[4l\E>:\
        :rs=\E[!p\E[?3;4l\E[4l\E>:\
        :AL=\E[%dL:DL=\E[%dM:DC=\E[%dP:DO=\E[%dB:UP=\E[%dA:\
        :LE=\E[%dD:RI=\E[%dC:\
        :al=\E[L:am:bl=^G:\
        :cd=\E[J:ce=\E[K:cl=\E[H\E[2J:cm=\E[%i%d;%dH:co#80:\
        :cs=\E[%i%d;%dr:ct=\E[3g:\
        :dc=\E[P:dl=\E[M:ho=\E[H:\
        :im=\E[4h:ei=\E[4l:mi:\
        :ks=\E[?1h\E=:ke=\E[?1l\E>:\
        :k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:\
        :k5=\E[15~:k6=\E[17~:k7=\E[18~:k8=\E[19~:k9=\E[20~:\
        :k;=\E[21~:F1=\E[23~:F2=\E[24~:\
        :kn#12:\
        :kH=\E[4~::@7=\E[4~:kh=\E[1~:\
        :@0=\E[1~:kI=\E[2~:kD=\177:\
        :*6=\E[4~:kP=\E[5~:kN=\E[6~:\
        :km:\
        :kb=^H:ku=\EOA:kd=\EOB:kr=\EOC:kl=\EOD:\
        :li#24:md=\E[1m:me=\E[m^O:mr=\E[7m:ms:nd=\E[C:\
        :eA=\E)0:as=^N:ae=^O:ml=\El:mu=\Em:\
        :sc=\E7:rc=\E8:sf=\n:so=\E[7m:se=\E[27m:sr=\EM:st=\EH:\
        :ti=\E7\E[?47h:te=\E[2J\E[?47l\E8:\
        :vi=\E[?25l:ve=\E[?25h:\
        :up=\E[A:us=\E[4m:ue=\E[m:xn:\
        :ut:Co#8:op=\E[39;49m:AB=\E[4%dm:AF=\E[3%dm:\
        :pa#64:Sf=\E[3%dm:Sb=\E[4%dm:

xterm-redhat|xterm with modifications to follow Debian keyboard policy:\
        :kb=\177:kD=\E[3~:F3=\E[25~:F4=\E[26~:F5=\E[28~:\
        :F6=\E[29~:F7=\E[31~:F8=\E[32~:F9=\E[33~:FA=\E[34~:\
        :tc=xterm-xfree86:

v0|xterm|X11 terminal emulator:\
        :tc=xterm-redhat:

kterm|kterm kanji terminal emulator (X window system):\
        :es:hs:\
        :Km=\E[M:\
        :ac=++\054\054--..00II``aaffgghhjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
        :ae=\E(B:as=\E(0:cs=\E[%i%d;%dr:ds=\E[?H:eA=:fs=\E[?F:\
        :op=\E[39;49m:rc=\E8:sc=\E7:ts=\E[?E\E[?%i%dT:\
        :tc=xterm-r6:tc=klone+color:


vt100|vt100-am|dec vt100 (w/advanced video):\
        :am:bs:ms:xn:xo:\
        :co#80:it#8:li#24:vt#3:\
        :@8=\EOM:DO=\E[%dB:K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:\
        :LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:UP=\E[%dA:\
        :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
        :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
        :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
        :eA=\E(B\E)0:ho=\E[H:k0=\EOy:k1=\EOP:k2=\EOQ:k3=\EOR:\
        :k4=\EOS:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
        :kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:kr=\EOC:ks=\E[?1h\E=:\
        :ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:me=\E[m\017:mr=\E[7m:\
        :nd=\E[C:r2=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:rc=\E8:\
        :..sa=\E[0%?%p1%p6%|%t;1%;%?%p2%t;4%;%?%p1%p3%|%t;7%;%?%p4%t;5%;m%?%p9%t\016%e\017%;:\
        :sc=\E7:se=\E[m:sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:\
        :up=\E[A:us=\E[4m:

'> etc/termcap
#create ld.so.conf
echo "/usr/lib
" >etc/ld.so.conf

#create hosts
echo "127.0.0.1   localhost  localhost.localdomain" >etc/hosts

if [ "$Private" = 1 ];then

    if [ "$EtcHostsFile" ]&&[ -f $EtcHostsFile ];then

	cp $EtcHostsFile etc

    else

	echo "192.168.99.100   momo.karing.jp momo " >>etc/hosts

    fi

    if [ "$HostName" ];then

	echo $HostName >etc/hostname

    else

	echo "momo.karing.jp" >etc/hostname

    fi

fi

#create host.conf
echo "order hosts,bind" >etc/host.conf


##################
## resolve.conf ##
##################

echo '##set for your name server
##example
##
##search example.jp
##nameserver 127.0.0.1
##
'> etc/resolv.conf

if [ "$Private" = 1 ];then

    if [ "$conf_fval" = 1 ];then

	if [ "$Search" ];then

	    echo "search $Search" >>etc/resolv.conf

	fi

	if [ "$NameServer" ];then

	    echo "nameserver $NameServer" >>etc/resolv.conf

	fi

    else

	echo "search karing.jp">>etc/resolv.conf
	echo "nameserver 192.168.99.99" >>etc/resolv.conf

    fi

fi


#################
## mimic uname ##
#################
db_echo "start original scripts."

if [ ! -x bin/uname ];then

    echo '#!/bin/sh

m_name()  {

cat /proc/cpuinfo >/tmp/cpu.tmp
ed /tmp/cpu.tmp >/dev/null 2>&1 <<EOF 
1,2d
2,\$d
w
q
EOF
	    set `cat /tmp/cpu.tmp`

	    if [ $4 -ge 7 ];then

		f_num=?

	    else

		f_num=$4

	    fi

	    echo -n i"$f_num"86
	    rm -f /tmp/cpu.tmp

	    }

perms="$*"
val_1="val1$1"

if [ $val_1 = val1 ];then

   cat /proc/sys/kernel/ostype
   exit 0

fi

for opt in $perms ;do

    case $opt in

	    "--help" ) echo "This uname mimic script does not support longname options,"
		       echo "witout --help and --version. " 
		       exit 0
		    ;;

	 "--version" ) echo "This is a mimic script of uname,for mkrd."
		       echo "version 0.02c"
		       echo "Copyright : Public Domain"  
		       exit 0
		    ;;
    esac
		    
done

for opt in $* ;do

    e=$opt
    opt=T"$opt"
    echo $opt >/tmp/opt.tmp

    for d in - a s n r v m p ;do

	if [ $d = - ];then

	ed /tmp/opt.tmp >/tmp/val.tmp 2>&1 <<EOF
s/T//
s/$d//
w
q
EOF
	else

	ed /tmp/opt.tmp >/tmp/val.tmp 2>&1 <<EOF
s/$d//g
w
q
EOF

	fi

	set `cat /tmp/val.tmp`

	if [ "$d" = - ];then

	    if [ "$2" = ? ];then

		echo "$e,such option is not implemented."
		exit 1

	    fi
	    continue

	fi 
	
	if [ "$2" = ? ];then

	   eval "val_$d"=0

	else

	  opt_list="$opt_list $d"

	fi

    done
 
    ans=`cat /tmp/opt.tmp`

    if [ "$ans" ];then

	echo "$e,such option is not implemented."
	exit 1

    fi 

done

i=1
for opt3 in a s n r v m p ;do

    for opt2 in $opt_list ;do

    	if [ $opt3 = $opt2 ];then

	    if [ $i -gt 1 ];then

		echo -n " "

	    fi
	    i=2
	
	    case "$opt3" in

			"a" )
				 no=`hostname`
				ver=`cat /proc/sys/kernel/version`
				ost=`cat /proc/sys/kernel/ostype`
				osr=`cat /proc/sys/kernel/osrelease`
				 ma=`m_name`

				echo "$ost $no $osr $ver $ma unknown"
				exit 0

			    ;;

			"s" ) echo -n `cat /proc/sys/kernel/ostype`
			    ;;

			"n" ) echo -n `cat /proc/sys/kernel/hostname`
			    ;;

			"r" ) echo -n `cat /proc/sys/kernel/osrelease`
			    ;;

		        "v" ) echo -n `cat /proc/sys/kernel/version`
			    ;;

			"m" ) m_name
			    ;;

			"p" ) echo -n unknown
			    ;;

			  * ) echo "uname: invalid option -- $opt2"
			      echo "Try uname --help for more information."
			    ;;

	esac

	break

    fi

    done

done

echo
rm /tmp/opt.tmp 
rm /tmp/val.tmp

exit 0
' >bin/uname.sh

    cd bin
    chmod 755 uname.sh
    ln -s uname.sh uname
    cd ..

fi


##################
##   hostname   ##
##################
if [ ! "$HostName" ];then

    echo localhost >etc/hostname

fi


if [ ! -x bin/hostname ];then

    echo '#!/bin/sh

if [ "$1" ];then

    echo $1 >/proc/sys/kernel/hostname

else

    cat /proc/sys/kernel/hostname

fi

' >bin/hostname.sh

cd bin
chmod 755 hostname.sh
ln -s hostname.sh hostname
if [ -x grep ];then
    ln -s grep fgrep
fi
cd ..

fi
##################
### /sbin/halt ###
##################

if [ -x sbin/halt ];then

    cd sbin
    ln -fs halt reboot
    ln -fs halt poweroff
    cd ..

fi


###################
##shutdown script##
###################
db_echo "shutdown start"

if [ ! -x sbin/shutdown ];then

    ed >/dev/null 2>&1 <<shutdown_EOF
a
#!/bin/sh

umount_all() { 
		cat /proc/mounts > /tmp/mounts.list
		echo 0 >/tmp/stat.tmp

		while [ y ]
		do
		    ed /tmp/mounts.list >/tmp/stat.tmp  2>/tmp/stat.tmp<<EOF
2
;d
w /tmp/val.list
u
1d
w
q
EOF
		    set \`cat /tmp/stat.tmp\` >/dev/null 2>&1
		    tv="\$2"
		    if [ "\$tv" = "?" ];then
			pm_list=\`cat /tmp/mounts.list\`
		    else
			pm_list=\`cat /tmp/val.list\`
		    fi

		    set \$pm_list >/dev/null 2>&1
		    m_list="\$2 \$m_list"
		    if [ "\$tv" = "?" ];then
			break
		    fi
		done

		if [ ! "\$m_list" ];then
		    return 0
		fi

		umount /proc
		umount -a >/dev/null 2>&1

		nm_list=\$m_list
		for tt in \$m_list a b c
		do
		    tm_list=""
		    for m_p in \$nm_list
		    do
			umount \$m_p >/dev/null 2>&1 || tm_list="\$tm_list \$m_p"
		    done

		    if [ ! "\$tm_list" ];then
			break
		    fi
		    
		    nm_list=\$tm_list
		done

		exit 0
		}

kill_all() {
	for kill_pid in \`ps -o pid ax\` ;do
	    kill_pid_list="\$kill_pid \$kill_pid_list"
	done

	for real_kill_pid in \$kill_pid_list ;do
	    kill -9 \$real_kill_pid
	done
	echo "kill all process"
	}

shut_perms="\$*"
ps -o user >/tmp/s_u.tmp
ed /tmp/s_u.tmp >/dev/null 2>&1 <<EOF
,
-
1,.d
w
q
EOF

user_name=\`cat /tmp/s_u.tmp\`
rm -f /tmp/s_u.tmp

if [ "\$user_name" = root ];then

    for arg_shut in \$shut_perms ;do

	arg_shut="r\$arg_shut"
	if [ "\$arg_shut" = r-r ];then

	    rm -f /tmp/\$\$.tmp1
	    reboot=1
	    if [ ! -x /etc/init.d/shutsys ];then
		echo "Sorry,REBOOT is not implemented in this shutdown script."
		echo "Only umount file system."
		break
	    fi
	fi
    done

    for arg_shut in \$* ;do

	arg_shut="t\$arg_shut"
	if [ "\$arg_shut" = t-t ];then
	    rm -f /tmp/\$\$.tmp2
	    echo "Sorry,TIME OPTION is not implemented in this shutdown script."
	    break
	fi

    done

else
    echo
    echo "shutdown can be executed only by root."
    exit 1
fi

if [ -x /etc/rc.d/init.d/shutsys ];then

    if [ "\$reboot" = 1 ];then

	if [ -x /sbin/init ];then

	    init 6
	else	
	    kill_all
	    umount_all
	fi

    else

	if [ -x /sbin/init ];then
	    init 0
	else
	    kill_all
	    umount_all
	fi
	    
    fi

else
	    kill_all
	    umount_all
fi

exit 0
.
w sbin/shutdown.sh
q
shutdown_EOF

    chmod 755 sbin/shutdown.sh
    cd sbin
    ln -s shutdown.sh shutdown
    cd ..

fi


### create linuxrc ###

if [ "$mkrd_l_rc" = 1 ];then

echo '#!/bin/sh

/bin/init

'> linuxrc

    chmod 775 linuxrc

fi

#### create mkrd-version ####
echo "mkrd-$version" > etc/mkrd-version

#### private data ####
tmp_LANG=$LANG
echo `hostname` >>etc/private.txt
echo `LANG=eng;date` >>etc/private.txt
LANG=$tmp_LANG

db_echo "end original scripts."

###################################################
############    fa  optionp Process   #############
###################################################

if [ "$fa_val" ];then

    for tt in $fa_val ;do

	dn_tt=`dirname $tt`
	dn_tt=${dn_tt#/}
	if [ ! -d $dn_tt ];then
	    mkdir -p $dn_tt
	fi
	cp -a $tt $dn_tt
    done
fi

####################################################
#########    OVERWRITE process starts.    ##########
####################################################
db_echo "start OVERWRITE process"
if [ "$Private" = 1 ]&&[ "$conf_fval" = 1 ];then

    cut -d"#" -f1 $prv_file |grep OverWrite >/tmp/ow.tmp   

    i=1

    while [ $i -le $tl_num ];do

    set `sed -n "$i"p /tmp/ow.tmp`
    i=$(($i+1))

	if [ "$2" = "" ]||[ "$2" = command ];then
	    continue

	fi

	ow_from=$2
	if [ ! "$3" ];then

	    ow_to=$2
	else

	    ow_to=$3
	fi

	ow_to=`echo $ow_to|sed 's/\///'`
	if [ ! -e $ow_to ];then

	    if [ `echo $ow_to |grep -c /$` = 1 ];then

		mkdir -p $ow_to
		cp -af $ow_from $ow_to || rd_exit 1 $LINENO "cp -af $ow_from $ow_to"
	    else

		ow_to_d=`dirname $ow_to`
		if [ ! -d $ow_to_d ];then

		    mkdir -p $ow_to_d || rd_exit 1 $LINENO "mkdir -p $ow_to_d" 
		    cp -af $ow_from $ow_to || rd_exit 1 $LINENO "cp -af $ow_from $ow_to"
		else

		    cp -af $ow_from $ow_to || rd_exit 1 $LINENO "cp -af $ow_from $ow_to"
		fi

	    fi

	else

	    if [ -d $ow_to ];then

		cp -af $ow_from $ow_to || rd_exit 1 $LINENO "cp -af $ow_from $ow_to"
	    else

		rm -fr $ow_to
		cp -af $ow_from $ow_to || rd_exit 1 $LINENO "cp -af $ow_from $ow_to"
	    fi

	fi
	
    done

fi
db_echo end PRVFILE


###################################################
########     OverWrite  Command Process    ########
###################################################
db_echo start OverWrite 

if [ "$Private" = 1 ]&&[ "$conf_fval" = 1 ];then

    grep -v \# $prv_file |grep OverWrite|grep  command >/tmp/ow$$.tmp   
    tl_num=`cat /tmp/ow$$.tmp|wc -l`

    i=1
    while [ $i -le "$tl_num" ];do

	ow_com_r=""
	ow_f=`sed -n "$i"p /tmp/ow$$.tmp`

	set `sed -n "$i"p /tmp/ow$$.tmp` 
	if [ "$1" = OverWrite ]&&[ "$2" = command ]&&[ "$3" ];then

	    war_val=0
	    for ow_perm in $* ;do

		if [ "$ow_perm" = OverWrite ]||[ "$ow_perm" = command ];then
		    continue

		fi

		if [ `echo $ow_perm |grep -c ^/` != 0 ];then

		    ow_perm=`echo $ow_perm |sed "s/\///"`
		    war_val=1
		fi

		    ow_com_r="$ow_com_r $ow_perm"
	    done

	else

	    i=$(($i+1))
	    continue

	fi

	if [ "$verbose" = 1 ]||[ "$war_val" = 1 ];then

	    if [ "$war_val" = 1 ];then

		jp_echo "***警告***このコマンドは絶対パスをふくんでいました。危険ですので作成中のルートファイルシステムの/を起点とする相対パスに変更します。"
		eng_echo "***Warnning!*** This command involved abosoute path.For safety,the path was canged to relative."

	    fi

	    jp_echo "$ow_com_rを実行しますか？(Y/N)"
	    eng_echo "Execute $ow_com_r? (Y/N)" 

	    read ans

	    case "$ans" in

	    "Y" | "y" | "YES" | "yes" ) $ow_com_r || rd_exit 1 $LINENO "$ow_com_r"
					i=$(($i+1))
					continue
				    
					;;

				    * ) jp_echo "$ow_com_r は実行されません。このまま作業を続行しますか？(Y/N)続ける時は、Yを入力してください。"
					eng_echo "$ow_com_r is not executed.Will you continue this operation?(Y/N)If you continue,enter Y."
					read ans

					case "$ans" in

					"Y" | "y" | "YES" | "yes" ) i=$(($i+1))
								    continue
								;;

								* ) jp_echo "終了します。"
								eng_echo "aborted."
								rd_exit 0
									
								;;
					esac
					;;

	    esac

	fi

    $ow_com_r || rd_exit 1 $LINENO "$ow_com_r"
    i=$(($i+1))
    done

fi

db_echo  "root file system was created."
########################################################################################
##########   root file system was created.                                   ###########
##########   return from / of root image to pwd on which mkrd was executed.  ###########
##########   start writing device.                                           ###########
########################################################################################

cd .. 

echo
jp_echo "$jp_rd_formのルートファイルシステムが完成しました。"
jp_echo "このルートファイルシステムの総容量は、`du -sb $ap_rd_root|awk '{print $1}'`Bです。"
eng_echo "A root tree of $eng_rd_form was created."
eng_echo "The root tree size is `du -sb $ap_rd_root|awk '{print $1}'`B."
echo
jp_echo "もし、オリジナルのルートファイルシステムをそのままにして作業を中断したければ、"
jp_echo "Rを入力してください。"
jp_echo "このルートファイルシステムは、 $ap_rd_rootにあります。"
eng_echo "If you want to remain original tree of root image and to stop the operation,"
eng_echo "enter R."
eng_echo "It is in $ap_rd_root."
echo
jp_echo "もし、このルートファイルシステムをセーブして作業を続行したければ、Sを入力してください。"
jp_echo "セーブされたルートファイルシステムは`pwd`/rd_root.$$.saveにあります。"
eng_echo "If you want to save the tree of root image and restart,enter S." 
eng_echo "It is saved to `pwd`/rd_root.$$.save."
echo
jp_echo "その他の値を入力すると作業を再開します。"
eng_echo "Others,the operation restarts.Simply press Enter."
echo

read remain

case $remain in

    "R" | "r" ) jp_echo "作業は完了しました。"
		eng_echo "The operation finished."

		rd_exit 0

		;;

    "S" | "s" ) cp -a $rd_root rd_root.$$.save
		jp_echo "ルートファイルシステムは保存されました、作業を再開します。"
		eng_echo "The tree of root image was saved,and the operation restarted."

		;;

	     *) jp_echo "作業を再開します。"
		eng_echo "The operation restarts."
	     
		;;

esac

umount $rd_root
rm -fr $rd_root
rm -f ${rd_img}.gz
gzip -9 -f $rd_img


	    echo
	    jp_echo "ルートディスクイメージの作成が完了しました。"
	    eng_echo "Root disk image was created."

	    if [ "$def_libs_val" = 1 ];then

		jp_echo "ただし、共有ライブラリに依存性の問題が残っています。"
		eng_echo "but library deficiency remains."
		echo

	    fi

	    jp_echo "${rd_img}.gzの大きさは`ls -l ${rd_img}.gz|awk '{print $5}'`Bです。"
	    eng_echo "${rd_img}.gz is `ls -l ${rd_img}.gz|awk '{print $5}'`B."

		if [ `ls -l ${rd_img}.gz|awk '{print $5}'` -gt 1474560 ]&&[ "$jail_val" != 1 ];then

		    jp_echo "${rd_img}.gzは1474560Bより大きいです。おそらく、ddは失敗するでしょう。"
		    eng_echo "${rd_img}.gz is bigger than 1474560B,maybe dd fails."

		elif [ "$jail_val" = 1 ];then

		    jp_echo "作業は完了しました。"
		    eng_echo "The operation was completed."

		    exit 0

		fi

	    echo
	    jp_echo "/dev/fd0にフロッピーディスクを準備して、何も入力せずEnterを押してください。"
	    jp_echo "もし、作業を中断したいときは何か値を入力してください。"
	    eng_echo "Set floppy disk on /dev/fd0, and simply press Enter."
	    eng_echo "If you set any values,the operation is aborted."

	    read dummy

	    if [ "$dummy" ];then

		jp_echo "作業を終了します。"
		eng_echo "The operation was aborted."
		exit 0

	    fi

	    jp_echo "作業を続行します。ddの実行を開始します"
	    eng_echo "The operation has continued,and dd started."

	    dd if=${rd_img}.gz of=/dev/fd0 ||{ jp_echo "ddの実行に失敗しました、終了します。"
    eng_echo "dd failed,aborted."
				    exit 1
				   }

jp_echo "作業は完了しました。"
eng_echo "The operation was completed."

exit 0

#* Sat Jul 1 2006 yoshinobu syouzi <yoshino@rita.karing.jp>
#- udev support was added.
#- version was up to 0.2.5-pre1 (kotori).
#
#* Sat Jul 1 2006 yoshinobu syouzi <yoshino@rita.karing.jp>
#- many bugs were fixed.
#- version was up to 0.2.4-rc1 (kotori).
#
#* Fri Nov 26 2004 yoshinobu syouzi <yoshino@rita.karing.jp>
#- many bugs were fixed and added iso option.
#- version was up to 0.18a (miharu).
#
#* Fri Jun 18 2004 yoshinobu syouzi <yoshino@rita.karing.jp>
#- changed as libraries and commands were copied to original location.
#- version was up to 0.17a (miharu).
#
#* Sat May 22 2004 yoshinobu syouzi <yoshino@rita.karing.jp>
#- implemention of halt finished on 0.16e (nemu).
#
#* Mon May 10 2004 yoshinobu syouzi <yoshino@rita.karing.jp>
#- init system was changed for 0.15 (nemu).
#
#* Tue Jul 8 2003  yoshinobu syouzi <yoshino@rita.karing.jp>
#- router option was added since 0.13.
#
#* Mon jun 30 2003 yoshinobu syouzi <yoshino@rita.karing.jp>
#- auto detection of shared library was added since 0.11.
#
#* Sat jun 28 2003 yoshinobu syouzi <yoshino@rita.karing.jp>
#- first public release 0.02.

