aboutsummaryrefslogtreecommitdiff
path: root/scripts/mkinstall2
blob: e23e44609d806ccce985b5967d615b64562969c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
#!/bin/bash
#
# /sbin/mkinstall - System Installer - version 1.9.0 - 2003-06-16
# This file is part of the Formilux project : http://formilux.ant-computing.com/
#
# Copyright (C) 2001-2003 Benoit Dolez & Willy Tarreau
#       mailto: benoit@ant-computing.com,willy@ant-computing.com
#
# This program is licenced under GPLv2 ( http://www.gnu.org/licenses/gpl.txt )

cdlist=( )
names=( )
sizes=( )
parts=( )

# this utility uses a few temporary files :
#
# /tmp/fsmnt  : <device><space><mount_point>
#    device=/dev/hda, /dev/sda1...
#    space=exactly one space
#    mount_mount=/home, /var, /, ..., mbr, cdrom, swap, <empty string when not affected>
# /tmp/fsid   : <device><space><hex_id><space><os_name>
# /tmp/fstype : <device><space><fstype>
# /tmp/fslist : <device>
# /tmp/fssize : <device><space><size_in_kB>
# /tmp/sourcepkg : symlink to the source packages directory

# give it a number suffixed with a k,M,G,T, and it will return it reduced to
# the nearest power of 1024 suffixed with k,M,G or T
humansize() {
   local pow val
   local -a suff=( "" k M G T P )
   case "${1##[0-9]*}" in
      k) pow=1;;
      M) pow=2;;
      G) pow=3;;
      T) pow=4;;
      P) pow=5;;
      *) pow=0;;
   esac
   val=${1%%[a-zA-Z]*}
   # we don't return values lower than 10 when possible
   while [ $val -ge 10240 ]; do
      ((pow++))
      ((val/=1024))
   done  
   REPLY=$val${suff[$pow]}
   echo -n $REPLY
}

detect_cdrom() {
    cdlist=($(grep -i '^drive name:' /proc/sys/dev/cdrom/info |cut -f2 -d:))
    CDROMS="${cdlist[*]}"
}

detect_disks() {
    local i j major minor size name rest

    names=( )
    sizes=( )
    parts=( )

    rm -f /tmp/partitions
    tail +3 /proc/partitions | sort -k4.1 >/tmp/partitions
    while read major minor size name rest; do
	if ((i >= 0)); then
	    names[i]=$name
	    sizes[i]=$size
	    parts[i]=1
	fi
	((i++))
    done < /tmp/partitions
    
    i=0
    while ((i < ${#names[@]})); do
    if [ -n "${names[i]}" ]; then
	j=0 
	while ((j < ${#names[@]})); do
	    if [ $i -ne $j -a -n "${names[j]}" -a -z "${names[j]##${names[i]}*}" ]; then
		names[j]=""
		((parts[i]+=${parts[j]}))
	    fi
	    ((j++))
	done
    fi
    ((i++))
    done

    i=0 j=0
    while ((i < ${#names[@]})); do
	if [ -n "${names[i]}" -a "${cdlist[*]##${names[i]}}" = "${cdlist[*]}" ]; then
	    names[j]=${names[i]}
	    sizes[j]=${sizes[i]}
	    ((parts[j]=${parts[i]}-1))
	    ((j++))
	fi
	
	if ((i >= j)); then
	    unset names[i] parts[i] sizes[i]
	    names=("${names[@]}") parts=("${parts[@]}") sizes=("${sizes[@]}")
	else
	    ((i++))
	fi
    done
    # now we have the list of the hard disks :
    #   names[] contains their name in /dev
    #   sizes[] contains their size in blocks (1 kB)
    #   parts[] contains their number of partitions
}

# This function collects some information in /proc and other places.
# It should be called ASAP.

function init {
    #umount /mnt/cdrom >/dev/null 2>&1 # to avoid stupid recursive copying !
    detect_cdrom
    detect_disks
    mkdir -p $ROOTDIR >/dev/null 2>&1
}

# displays the main menu
# if an arg is passed, it will be used as the name entry of the default action.
# it returns the name of the entry in REPLY
# returns 0 if OK, 1 if Cancel/Exit.
function menu_main {
    REPLY=$(dialog --title " Formilux Installation Utility " --no-cancel --no-shadow ${1:+--default-item $1 }\
	   --menu "Installation Menu\n\n   Press <Up>/<Down> to select an item.\n   Press <Tab> to move between [OK] and [Cancel].\n   Press <Enter> to confirm your choice." 24 80 14 \
	  "PART" "Configure Partitions and File Systems" \
          "SOURCE" "Select Packages Source" \
	  "PKG" "Select which Packages to Install" \
	  "CONFIG" "Build Configuration" \
	  "SAVE" "Save Current Configuration" \
	  "LILO" "Run LILO" \
	  "UNMOUNT" "Unmount all File Systems" \
	  "SHELL" "Get Access to a Shell" \
	  "EXIT" "Exit this Utility" 2>&1 | grep -v EXIT)
    [ -n "$REPLY" ]
}


# presents a list of hard disks and lets the user choose one.
# the default one can be passed in $1
# returns 0 if OK, 1 if Cancel/Exit.
function menu_harddisk {
    local -a args=( )
    local i=0
    local model
    while ((i < ${#names[@]})); do
	model='unknown model'
	[ -e "/proc/ide/${names[i]}/model" ] && model=$(</proc/ide/${names[i]}/model)
	args=("${args[@]}" "${names[i]}" "$model, $(humansize ${sizes[i]}k)B, ${parts[i]} part.")
	((i++))
    done
	
    REPLY=$(dialog --title " Formilux Installation Utility " --no-shadow  ${1:+--default-item $1 } --menu "\nSelect a Hard Disk to Configure\n\n   Press <Up>/<Down> to select an item.\n   Press <Tab> to move between [OK] and [Cancel].\n   Press <Enter> to confirm your choice." 24 80 12 "${args[@]}" "EXIT" "Exit this menu"  2>&1 |grep -v EXIT)
    [ -n "$REPLY" ]
}

# presents a list of  hard disks partitions and lets the user choose one.
# the disk is passed in $1
# the default one can be passed in $2
# returns 0 if OK, 1 if Cancel/Exit.
function menu_partition {
    local -a args=( )
    local dev=$1
    local disk=${1##/dev/}
    local def=$2
    local mnt
    local device size id syst rest

    for device in $(grep "^$dev" /tmp/fslist 2>/dev/null); do
	size=$(grep "^$device " /tmp/fssize 2>/dev/null |cut -f2 -d' ')
	mnt=$(grep "^$device " /tmp/fsmnt 2>/dev/null |cut -f2 -d' ')
	id=$(grep "^$device " /tmp/fsid 2>/dev/null |cut -f2 -d' ')
	syst=$(grep "^$device " /tmp/fsid 2>/dev/null |cut -f3 -d' ')
	type=$(grep "^$device " /tmp/fstype 2>/dev/null |cut -f2 -d' ')
	if [ -z "$mnt" ]; then
	    def=${def:-${device##/dev/}}
	    mnt="not assigned"
	else
	    mnt="assigned to $mnt"
	fi
	if [ -z "$type" ]; then
	    def=${def:-${device##/dev/}}
	    type="not formated"
	else
	    type="type $type"
	fi
	args=("${args[@]}" "${device##/dev/}" "$(humansize ${size}k)B, Id 0x$id($syst), $type, $mnt")
    done

    def=${def:-EXIT}
    args=("${args[@]}" "CLEAR" "Clear ALL partitions on this disk" "FDISK" "Run fdisk on this disk")

    REPLY=$(dialog --title " Formilux Installation Utility " --no-shadow  ${def:+--default-item $def} --menu "\nSelect a Partition\n\n   Press <Up>/<Down> to select an item.\n   Press <Tab> to move between [OK] and [Cancel].\n   Press <Enter> to confirm your choice." 24 80 12 "${args[@]}" "EXIT" "Exit this menu"  2>&1 |grep -v EXIT)
    [ -n "$REPLY" ]
}

# reads the partition table on a disk and updates the associated files :
#   /tmp/fslist, /tmp/fssize, /tmp/fsid, /tmp/fsmnt, /tmp/fstype
# the disk device is passed in $1 (/dev/xxx). There's no return code.
function disk_read_part {
    local dev=$1
    local disk=${1##/dev/}
    local device begin end cyl size id syst rest file

    rm -f /tmp/sfdisk.$disk
    for file in fslist fssize fsid; do
	grep -v "^$dev" /tmp/$file 2>/dev/null >/tmp/${file}-; mv /tmp/${file}- /tmp/$file
    done

    for file in fsmnt fstype; do
	grep "^$dev" /tmp/$file 2>/dev/null >/tmp/$file.$disk
	grep -v "^$dev" /tmp/$file 2>/dev/null >/tmp/${file}-; mv /tmp/${file}- /tmp/$file
    done

    sfdisk -l $dev | tr '*' ' ' | grep '^/dev/' >/tmp/sfdisk.$disk
    while read device begin end cyl size id syst rest; do
	# let's ignore empty and extended partitions
	[ "$id" = "0" -o "$id" = "5" ] && continue
	size=${size%+}
	echo "$device" >> /tmp/fslist
	echo "$device $size" >> /tmp/fssize
	echo "$device $id $syst" >> /tmp/fsid
	if [ "$id" != "82" ] || grep -q "^$device " /tmp/fsmnt.$disk && ! grep -q "^$device swap" /tmp/fsmnt.$disk; then
	    grep "^$device " /tmp/fsmnt.$disk >> /tmp/fsmnt
	    grep "^$device " /tmp/fstype.$disk >> /tmp/fstype
	else
	    echo "$device swap" >> /tmp/fsmnt
	    # we want to keep a trace of the format status of the swap partition
	    # instead of assuming it has already been formated
	    grep "^$device " /tmp/fstype.$disk >> /tmp/fstype
	    #echo "$device swap" >> /tmp/fstype
	fi
    done < /tmp/sfdisk.$disk
    rm -f /tmp/sfdisk.$disk
}

# presents a list of possible actions for a partition :
# - change its mount point
# - format it
# the partition is passed in $1
# if a default choice should be proposed, its name must be in $2
# returns 0 if OK, 1 if Cancel/Exit.
function menu_setupfs {
    local -a args=( )
    local dev=$1
    local def size type strext2s strext2l strext3 strreiser
    local mnt

    def=$2
    mnt=$(grep "^$dev " /tmp/fsmnt 2>/dev/null |cut -f2 -d' ')
    [ -z "$mnt" ] && { mnt="not assigned" ; def=${def:-"MNT"}; }

    type=$(grep "^$dev " /tmp/fstype 2>/dev/null |cut -f2 -d' ')
    [ -z "$type" ] && type="not formated"

    size=$[ ($(grep "^$dev " /tmp/fssize 2>/dev/null | cut -f2 -d' ')+0) / 1024 ]

    args=("MNT" "Change the mount point ($mnt)")

    if [ "$mnt" = "swap" ]; then
	args=("${args[@]}" "SWAP" "* mkswap $dev (format as swap)")
	args=("${args[@]}" "SWAPC" "  mkswap -c $dev (format and check)")
	def=${def:-SWAP}
    else
	strext2s="mke2fs -b 1024 -m 1 -s 1 $dev (format as small Ext2)"
	strext2l="mke2fs -b 4096 -s 1 $dev (format as large Ext2)"
	strext3="mke2fs -b 4096 -s 1 -j $dev (format as Ext3)"
	strreiser="mkreiserfs -h r5 $dev (format as ReiserFS)"
	if [ $size -lt 64 ]; then
	    strext2s="* $strext2s"
	    strext2l="  $strext2l"
	    strext3="  $strext3"
	    strreiser="  $strreiser"
	    def=${def:-EXT2S}
	else
	    strext2s="  $strext2s"
	    strext2l="  $strext2l"
	    strext3="* $strext3"
	    strreiser="  $strreiser"
	    def=${def:-EXT3}
	fi

	args=("${args[@]}" "EXT2S" "$strext2s" "EXT2L" "$strext2l" "EXT3" "$strext3" "REISER" "$strreiser" )
    fi
    args=("${args[@]}" "SHELL" "Get Access to a Shell")
    
    REPLY=$(dialog --title " Formilux Installation Utility " --no-shadow  ${def:+--default-item $def} --menu "\nFormat and Mount Point\n\nChoose a mount point for this partition, then select a format command to format it and set its type. The '*' indicates the recommended format.\n\n   Press <Up>/<Down> to select an item.\n   Press <Tab> to move between [OK] and [Cancel].\n   Press <Enter> to confirm your choice." 24 80 8 "${args[@]}" "EXIT" "Exit this menu"  2>&1 |grep -v EXIT)
    [ -n "$REPLY" ]
}


# lets the user change a mount point for a partition.
# the partition is passed in $1
# returns 0 if something changed, else 1.
function menu_mount_point {
    local mnt dev type id name
    dev=$1
    mnt=$(grep "^$dev " /tmp/fsmnt 2>/dev/null |cut -f2 -d' ')
    id=$(grep "^$dev " /tmp/fsid 2>/dev/null |cut -f2 -d' ')
    name=$(grep "^$dev " /tmp/fsid 2>/dev/null |cut -f3 -d' ')
    type=$(grep "^$dev " /tmp/fstype 2>/dev/null |cut -f2 -d' ')

    REPLY=$mnt
    [ -z "$REPLY" -a "$id" = "82" ] && REPLY="swap"
    [ -z "$REPLY" ] && ! grep -q " /boot$" /tmp/fsmnt && REPLY="/boot"
    [ -z "$REPLY" ] && ! grep -q " /$" /tmp/fsmnt && REPLY="/"
    [ -z "$REPLY" ] && ! grep -q " /var$" /tmp/fsmnt && REPLY="/var"
    [ -z "$REPLY" ] && ! grep -q " /home$" /tmp/fsmnt && REPLY="/home"

    REPLY=$(dialog --title " Formilux Installation Utility " --no-shadow --inputbox "Enter the mount point for partition $dev\n${id:+Partition Id=$id($name)}${type:+${id:+, }Formated as $type}\nEnter 'swap' to force it to become swap\n\n   Press <Tab> to move between [OK] and [Cancel].\n   Press <Enter> to confirm your choice." 12 60 $REPLY 2>&1 || echo $mnt)
    if [ "$REPLY" != "$mnt" ]; then
	(grep -v "^$dev " /tmp/fsmnt 2>/dev/null; echo "$dev $REPLY") >/tmp/fsmnt-
	mv /tmp/fsmnt- /tmp/fsmnt
	return 0
    else
	return 1
    fi
}


# lets the user change the package directory
# the entry is checked to be a valid directory
# returns 0 if something changed, else 1.
function menu_pkg_dir {
    local old

    old=$(readlink /var/flx-pkg 2>/dev/null)
    REPLY=$old
    while : ; do
	REPLY=$(dialog --title " Formilux Installation Utility " --no-shadow --inputbox "Enter the new packages directory\nCurrently set to '$old'\n\n   Press <Tab> to move between [OK] and [Cancel].\n   Press <Enter> to confirm your choice." 12 60 $REPLY 2>&1 || echo $old)
	if [ "$REPLY" != "$old" ]; then
	    if [ -d "$REPLY/." ]; then
		PKGDIR=$REPLY
		rm -f /var/flx-pkg
		ln -sf $REPLY /var/flx-pkg
		return 0
	    else
		dialog --title " Formilux Installation Utility " --msgbox "'$REPLY' is not a valid directory. Please try again, or press [Cancel].\n" 8 60
	    fi
	else
	    # cancel or no changes
	    return 1
	fi
    done
}

# ask the user about the desired source of packages for the installation
function menu_select_source {
    local -a args=( )
    args=("CDROM" "CD-ROM" "DISK" "Hard disk or USB storage device" "NFS" "Remove file-system over NFS" "DIR" "Pre-mounted directory")
    REPLY=$(dialog --title " Formilux Installation Utility " --no-shadow --menu "\nSource Packages Selection\n\nChoose from which medium the source packages will be fetched.\n\n   Press <Up>/<Down> to select an item.\n   Press <Tab> to move between [OK] and [Cancel].\n   Press <Enter> to confirm your choice." 24 80 8 "${args[@]}" "EXIT" "Exit this menu"  2>&1 |grep -v EXIT)
    [ -n "$REPLY" ]
}


# ask the user about the desired source cdrom
# returns 0 if the CD could be mounted, 1 otherwise.
function menu_select_cdrom {
    local -a args=( )
    local cdrom

    umount -l /mnt/cdrom >/dev/null 2>&1
    rm -f /dev/cdrom
    rm -f /var/flx-pkg
    PKGDIR=
    case ${#cdlist[@]} in
	0)
	    dialog --title " Formilux Installation Utility " --msgbox "No CD-ROM device was detected on this system. Please load manually any appropriate module or choose another source medium." 12 60 ;
	    rm -f /var/flx-pkg
	    return 1
	    ;;
	1)
	    ln -sf ${cdlist[0]} /dev/cdrom
	    if mount /mnt/cdrom >/dev/null 2>&1 ; then
		PKGDIR=/mnt/cdrom/pkg
		ln -s $PKGDIR /var/flx-pkg
		dialog --title " Formilux Installation Utility " --msgbox "/dev/${cdlist[0]} has been selected as the only CD-ROM device, and mounted successfully.\n" 8 60
		return 0
	    else
		dialog --title " Formilux Installation Utility " --msgbox "/dev/${cdlist[0]} has been selected as the only CD-ROM device, but could not be mounted. Please insert a valid CD-ROM and try again.\n" 8 60
		return 1
	    fi
	    ;;
    esac

    args=("AUTO" "Automatically find which drive contains a CD")
    for cdrom in "${cdlist[@]}"; do
	if [ -e "/proc/ide/$cdrom/model" ]; then
	    args=("${args[@]}" "$cdrom" "$(cat /proc/ide/$cdrom/model)")
	elif [ -z "${cdrom##sr*}" -o -z "${cdrom##scd*}" ]; then
	    args=("${args[@]}" "$cdrom" "SCSI CD-ROM")
	else
	    args=("${args[@]}" "$cdrom" "Unknown CD-ROM model")
	fi
    done

    cdrom="${args[0]}"
    while [ -z "$PKGDIR" ]; do
	rm -f /dev/cdrom
	REPLY=$(dialog --title " Formilux Installation Utility " --no-shadow --default-item "$cdrom" --menu "\nCD-ROM\n\nSelect the CD-ROM drive which contains the Formilux CD-ROM you want to use for the installation.\n\n   Press <Up>/<Down> to select an item.\n   Press <Tab> to move between [OK] and [Cancel].\n   Press <Enter> to confirm your choice." 24 80 8 "${args[@]}" "EXIT" "Exit this menu"  2>&1 |grep -v EXIT)
	cdrom=$REPLY
	case "$REPLY" in
	    "")
		return 1
		;;
	    AUTO)
		findcdrom
		if [ ! -e /dev/cdrom ]; then
		    dialog --title " Formilux Installation Utility " --msgbox "No drive containing a valid CD-ROM was found. Please insert a valid CD-ROM and try again.\n" 8 60
		    continue
		fi
		;;
	    *)
		ln -sf $REPLY /dev/cdrom
		;;
	esac
	if mount /mnt/cdrom >/dev/null 2>&1; then
	    PKGDIR=/mnt/cdrom/pkg
	    dialog --title " Formilux Installation Utility " --msgbox "/dev/$(readlink /dev/cdrom) has been selected as the new CD-ROM device, and mounted successfully.\n" 8 60
	    return 0
	else
	    dialog --title " Formilux Installation Utility " --msgbox "/dev/$(readlink /dev/cdrom) has been selected as the new CD-ROM device, but could not be mounted. Please insert a valid CD-ROM and try again.\n" 8 60
	fi
    done
    return 1
}


# ask the user a question to which he can reply by "yes" or "no".
# A simple message is passed in $1
# if $2 is NOT EMPTY, the default response will be NO.
# returns 0 if No, 1 if Yes
function menu_yesno {
    dialog --title " Formilux Installation Utility " --no-shadow  ${2:+--defaultno} --yesno "$1" 12 65
}

# user interface functions
function ask {
        echo -n "$1 [$2] : "; read 
        if [ "$REPLY" = "" ]; then REPLY=$2; fi
}

function yesno {
        echo -n "$1 [$2] : "; read ; REPLY=`echo $REPLY | cut -c1`
        if [ "$REPLY" = "" ]; then REPLY=$2; fi
        if [ "$REPLY" = o -o "$REPLY" = y -o "$REPLY" = Y ]; then
           REPLY="y"
        fi
}

#
function check_directories {
    if [ ! -d $ROOTDIR/etc/. ] ; then
	if [ -d $ROOTDIR/boot/etc/. ] ; then 
	    ln -s boot/etc $ROOTDIR/etc
	else 
	    echo "Can't find 'etc' directory neither in '/' nor in '/boot'"
	    return 1
	fi
    fi
    if [ ! -d $ROOTDIR/var/. ] ; then 
	mkdir $ROOTDIR/var ; fi
    if [ ! -d $ROOTDIR/var/tmp ] ; then 
	mkdir $ROOTDIR/var/tmp ; chmod 1777 $ROOTDIR/var/tmp ; fi
    if [ ! -d $ROOTDIR/var/run ] ; then 
	mkdir $ROOTDIR/var/run ; fi
    if [ ! -d $ROOTDIR/var/spool ] ; then 
	mkdir $ROOTDIR/var/spool ; fi
    if [ ! -d $ROOTDIR/var/log ] ; then 
	mkdir $ROOTDIR/var/log ; fi
    if [ ! -d $ROOTDIR/var/cache ] ; then 
	mkdir $ROOTDIR/var/cache ; fi
    if [ ! -d $ROOTDIR/var/adm/. ] ; then 
	ln -s log $ROOTDIR/var/adm ; fi

    # /tmp should exist, either as a dir or a link.
    if [ ! -e $ROOTDIR/tmp ] ; then 
	mkdir $ROOTDIR/tmp ; chmod 1777 $ROOTDIR/tmp ; fi

    return 0
}

# this function lists all hard drives found, but hides CDROMs
function list_disks {
    echo "${names[@]}"
    #for i in `tail +3 /proc/partitions | awk '{print $4}' | grep '\([a-zA-Z]$\)\|\(c[0-9]*d[0-9]*$\)'`; do
    #	 if ! echo $CDROMS | grep -qw $i; then
    #	 echo $i
    #	 fi
    #done
}

# mounts all the new file-systems, and creates the mount points when needed
function mount_new_fs {
    local -a mplist=($(sort -k2.1 /tmp/fsmnt 2>/dev/null|cut -f2 -d' ') )
    local fs mp type

    rm -f /tmp/fstab

    echo "/proc		/proc		proc	defaults			0 0" >> /tmp/fstab
    echo "#/dev		/dev		tmpfs	size=0,nr_inodes=4096,remount	0 0" >> /tmp/fstab
    echo "#/var/empty	/var/empty	tmpfs	size=0,nr_inodes=1,ro,remount	0 0" >> /tmp/fstab
    echo "/dev/pts	/dev/pts	devpts	defaults			0 0" >> /tmp/fstab
    echo "/dev/cdrom	/mnt/cdrom	iso9660	defaults,noauto,ro,users	0 0" >> /tmp/fstab

    for mp in "${mplist[@]}"; do
	fs=$(grep " $mp\$" /tmp/fsmnt 2>/dev/null|cut -f1 -d' ')
	type=$(grep "^$fs " /tmp/fstype 2>/dev/null|cut -f2 -d' ')
	if [ "$mp" != "swap" ]; then
	    mkdir -p -m 0755 $ROOTDIR$mp 2>/dev/null
	    mount -t $type $fs $ROOTDIR$mp
	fi
	if [ "$mp" = "/" ]; then
	    echo "#$fs	$mp	$type	defaults,ro			1 1" >> /tmp/fstab
	    echo "/dev/root	$mp	$type	defaults,ro			1 1" >> /tmp/fstab
	    mkdir -p -m 0755 $ROOTDIR/dev $ROOTDIR/proc $ROOTDIR/etc
	    mount -t proc proc $ROOTDIR/proc  # may be needed for compressed files
	elif [ "$mp" = "/boot" ]; then
	    echo "$fs	$mp		$type	defaults,ro			1 2" >> /tmp/fstab
	elif [ "$mp" = "swap" ]; then
	    echo "$fs	$mp		$type	defaults			0 0" >> /tmp/fstab
	else
	    echo "$fs	$mp		$type	defaults			1 2" >> /tmp/fstab
	fi
    done

}

# unmounts all the new file-systems
function unmount_new_fs {
    local -a mplist=($(sort -rk 2.1 /proc/mounts 2>/dev/null | cut -f2 -d' ' | grep "^$ROOTDIR") )
    local fs mp type

    for mp in "${mplist[@]}"; do
	if [ "$mp" != "swap" ]; then
	    umount $mp >/dev/null 2>&1
	fi
    done
}


function setup_part {

    if [ -e /tmp/devmap -a -e /tmp/fstab ]; then
	echo "Filesystems are currently configured this way :"
	for i in `cut -f1 -d' ' /tmp/devmap`; do grep "^$i[ 	]" /tmp/fstab | awk '{print $1 " => " $2 }'; done
	echo "-- end of list --"
	yesno "Do you want to change something (y/n) ?" "n"
	[ "$REPLY" = "y" ] || return 0
	cp /tmp/fstab /tmp/fstab-
	cp /tmp/devmap /tmp/devmap-
    else
	touch /tmp/fstab- /tmp/devmap-
    fi

    # clean temporary files
    rm -rf /tmp/temp.$$ /tmp/fstab /tmp/devmap

    # unmount previously mounted file-systems
    for part in $(grep " $ROOTDIR" /proc/mounts|awk '{print $2}'|sort -r); do
	umount $part;
    done

    echo "/proc		/proc		proc	defaults			0 0" >> /tmp/fstab
    echo "#/dev		/dev		tmpfs	size=0,nr_inodes=4096,remount	0 0" >> /tmp/fstab
    echo "/dev/pts	/dev/pts	devpts	defaults			0 0" >> /tmp/fstab
    echo "/dev/cdrom	/mnt/cdrom	iso9660	defaults,noauto,ro,users	0 0" >> /tmp/fstab

    echo "/dev/$(readlink /dev/cdrom) /dev/cdrom" >> /tmp/devmap 
    (grep -v ' CDROM$' /tmp/mounts 2>/dev/null; echo "/dev/$(readlink /dev/cdrom) CDROM") >/tmp/mounts- && mv /tmp/mounts- /tmp/mounts

    # scan all disk
    # for disk in $(list_disks); do
    REPLY=${names[0]}
    while menu_harddisk $REPLY; do
	disk=${REPLY##/dev/}

   # look for all detected partitions
#   for spart in `sfdisk -l /dev/$disk 2>/dev/null | grep "^/" |tr '*' ' '| cut -f3- -d'/' | awk '{print $1 ":" $6 }'` ; do


	REPLY=
	while { disk_read_part /dev/$disk ; menu_partition /dev/$disk $REPLY; } do
	    [ -z "$REPLY" ] && break
	    if [ "$REPLY" = "CLEAR" ] ; then
		if menu_yesno "Warning !\n\nThis will erase the partition table for /dev/$disk.\nAll data will be lost.\n\nDo you still want to proceed ?" N; then
		    dd if=/dev/zero of=/dev/$disk bs=1024 count=1 > /dev/null 2>&1
		    (echo o ; echo w ) | fdisk /dev/$disk > /dev/null 2>&1
		fi
		REPLY=FDISK
		continue
	    elif [ "$REPLY" = "FDISK" ]; then
		fdisk /dev/$disk
		REPLY=
		continue
	    else
		echo "menu format/mount pour $REPLY"
	    fi





     # partition name
     part=`echo $spart|cut -f1 -d:`
     set -- `echo $spart | tr ':' ' '`
     set -- $* `fdisk -s /dev/$1` `sfdisk -l /dev/$disk 2>/dev/null | grep "/dev/$1 " | cut -c15-`

     # hide mounted partitions
     part=/dev/$part
     if grep -q "$part " /proc/mounts > /dev/null ; then continue ; fi

     # hide empty or extended partitions
     if [ $2 = 0 -o $2 = 5 ] ; then continue ; fi

     # partition size in Mega Bytes
     size=$[ $3 / 1024]
     # partition type in human readable form
     type=$2
     shift 8
     htype=$* 
     
     # redo while not a good value
     askagain=y
     unset mountpoint
     while [ "$askagain" = "y" ] ; do
       askagain=n
       echo 
       echo "Partition : $part ($htype, ${size} MB)"

       # non linux partition
       if [ $type != 82 -a $type != 83 -a $type != 8e -a $type != fd ] ; then 
         yesno "$part isn't a standard linux partition, would you like an entry in /etc/fstab (y/n)?" "n"
         if [ "$REPLY" = "n" ] ; then continue ; fi
	 fstype=noauto
       else
         fstype=auto
       fi
 
       # getting mount point
       if [ $type = 82 ] ; then
         mountpoint=swap
       else
         mountpoint=$(grep "^$part[ 	]" /tmp/fstab- | awk '{print $2 }')
       fi
       ask "  Choose a mount point (swap,/,/boot,/var,...) or [Enter] for none" "$mountpoint"
       # remove useless leading and trailing slashes
       mountpoint=$(echo $REPLY|sed 's@^\([/]*\)\(/.*\)$@\2@'|sed 's@^\(.*[^/]\)\([/]*\)$@\1@')
       if [ -z "$mountpoint" ] ; then break ; fi

       case "$mountpoint" in
         /)     echo "$part /dev/root" >> /tmp/devmap ;;
         /boot) echo "$part /dev/boot" >> /tmp/devmap ;;
         swap)  echo "$part /dev/swap" >> /tmp/devmap ;
		echo "$part	swap	swap	defaults	0 0" >> /tmp/fstab;;
         *)     echo "$part /dev/fs/$(echo ${mountpoint#/} |tr '/' '.')" >> /tmp/devmap ;;
       esac

       
	case "$mountpoint" in
	    swap)
		(grep -v "^$part " /tmp/mounts 2>/dev/null; echo "$part SWAP") >/tmp/mounts- && mv /tmp/mounts- /tmp/mounts;;
	    *)
		(grep -v "^$part " /tmp/mounts 2>/dev/null; echo "$part $mountpoint") >/tmp/mounts- && mv /tmp/mounts- /tmp/mounts;;
	esac


       formatcmd=''
       # format linux partitions
       if [ $type = 82 -o $type = 83 -o $type = 8e -o $type = fd ] ; then 
         # tell how to format
         echo "  0: -- DO NOT format this partition (default) --" ; ch=0
         if [ "$mountpoint" = "swap" ] ; then 
           ch1="mkswap $part" ; echo " >1: $ch1"
           ch2="mkswap -c $part" ; echo "  2: $ch2 (with check)"
	 elif [ "`echo $mountpoint|cut -c1`" = "/" ] ; then
           ch1="mke2fs -b 1024 -m 1 -s 1 $part"
           if [ "$size" -lt 64 ]; then
              echo " >1: $ch1 (small ext2)"
           else
              echo "  1: $ch1 (small ext2)"
           fi
           ch2="mke2fs -b 4096 -s 1 $part" ; echo "  2: $ch2 (large ext2)"
           ch3="mke2fs -b 4096 -s 1 -j $part"
           if [ "$size" -ge 64 ]; then
              echo " >3: $ch3 (ext3)"
           else
              echo "  3: $ch3 (ext3)"
           fi
           ch4="mkreiserfs -h r5 $part" ; echo "  4: $ch4 (reiserfs)"
         fi
         ask "  Select a format method or enter the complete command" "$ch"
         case $REPLY in
           0)     formatcmd='' ;;
           [1-9]) formatcmd=`eval echo '\$ch'$REPLY` ;;
           *)     formatcmd=$REPLY ;;
         esac
       fi
     done
     
     # execute format
     if [ "$formatcmd" ] ; then
       echo -n "Doing: '$formatcmd': "
       eval "echo y | $formatcmd" > /tmp/error.$$ 2>&1
       if [ "$?" = "0" ] ; then 
         echo "done."
       else 
         echo "failed."
         echo "--------------------------------------------------------"
         echo "$formatcmd said:"
         sed -e "s/^/>> /" /tmp/error.$$
         echo "----------- Filesystem will not be mounted -------------"
         mountpoint=""
       fi 
     fi
     if [ "`echo $mountpoint|cut -c1`" = "/" ] ; then
       echo "$mountpoint -t $fstype $part" >> /tmp/temp.$$
     fi
   done 
 done

 # now we will mount all the filesystems we have just created, and add them to fstab
 sort /tmp/temp.$$ | while read ; do
   set -- $REPLY
   mp=$1 ; shift
   if [ ! -d $ROOTDIR$mp ] ; then mkdir -p $ROOTDIR$mp ; echo "Creating directory $mp" ; fi
   if [ $2 != noauto ] ; then 
     echo -n "Mounting $mp ($3): "
     mount -n $* $ROOTDIR$mp > /dev/null 2>&1 
     if [ "$?" = "0" ] ; then echo "done." ; else
       echo "failed."
       echo "##failed## $3	$mp	$2	defaults	1 1" >> /tmp/fstab
     fi
   else 
     echo "#$3	$1      	auto		defaults	0 0" >> /tmp/fstab
   fi
 done
 mount | grep '^[ 	]*/' | grep "$ROOTDIR[/ 	]" | sed "s@$ROOTDIR[/]*@/@" | \
   awk '{ printf "%s\t%s\t%s\tdefaults%s\t1 %s\n",$1,$3,$5,($3=="/" || $3=="/boot")?",ro":"",($3=="/")?"1":"2" }' >> /tmp/fstab
 #grep '^/' /proc/swaps | awk '{ print $1 "\tswap       \tswap       \tdefault	0 0"}' >> /tmp/fstab

 mkdir -p -m 0755 $ROOTDIR/dev $ROOTDIR/proc $ROOTDIR/etc $ROOTDIR/mnt/cdrom
 mount -t proc proc $ROOTDIR/proc  # may be needed for compressed files

### sed -e "s,^#L.*cdrom\$,ln ` readlink /dev/cdrom` /dev/cdrom," < /.preinit > $ROOTDIR/.preinit

 rm -f /tmp/temp.$$
 return 0
}

function setup_keyboard {
 sed -n '/^service keyboard/,/^ *$/p' < /etc/config.rc
}

function setup_mouse {
 sed -n '/^service mouse/,/^ *$/p' < /etc/config.rc
}

function setup_network {
 echo "# hostname formilux"
 echo "# interface eth0"
 echo "#    ip address 10.101.20.4/16"
 echo "#    ip dhcp"
 echo "# service network"
}

# (re)build config.rc, startup.rc, fstab, devmap, lilo.conf
function setup_config {
  local MNT="$1"
  local MPWD=$PWD
  local TMP=/tmp/newconf

  if [ ! -w /tmp ] ; then
    echo "Directory /tmp not writable"; exit 1
  fi

  mkdir -p $TMP >/dev/null 2>&1

  if [ "$MNT" = "" ] ; then MNT="$ROOTDIR"; fi

# copy startup.rc and config.rc
  rm -f $TMP/config.rc $TMP/startup.rc
  if [ "$UPDATE_ONLY" != "y" -o ! -e $MNT/etc/config.rc ]; then
    echo           >> $TMP/config.rc
    echo "# service syslogd" >> $TMP/config.rc
    echo           >> $TMP/config.rc
    setup_network  >> $TMP/config.rc
    echo           >> $TMP/config.rc
    setup_keyboard >> $TMP/config.rc
    setup_mouse    >> $TMP/config.rc
    chmod 644 $TMP/config.rc
  fi

  if [ "$UPDATE_ONLY" != "y" -o ! -e $MNT/etc/startup.rc ]; then
    echo "#!/bin/sh" >> $TMP/startup.rc
    echo "echo -n \"Default configuration ... \" " >> $TMP/startup.rc
    echo "/sbin/init.d/sysprofiles /etc/config.rc" >> $TMP/startup.rc
    chmod 755 $TMP/startup.rc
  fi

# copy fstab
  cp /tmp/fstab $TMP/fstab

# copy devmap
  BOOTDEV=${BOOTDEV:-`echo $ROOTDEV | sed 's/[0-9]\+$//'`}
  ask "Choose device on which LILO will be installed " $BOOTDEV
  BOOTDEV=$REPLY
  grep -v '/dev/\(mbr\|cdrom\)$' /tmp/devmap > $TMP/devmap 2>/dev/null
  echo "$BOOTDEV /dev/mbr" >> $TMP/devmap
  echo "/dev/$(readlink /dev/cdrom) /dev/cdrom" >> $TMP/devmap 


# build a new lilo.conf 
  cd $MNT

  KERNVER=`ls boot/*/bzImage | tail -1 | cut -f2 -d/`
  KERNLBL=`echo $KERNVER | tr -d '.-'`
  cat > $TMP/lilo.conf <<EOF
serial = 0,9600n8
boot = $BOOTDEV
default = l$KERNLBL
EOF

  for bootimg in `ls boot/*/bzImage` ; do
    echo "image = /$bootimg" >> $TMP/lilo.conf
    echo "  label = l`echo $bootimg | cut -f2 -d/ | tr -d '.-'`" >> $TMP/lilo.conf
    echo "  append = \"root=$ROOTDEV\"" >> $TMP/lilo.conf
    echo "  read-only" >> $TMP/lilo.conf
  done

  if [ `grep "label =" $TMP/lilo.conf | wc -l` = 0 ]; then
    echo
    echo "WARNING!!! no kernel was found in $MNT/boot !!!"
    echo "You must install a kernel and edit $TMP/lilo.conf or your system will never boot."
    echo
    sleep 1
  fi

# copy /.preinit
  if [ -e $MNT/.preinit -a "$UPDATE_ONLY" = "y" ]; then
    cp $MNT/.preinit $TMP/.preinit
  else
    cp /.preinit $TMP/.preinit
  fi

  # 1st complicated part : merge old and new fstab.
  # Method:
  #  - mount points only in old fstab are kept as-is
  #  - mount points in both with same types only have their dev updated
  #  - mount points in both with diff types are fully updated
  #  - mount points only in new fstab are merged
  if [ "$UPDATE_ONLY" = "y" -a -e $MNT/etc/fstab ]; then
    rm -f $TMP/fstab
    while read; do 
      set -- $REPLY
      if ! grep -q "^[^ 	]\+[ 	]\+${2}[ 	]" /tmp/fstab; then
        # not found
	echo "$REPLY" >> $TMP/fstab
      elif ! grep -q "^[^ 	]\+[ 	]\+${2}[ 	]\+${3}[ 	]" /tmp/fstab; then
        # not found with the same type
        echo "##old##$REPLY" >> $TMP/fstab 
        grep "^[^ 	]\+[ 	]\+${2}[ 	]" /tmp/fstab >> $TMP/fstab
      else
        # found with the same type. We may have to update the device
        dev=`grep "^[^# 	]\+[ 	]\+${2}[ 	]" /tmp/fstab|head -1|awk '{print $1}'`
        if [ "$dev" -a "$dev" != "$1" ]; then
           echo "##old##$REPLY" >> $TMP/fstab 
           echo "$dev	$2	$3	$4	$5 $6" >> $TMP/fstab  # keep options
        else
           echo "$REPLY" >> $TMP/fstab  # was commented or the same
        fi
      fi 
    done < $MNT/etc/fstab

    # add new entries
    while read; do 
      set -- $REPLY
      if ! grep -q "^[^ 	]\+[ 	]\+${2}[ 	]" $TMP/fstab; then
	echo "$REPLY" >> $TMP/fstab
      fi 
    done < /tmp/fstab
  fi

  # try to update lilo.conf

  if [ -e $MNT/etc/lilo.conf -a "$UPDATE_ONLY" = "y" ]; then
    rm -f $TMP/lilo.conf
    if grep -q "^image.*$KERNVER" $MNT/etc/lilo.conf; then
      # the latest kernel is referenced. We can use this conf.
      (echo "boot = $BOOTDEV" ; grep -v "^boot[ 	]*=[ 	]*$BOOTDEV" $MNT/etc/lilo.conf | sed 's/^\(boot[ 	]*=\)/##old##\1/') > $TMP/lilo.conf
    else
      # the latest kernel is missing from lilo.conf.
      (echo "boot = $BOOTDEV" ; grep -v "^boot[ 	]*=[ 	]*$BOOTDEV" $MNT/etc/lilo.conf | sed 's/^\(boot[ 	]*=\)/##old##\1/') | sed -e 's:^\(image.*=[^/]*/boot/\)\([^/]*\)\(/.*\)$:\1'$KERNVER'\3:' > $TMP/lilo.conf
    fi
  fi
 
  # now, we may have to update .preinit to create aliased entries.
  # we remove all references to devices that have been updated

  grep -v "^[#]*\(ln\|L\).*[ 	]\+\($(echo `cut -f2 -d' ' $TMP/devmap`|sed 's@ @\\|@g')\)" $TMP/.preinit > $TMP/.preinit-
  grep -v ' /dev/fs/' $TMP/devmap | sed 's@^/dev/@ln @' >> $TMP/.preinit-
  if grep -q ' /dev/fs' $TMP/devmap; then
    if ! grep -q "^\(md\|M\)[ 	]\+/dev/fs" $TMP/.preinit-; then
       echo "md /dev/fs 755" >> $TMP/.preinit-
    fi
    grep ' /dev/fs/' $TMP/devmap | sed 's@^/dev@ln ..@' >> $TMP/.preinit-
  fi
  mv $TMP/.preinit- $TMP/.preinit ; chmod 711 $TMP/.preinit

  rm -f $TMP/passwd
  if [ "$UPDATE_ONLY" != "y" -o ! -e $MNT/etc/passwd ]; then
    sed -e 's@/home/admin@/root@' < /etc/passwd |grep -v '^\(squid:\|pdnsd:\|flx:\|install:\)' > $TMP/passwd
    chmod 644 $TMP/passwd
  fi

  # if /var is on its own filesystem, /tmp must be a true filesystem too.
  if grep -q ' \(/dev/fs/var\|/dev/fs/var\.tmp\)$' $TMP/devmap; then
    if ! grep -q "^[^ 	]\+[ 	]\+/tmp[ 	]" $TMP/fstab; then
      echo "/tmp		/tmp	tmpfs	defaults	0 0" >> $TMP/fstab
    fi 
  fi 
       
  cd $MPWD
  return 0
}

function apply_config {
  local MNT=$1
  local TMP=/tmp/newconf

  [ -e $TMP/.preinit ]   && (rm -f $MNT/.preinit       ; cp -p $TMP/.preinit   $MNT)
  [ -e $TMP/passwd ]     && (rm -f $MNT/etc/passwd     ; cp -p $TMP/passwd     $MNT/etc)
  [ -e $TMP/lilo.conf ]  && (rm -f $MNT/etc/lilo.conf  ; cp -p $TMP/lilo.conf  $MNT/etc)
  [ -e $TMP/fstab ]      && (rm -f $MNT/etc/fstab      ; cp -p $TMP/fstab      $MNT/etc)
  [ -e $TMP/config.rc ]  && (rm -f $MNT/etc/config.rc  ; cp -p $TMP/config.rc  $MNT/etc)
  [ -e $TMP/startup.rc ] && (rm -f $MNT/etc/startup.rc ; cp -p $TMP/startup.rc $MNT/etc)

  # replace /tmp with a directory if needed
  if grep -q "^[^ 	]\+[ 	]\+/tmp[ 	]" $TMP/fstab; then
    if [ -L $MNT/tmp ]; then rm -f $MNT/tmp && mkdir -m 1777 $MNT/tmp >/dev/null 2>&1; fi
  fi 
  return 0
}

function do_lilo {
  if [ -e $ROOTDIR/dev -a -e $ROOTDIR/.preinit -a -e $ROOTDIR/etc/lilo.conf ]; then
    chroot $ROOTDIR /.preinit rebuild
    chroot $ROOTDIR /sbin/lilo || lilo -r $ROOTDIR
    umount $ROOTDIR/dev  # remove what .preinit has installed
  else
    echo "LILO has NOT been run because it needs /dev, /.preinit, and /etc/lilo.conf"
  fi
  return 0
}



# main

ROOTDIR=/var/mount
PKGDIR=/home/pkg
SHELLDIR=/

init

if [ $0 = "mkdisk" ] ; then setup_part && exit 0 ; exit 1 ; 
elif [ $0 = "mkconfig" ] ; then setup_config $1 && exit 0 ; exit 1 ; 
elif [ $0 = "doconfig" ] ; then apply_config $1 && exit 0 ; exit 1 ; 
fi

end=0
while [ $# -ge 1 -a "$end" -ne 1 ] ; do
  case $1 in
    --disk) setup_part;;
    --config) setup_config $2; shift;;
    --apply) apply_config $2; shift;;
    --) end=1;;
    *) end=1;; 
  esac
  if [ "$end" -eq 1 ] ; then break ; fi
  shift
done

action="PART"
while [ -n "$action" ] ; do
  
    ROOTDEV=$(mount | grep " on $ROOTDIR " | cut -f1 -d' ')
  
#  echo
#  echo "Possible actions are :"
#  echo "1: Run fdisk"
#  echo "2: Setup filesystems"
#  echo "3: Install packages"
#  echo "4: Build running config"
#  echo "5: Save built config for next reboot"
#  echo "6: Run lilo"
#  echo "7: Unmount all"
#  echo "s: Shell"
#  echo "q: Quit"
#
#  ask "Select an action (1..7/s/q)" "$action"

    menu_main $action

    [ -z "$REPLY" ] && break

    if [ "$REPLY" = "SHELL" ] ; then (cd $SHELLDIR; echo -n "Current directory : "; pwd; bash -i); REPLY=$action; continue; fi

    case $REPLY in
    PART) 
      # unmount previously mounted file-systems
      #for part in $(grep " $ROOTDIR" /proc/mounts|awk '{print $2}'|sort -r); do umount $part; done
      unmount_new_fs
      detect_disks
      REPLY=${names[0]}
      while menu_harddisk $REPLY; do
	disk=${REPLY##/dev/}

        #sfdisk -l /dev/$disk
        #yesno "Clear partition table for '/dev/$disk' (y/n)?" "n"
	REPLY=
	while { disk_read_part /dev/$disk ; menu_partition /dev/$disk $REPLY; } do
	    part=$REPLY
	    [ -z "$REPLY" ] && break
	    if [ "$REPLY" = "CLEAR" ] ; then
		if menu_yesno "Warning !\n\nThis will erase the partition table for /dev/$disk.\nAll data will be lost.\n\nDo you still want to proceed ?" N; then
		    dd if=/dev/zero of=/dev/$disk bs=1024 count=1 > /dev/null 2>&1
		    (echo o ; echo w ) | fdisk /dev/$disk > /dev/null 2>&1
		fi
		REPLY=FDISK
	    elif [ "$REPLY" = "FDISK" ]; then
		fdisk /dev/$disk
		REPLY=
	    else
		part=$REPLY
		REPLY=
		while menu_setupfs /dev/$part $REPLY; do
		    case "$REPLY" in
		    SHELL)
			(cd $SHELLDIR; echo -n "Current directory : "; pwd; bash -i)
			(grep -v "^/dev/$part " /tmp/fstype 2>/dev/null; echo "/dev/$part auto") >/tmp/fstype-
			mv /tmp/fstype- /tmp/fstype
			# goes automatically to default format
			REPLY=
			;;
		    MNT)
			menu_mount_point /dev/$part
			# goes automatically to default format
			REPLY=
			;;
		    SWAP)
			mkswap /dev/$part
			(grep -v "^/dev/$part " /tmp/fstype 2>/dev/null; echo "/dev/$part swap") >/tmp/fstype-
			mv /tmp/fstype- /tmp/fstype
			REPLY=EXIT
			;;
		    SWAPC)
			mkswap -c /dev/$part
			(grep -v "^/dev/$part " /tmp/fstype 2>/dev/null; echo "/dev/$part swap") >/tmp/fstype-
			mv /tmp/fstype- /tmp/fstype
			REPLY=EXIT
			;;
		    EXT2S)
			mke2fs -b 1024 -m 1 -s 1 /dev/$part
			(grep -v "^/dev/$part " /tmp/fstype 2>/dev/null; echo "/dev/$part ext2") >/tmp/fstype-
			mv /tmp/fstype- /tmp/fstype
			REPLY=EXIT
			;;
		    EXT2L)
			mke2fs -b 4096 -s 1 /dev/$part
			(grep -v "^/dev/$part " /tmp/fstype 2>/dev/null; echo "/dev/$part ext2") >/tmp/fstype-
			mv /tmp/fstype- /tmp/fstype
			REPLY=EXIT
			;;
		    EXT3)
			mke2fs -b 4096 -s 1 -j /dev/$part
			(grep -v "^/dev/$part " /tmp/fstype 2>/dev/null; echo "/dev/$part ext3") >/tmp/fstype-
			mv /tmp/fstype- /tmp/fstype
			REPLY=EXIT
			;;
		    REISER)
			mkreiserfs -h r5 /dev/$part
			(grep -v "^/dev/$part " /tmp/fstype 2>/dev/null; echo "/dev/$part reiserfs") >/tmp/fstype-
			mv /tmp/fstype- /tmp/fstype
			REPLY=EXIT
			;;
		    esac
		    # this is done to exit right after an mkfs
		    [ "$REPLY" = "EXIT" ] && break;
		done
		REPLY=
	    fi
	done
	# things might have changed !
	detect_disks
	REPLY=/dev/$disk
      done 
      # we now have to mount all these file systems
      mount_new_fs
      SHELLDIR=/
      action=SOURCE
      ;;
    SOURCE) 
	action=SOURCE
	while menu_select_source; do
	    case "$REPLY" in
		CDROM)
		    menu_select_cdrom && break;
		    ;;
		DISK)
		    menu_select_disk
		    ;;
		NFS)
		    menu_select_nfs
		    ;;
		DIR)
		    menu_select_dir
		    ;;
	    esac
	done
	PKGDIR=$(readlink /var/flx-pkg 2>/dev/null)
	[ -d "/var/flx-pkg/." ] && action=PKG
	;;
    PKG) 
	cd /
	if [ "$ROOTDEV" != "" ] ; then
	    #ask "Packages directory, or files pattern ($PKGDIR, $PKGDIR/*.lst, *.prf, *.tgz)"$'\n'"   " $PKGDIR ; PKGDIR="$REPLY"
	    #if [ -d "$PKGDIR" ]; then
	    #  list=`cd $PKGDIR && ls *.prf *.lst *.tgz 2>/dev/null`
	    #else
	    #  list=`basename "$PKGDIR"`
	    #  PKGDIR=`dirname "$PKGDIR"`
	    #  list=$(cd $PKGDIR && echo $list)
	    #fi
	    menu_pkg_dir
	    list=( ); idx=0
	    for pack in $(shopt -s nullglob ; cd $PKGDIR && echo *.{prf,tgz}); do
		list[idx++]=$pack
		list[idx++]=""
		if [ -z "${pack##[0-9][0-9][0-9]_*}" ]; then
		    # pre-selects every numbered package
		    list[idx++]="on"
		else
		    list[idx++]="off"
		fi
	    done

	    list=( $(dialog --title " Formilux Installation Utility " --no-shadow \
		--checklist "Packages Selection\n\n   Press <Space> to select/deselect a package.\n   Press <Up>/<Down> to select an item.\n   Press <Tab> to move between [OK] and [Cancel].\n   Press <Enter> to install your selection." 24 80 12 "${list[@]}" 2>&1) )
	
	    # dialog returns quoted words
	    eval list=( "${list[@]}" )
	    [ ${#list[@]} -gt 0 ] || break
	
	    for pack in "${list[@]}" ; do
		case "$pack" in
		    *tgz)
			[ -d "$ROOTDIR" ] || mkdir -p -m 0755 $ROOTDIR
			tar zpxf $PKGDIR/$pack -C $ROOTDIR ;;
		    *prf)
			flxextract -R $ROOTDIR -i $PKGDIR/$pack ;;
		esac
	    done 
	    check_directories || exit 1
	    SHELLDIR=/
	    action=CONFIG
	else
	    action=PART
	fi
	;;
    CONFIG) 
	check_directories || exit 1
	if [ "$UPDATE_ONLY" != "n" -a "$UPDATE_ONLY" != "y" ]; then
	echo "Existing files can be REPLACED or UPDATED."
	    yesno "Do you prefer to UPDATE existing files (y/n) ?" "y"
	    UPDATE_ONLY="$REPLY"
	fi
	setup_config $ROOTDIR 
	echo "done."
	echo "New files have been written to /tmp/newconf/"
	echo "You can check them now by starting a shell."
	SHELLDIR=/tmp/newconf
	(cd $SHELLDIR ; ls -lart)
	action=SAVE
	;;
    SAVE)
	echo "Saving config (.preinit, startup.rc, config.rc, fstab, lilo.conf, passwd)"
	apply_config $ROOTDIR 
	echo "Saving config ... done."
	echo "Updating dynamic files (/etc/formilux/sig.dat, /etc/ld.so.cache) ... "
	ldconfig -r $ROOTDIR > /dev/null 2>&1 
	
	for SYSTEMMAP in `echo $ROOTDIR/boot/System.map-*`; do
	    KERNVER=${SYSTEMMAP#$ROOTDIR/boot/System.map-}
	    chroot $ROOTDIR depmod -a -F /boot/System.map-$KERNVER $KERNVER
	done
	
	echo "Signing the filesystem... (this may take a while)"
	cd $ROOTDIR ; signfs -x `mount|grep -v ' type \(tmpfs\|ramfs\)'|grep "^/.* $ROOTDIR "| \
	    sed "s@^.*$ROOTDIR\([^ ]*\) .*@.\1@"` > /etc/formilux/sig.dat
	echo "Updating dynamic files ... done."
	SHELLDIR=/
	action=LILO
	;;
    LILO)
	do_lilo
	SHELLDIR=/
	action=UNMOUNT
	;;
    UNMOUNT)
	cd /
	unmount_new_fs
	umount -l /mnt/cdrom >/dev/null 2>&1
	SHELLDIR=/
	action=EXIT
	;;
    *) 
	;;
  esac
done