File: install.sh

package info (click to toggle)
eso-midas 23.02pl1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 146,968 kB
  • sloc: ansic: 360,674; makefile: 6,231; sh: 6,003; pascal: 535; perl: 40; awk: 36; sed: 14
file content (1399 lines) | stat: -rw-r--r-- 33,881 bytes parent folder | download | duplicates (7)
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
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
#!/bin/sh
# @(#)install.sh	19.1 (ESO-IPG) 02/25/03 14:31:59
#
#  Installation script for MIDAS from CD-ROM with binaries.
#
#set -x

###########################################################################
#
#  Initialize all the read-only variables used by this script.  Mark
#  them readonly so they don't get accidentally modified
#
###########################################################################

# Determine how to set prompt
if [ "`echo -n a`" = a ]; then
	# BSD echo
	NNN=-n 
	CCC=        
else
	# USG echo
	NNN= 
	CCC='\c'      
fi
readonly NNN CCC

# Most of this needs to updated for each new CD.
MIDAS_SOURCES="95novpl2.0.tar.z"
MIDVERS="95NOV"
DATE='February 1996'
COPYDATE="1996"
RELEASE="95NOVpl2.0"

RETURN_MSG="RETURN TO MAIN MENU"

readonly MIDAS_SOURCES MIDVERS DATE COPYDATE RETURN_MSG RELEASE

STATE_CD=1
STATE_ARCH=2
STATE_MIDASHOME=3
STATE_PROD_SELECT=4
STATE_INSTALL=5
STATE_SETUP=6
STATE_QUIT=Q

readonly STATE_CD STATE_ARCH STATE_MIDASHOME STATE_PROD_SELECT 
readonly STATE_INSTALL STATE_QUIT STATE_SETUP

PROD_BIN="MIDAS binary"
PROD_SRC="MIDAS sources"
PROD_GUI="MIDAS statically-linked GUIs"
PROD_DEMO="MIDAS demo data"
PROD_CALIB="MIDAS calibration data"

readonly PROD_BIN PROD_SRC PROD_GUI PROD_DEMO PROD_CALIB

ARCH_ALPHA="Alpha OSF"
ARCH_HP700="HP9000 Series 700/800"
ARCH_IBM="IBM RS6000 AIX"
ARCH_SGI5="SGI IRIX 5"
ARCH_SUNOS="SPARC Sunos 4.1.x"
ARCH_SOLARIS="SPARC Solaris 5.x"
ARCH_LINUX="PC LINUX 1.2.13"

REL_ALPHA="OSF1"
REL_HP700="HP-UX"
REL_IBM="AIX"
REL_SGI5="IRIX"
REL_SUNOS="SunOS_4.1.3"
REL_SOLARIS4="SunOS_5.4"
REL_SOLARIS5="SunOS_5.5"
REL_LINUX="Linux"

readonly ARCH_ALPHA ARCH_HP700 ARCH_IBM 
readonly ARCH_SGI5 ARCH_SUNOS ARCH_SOLARIS ARCH_LINUX
readonly REL_ALPHA REL_HP700 REL_IBM 
readonly REL_SGI5 REL_SUNOS REL_SOLARIS4 REL_SOLARIS5 REL_LINUX

ARCH_LIST="$ARCH_ALPHA,$ARCH_HP700,$ARCH_IBM"
ARCH_LIST="$ARCH_LIST,$ARCH_SGI5,$ARCH_SUNOS,$ARCH_SOLARIS,$ARCH_LINUX"

readonly ARCH_LIST

# Script sets these up during run (all begin with _)
MIDAS_CDDIR=
MIDAS_ARCH=
MIDAS_PROD_LIST=
MIDAS_PROD_SELECTED=
MIDAS_PROD_INSTALLED=
MIDAS_STATE=
MIDAS_SETUP=
MIDAS_BINDIR_DONE=
MIDAS_LIBDIR_DONE=
MIDAS_MANDIR_DONE=
MIDAS_ARCH_PREFIX=
MIDAS_MANDIR=/usr/man/manl
MIDAS_BINDIR=/usr/bin
MIDAS_LIBDIR=/usr/lib
MIDAS_SHID=3.1
#######################################################################
#
#  Generic support routines
#
#######################################################################

aborted()
{
	#  Catch user-generated interrupts, and quit

	echo ""
	echo "**** INSTALLATION ABORTED BY USER ****"
	echo ""
	exit 1
}
trap aborted 1 2 3

killed()
{
	#  Catch attempts to kill this process

    echo ""
    echo "**** INSTALLATION ABORTED ABNORMALLY ****"
    echo ""
    exit 1
}
trap killed 15

error()
{
	#  standard error message

	echo '***' $* > /dev/tty
	false
}

fatal_error()
{
	#  Print error message and die; this should never happen

	error $*
	kill -15 $$
}

change_ifs()
{
	#
	#  Change IFS to $1, saving previous value in $OFS.
	#  NOTE: this is not a stack!!  Do not nest calls!!

	if [ "$IFS" != "$1" ]; then
		OIFS="$IFS"
		IFS="$1"
	fi
}

restore_ifs()
{
	#  Restore the old value of IFS

	IFS="$OIFS"
}

array()
{
	#
	#  mimic the behavior of arrays; pick the $2'th element out of $1
	#  uses 1-indexing
	#

	vector="$1"
	rindex="$2"

	index=1
	change_ifs ","
	for elt in $vector; do
		if [ "$index" -eq "$rindex" ]; then
			echo "$elt"
			restore_ifs
			return
		fi
		index=`expr $index + 1`
	done

	restore_ifs
	fatal_error "Array selection failed for $vector[$rindex]"
}

necho()
{
	#
	#  A safe way to perform echo without sending a new-line
	#

	echo $NNN "$*" $CCC > /dev/tty
}

ask_return()
{
	#  Simply get the user to hit the return key

	necho 'Hit <return> to continue ...'
	read x
}

ask_yn()
{
	#
	#  Get a yes/no answer from the user; $1 is the prompt, $2 the default
	#

	prompt="$1"
	def="${2:-y}"		# yes is the default if not supplied
	while true; do
		necho "$prompt [$def]: "
		read answer
		answer="${answer:-$def}"
		case "$answer" in
			[yY])
				answer=y
				break
				;;
			[nN])
				answer=n
				break
				;;
			  *)
				error Please enter "'y'" or "'n'".
				;;
		esac
	done
	[ $answer = y ]
}

ask_response()
{
	#
	#  Get a (non-zero) response from the user
	#  $1 is the question, $2 is the default (if any).
	#

	question="$1"
	default="$2"
	while true; do
		if [ -z "$default" ]; then
			necho "$question: "
		else
			necho "$question [$default]: "
		fi
		read answer
		if [ -z "$answer" ]; then
			answer=$default
		fi
		if [ -z "$answer" ]; then
			error Please enter a non-empty string.
		else
			break
		fi
	done
	echo $answer
}

difference()
{
	#
	#  Calculate and return the list of all elements in $1, but not
	#  in $2.  This calculates set difference the hard way.
	#

	l1="$1" 
	l2="$2"
	l3=

	change_ifs ","
	for i in $l1; do
		if in_list "$l2" "$i"; then
			echo > /dev/null		# do nothing
		else
			l3=`addto "$l3" "$i"`
		fi
	done
	restore_ifs

	echo "$l3"
}

addto()
{
	#
	#  Add element $2 to list $1; return the result
	#

	ll="$1"
	el="$2"

	change_ifs ","
	if in_list "$ll" "$el"; then
		echo > /dev/null		# do nothing
	else
		if [ -z "$ll" ]; then
			ll="$el"
		else
			ll="$ll,$el"
		fi
	fi
	restore_ifs

	echo "$ll"
}

genlist()
{
	#  generate a list of numbers from $1..$2

	echo $1 $2 | awk '{
		for (i=$1; i<=$2; i++) list=list "," i
		print substr(list,2)
	}'
}

count()
{
	#  count the number of entries in $1

	echo $1 | awk -F, '{ print NF }'
}

in_list()
{
	#
	#  Determine whether $2 is in the list $1; $1 is a comma-separated
	#  list of valid values
	#

	found=f
	list="$1"
	arg="$2"
	change_ifs ","
	for i in $list
	do
		if [ "$i" = "$arg" ]; then
			found=t
			break
		fi
	done

	restore_ifs
	[ $found = t ]
}

ask_list()
{
	# 
	#  Ask the user for a value in list $2; the prompt is $1, and
	#  the default value is $3
	#

	prompt="$1"
	default="$3"
	list="$2"

	while true; do
		num=`ask_response "$prompt" "$default"`
		if in_list "$list" $num; then
			break
		else
			error "$num is not one of the choices."
		fi
	done

	echo $num
}

ask_multi()
{
	#
	#  Allow a multiple selection, by number.  $1 is the prompt
	#  to use, $2 is the list of valid inputs
	#

	prompt="$1"
	list="$2"

	while true; do
		pselect=`ask_response "$prompt"`
		err=f
		change_ifs ","
		for num in $pselect; do
			if in_list "$list" $num; then
				true
			else
				error "$num is not a valid choice"
				err=t
				break
			fi
		done
		restore_ifs
		if [ "$err" != "t" ]; then
			break
		fi
	done
	echo $pselect
}

valid_dir()
{
	#  Determine if $1 is a valid directory specification
	#  This means it must begin with a '/'

	if [ "`expr "$1" : '\(/\).*'`" != "/" ]; then
		error "Directory name must begin with a '/'"
	fi

	return $?
}

exists_dir()
{
	#  Determine whether directory $1 exists.  It must also
	#  be a valid directory (see above).

	dname=$1

	if [ -d "$dname" ]; then
		valid_dir $dname
	else
		error "Directory \"$dname\" does not exist"
	fi
	return $?
}

#######################################################################
#
#  Pre- and Post- installation banners
#
#######################################################################

banner()
{
#  Banner before the script begins.  Put trademarks, disclaimers,
#  etc. in here.

clear > /dev/tty
echo "                                                                    " > /dev/tty
echo "     ********************************************************************" > /dev/tty
echo "                       $RELEASE release of ESO-MIDAS " > /dev/tty
echo "               Copyright (C) $COPYDATE European Southern Observatory" > /dev/tty
echo "     This program is free software; you can redistribute it and/or modify" > /dev/tty
echo "     it under the terms of the GNU General Public License as published by" > /dev/tty
echo "     the Free Software Foundation; either version 2 of the License, or   " > /dev/tty
echo "     (at your option) any later version.                                 " > /dev/tty
echo "                                                                      " > /dev/tty
echo "     This program is distributed in the hope that it will be useful, but " > /dev/tty
echo "     WITHOUT ANY WARRANTY; without even the implied warranty of          " > /dev/tty
echo "     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    " > /dev/tty
echo "     General Public License for more details.                            " > /dev/tty
echo "                                                                      " > /dev/tty
echo "     You should have received a copy of the GNU General Public License   " > /dev/tty
echo "     along with this program; if not, write to the Free Software         " > /dev/tty
echo "     Foundation,  Inc., 675 Massachusetss Ave, Cambridge, MA 02139, USA. " > /dev/tty
echo "     Corresponding concerning ESO-MIDAS should be addressed as follows:  " > /dev/tty
echo "     Internet e-mail: midas@eso.org                                      " > /dev/tty
echo "     Postal address: European Southern Observatory                       " > /dev/tty
echo "                     Data Management Division                            " > /dev/tty
echo "                     Karl-Schwarzschild-Strasse 2                        " > /dev/tty
echo "                     D-85748 Garching bei Muenchen. GERMANY              " > /dev/tty
echo "     ********************************************************************" > /dev/tty
}

banner1()
{
#  Banner before completing MIDAS installation
  clear > /dev/tty
#  echo "" > /dev/tty
  echo "  Complete the ESO-MIDAS installation means:" > /dev/tty
  echo "" > /dev/tty
  echo "  1. Create a soft link from /midas to $MIDASHOME" >/dev/tty
  echo "  2. Copy the inmidas, gomidas and helpmidas scripts from" > /dev/tty
  echo "     $MIDASHOME/$MIDVERS/system/unix to e.g.: /usr/bin" > /dev/tty
  echo "  3. Copy inmidas.l, gomidas.l and helpmidas.l man-pages from" > /dev/tty 
  echo "     $MIDASHOME/$MIDVERS/system/unix/manl to e.g. /usr/man/manl" >/dev/tty
  echo "  4. Create the system references to MIDAS shared libraries located in" >/dev/tty
  echo "       $MIDASHOME/$MIDVERS/lib" >/dev/tty
  echo "" > /dev/tty
  echo "  If you can not become superuser to run these tasks you can, as a normal" > /dev/tty 
  echo "  user, run MIDAS by setting the following environment variables and" > /dev/tty
  echo "  executing the inmidas, gomidas and helpmidas scripts located in" > /dev/tty  
  echo "  $MIDASHOME/$MIDVERS/system/unix" > /dev/tty
  echo "" >/dev/tty
  echo "    % setenv MIDASHOME $MIDASHOME" >/dev/tty
  case "$MIDAS_ARCH" in
    $ARCH_HP700)
      echo "    % setenv SHLIB_PATH $MIDASHOME/$MIDVERS/lib" >/dev/tty
      ;;
    *)
      echo "    % setenv LD_LIBRARY_PATH $MIDASHOME/$MIDVERS/lib" >/dev/tty
      ;;
    esac
  echo "    % $MIDASHOME/$MIDVERS/system/unix/inmidas" > /dev/tty
  echo "" >/dev/tty
}

post_install()
{
	# Should do something here??

	echo "                                                                " > /dev/tty
	echo "                                                                " > /dev/tty
}

#######################################################################
#
#  State Querying Functions
#     - isset_*        : only returns status
#     - check_*        : calls isset_* and print message
#     - precond_       : checks if preconditions are met
#     - check_precond_ : checks preconditions, prints message
#
#######################################################################
isset_su()
{
	if  user=`sh -c whoami 2>/dev/null` ; then
		:
	elif  user=`sh -c /usr/bin/whoami 2>/dev/null` ; then
		:
	elif  user=`sh -c /usr/ucb/whoami 2>/dev/null` ; then
		:
	else
		user="$USER"
	fi
	[ "$user" = "root" ]
}

isset_cddir()
{
	[ -n "$MIDAS_CDDIR" ]
}

isset_arch()
{
	[ -n "$MIDAS_ARCH" ]
}

isset_midashome()
{
	[ -n "$MIDASHOME" -a -d "$MIDASHOME" ]
}

isset_product()
{
	[ -n "$MIDAS_PROD_SELECTED" ]
}

isset_prod_installed()
{
	[ -n "$MIDAS_PROD_INSTALLED" ]
}

isset_setup()
{
	[ -f "$MIDASHOME/$MIDVERS/monit/prepa.exe" ]
}

isset_setup_installed()
{
	[ -n "$MIDAS_SETUP" ]
}

#
#  Check functions
#

check_cddir()
{
	isset_cddir ||
		( error "Please enter the CD mount point first (item $STATE_CD)."; 
		  ask_return;
		  false
		)
}

check_arch()
{
	isset_arch ||
		( error "Please select an architecture first (item $STATE_ARCH).";
		  ask_return;
		  false
		)
}

check_setup()
{
	isset_setup ||
		( error "MIDAS binary must be installed first (item $STATE_PROD_SELECT and/or item $STATE_INSTALL)."; 
		  ask_return;
		  false
		)
}

check_midashome()
{
	isset_midashome ||
		( error "Please select the installation directory MIDASHOME first (item $STATE_MIDASHOME).";
		  ask_return;
		  false
		)
}

check_product()
{
	isset_product ||
		( error "Please select the ESO-MIDAS product(s) to install first (item $STATE_PROD_SELECT).";
		  ask_return;
		  false
		)
}

check_prod_installed()
{
	isset_prod_installed || 
		( error "Please install the ESO-MIDAS products first (item $STATE_INSTALL).";
		  ask_return;
		  false
		)
}

#
#  Precondition functions
#

precond_product()
{
	isset_arch && isset_midashome
}

precond_install()
{
	isset_cddir && isset_arch && isset_midashome && isset_product
}

precond_setup()
{
	isset_midashome && isset_product && isset_arch && isset_prod_installed
}

precond_run_password()
{
	isset_midashome && isset_password
}

#
#  Check Precondition functions
#
check_precond_run_password()
{
	check_cddir && check_midashome 
}

check_precond_product()
{
	check_arch && check_midashome
}

check_precond_install()
{
	check_cddir && check_arch && check_midashome && check_product
}

check_precond_setup()
{
	check_midashome && check_arch && check_setup
}

######################################################################
#
#  State setting and querying functions
#
######################################################################

set_state()
{
	#  
	#  Sanity check the state; find the lowest state whose preconditions
	#  are met, and which has not been completed.  Break after two passes.

	pass=1
	while true; do
		case "$MIDAS_STATE" in
			$STATE_CD)
				if isset_cddir; then
					MIDAS_STATE=`expr $MIDAS_STATE + 1`
					continue
				else
					break
				fi
				;;
			$STATE_ARCH)
				if isset_arch; then
					MIDAS_STATE=`expr $MIDAS_STATE + 1`
					continue
				else
					break
				fi
				;;
			$STATE_MIDASHOME)
				if isset_midashome; then
					MIDAS_STATE=`expr $MIDAS_STATE + 1`
					continue
				else
					break
				fi
				;;
			$STATE_PROD_SELECT)
				if precond_product; then
					if isset_product || isset_prod_installed; then
						MIDAS_STATE=`expr $MIDAS_STATE + 1`
						continue
					fi
					break
				fi
				;;
			$STATE_INSTALL)
				if precond_install; then
					break
				elif isset_prod_installed; then
					MIDAS_STATE=`expr $MIDAS_STATE + 1`
					continue
				fi
				;;
			$STATE_SETUP)
				if precond_setup; then
					break
				fi
				;;
			$STATE_QUIT)
				break
				;;
		esac
		if [ "$pass" -gt 1 ]; then
			break
		fi

		pass=`expr $pass + 1`
		MIDAS_STATE=1
	done
}

show_state()
{
	#  Display the current state.  Prints just about everything the user
	#  needs to know about the installation process.

	if isset_cddir || isset_arch || isset_midashome; then
		# one of these must be set for anything to be displayed
		echo > /dev/null
	else
		return
	fi

	echo '***************************************************************************' > /dev/tty
		echo "  ESO-MIDAS Release MIDVERS:           $MIDVERS"       > /dev/tty
	if isset_cddir; then
		echo "  CD mount directory:                  $MIDAS_CDDIR" > /dev/tty
	fi
	if isset_arch; then
		echo "  Target Architecture:                 $MIDAS_ARCH" > /dev/tty
	fi
	if isset_midashome; then
		echo "  Installation Directory MIDASHOME:    $MIDASHOME"       > /dev/tty
	fi
	if isset_product; then
		echo "  Products selected for installation:   "       > /dev/tty
		ctr=1
		change_ifs ","
		for i in $MIDAS_PROD_SELECTED; do
			echo "              ${ctr}. ${i}.         " > /dev/tty
			ctr=`expr $ctr + 1`
		done
		restore_ifs
	fi
	if isset_prod_installed; then
		echo "  Products installed:   "       > /dev/tty
		ctr=1
		change_ifs ","
		for i in $MIDAS_PROD_INSTALLED; do
			echo "              ${ctr}. ${i}.         " > /dev/tty
			ctr=`expr $ctr + 1`
		done
		restore_ifs
	fi
	echo '***************************************************************************' > /dev/tty
}

#######################################################################
#
#  Main Action Routines
#
#######################################################################

build_prodlist()
{
	#
	#  Build a list of products supported on chosen architecture
	#

	MIDAS_PROD_LIST="$PROD_SRC"
	case "$MIDAS_ARCH" in
		$ARCH_SUNOS|$ARCH_LINUX)
		    if [ -n "$MIDAS_PROD_LIST" ]; then
			MIDAS_PROD_LIST="$MIDAS_PROD_LIST,$PROD_BIN,$PROD_GUI,$PROD_DEMO,$PROD_CALIB"
		    else
			MIDAS_PROD_LIST="$PROD_BIN,$PROD_GUI,$PROD_DEMO,$PROD_CALIB"
		    fi
			;;
		$ARCH_HP700|$ARCH_SOLARIS|$ARCH_ALPHA|$ARCH_SGI5|$ARCH_IBM)
		    if [ -n "$MIDAS_PROD_LIST" ]; then
			MIDAS_PROD_LIST="$MIDAS_PROD_LIST,$PROD_BIN,$PROD_DEMO,$PROD_CALIB"
		    else
			MIDAS_PROD_LIST="$PROD_BIN,$PROD_DEMO,$PROD_CALIB"
		    fi
			;;
		*)
			fatal_error "Unknown architecture $MIDAS_ARCH"
			;;
	esac
}

pick_cddir()
{
	#
	#  Allow the user to specify the directory on which the CD
	#  is mounted.  Usually this is /cdrom.
	#

	clear > /dev/tty

	echo "                                                       " > /dev/tty
	echo "  This installation program must know where the MIDAS  " > /dev/tty
	echo "  CD has been mounted. The usual mount point is /cdrom." > /dev/tty
	echo "                                                       " > /dev/tty
	echo "  Type in 'R' at the prompt below if you want to return" > /dev/tty
	echo "  to the main menu. " > /dev/tty
	echo "                                                       " > /dev/tty

	while true; do
		cd_def=${MIDAS_CDDIR:-/cdrom}
		dir=`ask_response 'Enter the CD mount point' $cd_def`
		if [ "$dir" = "r" -o "$dir" = "R" ]; then
			break
		elif exists_dir "$dir"; then
			ifile=`ls $dir|grep -i install.sh`
			if [ -z "$ifile" ]; then
				error "This directory does not contain any MIDAS products."
			else
				MIDAS_CDDIR=$dir
				break
			fi
		fi
	done

	isset_cddir
}

pick_arch()
{
	#
	#  Select an architecture/toolkit combination
	#

	clear > /dev/tty

	echo "                                          " 	> /dev/tty
	echo "Please select one of the following," > /dev/tty
	echo "or type 'R' to return to the main menu." > /dev/tty
	echo "                                          " 	> /dev/tty
	echo "Architecture" | awk '{ printf("     %-25s\n", $1) }'  > /dev/tty
	echo "============" | awk '{ printf("     %-25s\n", $1) }'  > /dev/tty
	ctr=1
	change_ifs ","
	for i in $ARCH_LIST; do
		arch="$i"
		echo "${ctr}.:${arch}" 
		ctr=`expr $ctr + 1`
	done | awk -F: '{ printf(" %3s %-25s\n", $1, $2) }' > /dev/tty
	echo "  R. $RETURN_MSG." > /dev/tty
	echo "                 " > /dev/tty
	restore_ifs

	# For some reason, ctr=1 after the above for loop; 
	# The pipe seems to cause this
	ctr=`count "$ARCH_LIST"`
	alist=`genlist 1 $ctr`
	alist="$alist,r,R"

	aselect=`ask_list 'Enter your selection' "$alist"`
	case "$aselect" in
		[rR])	;;
		   *)	MIDAS_ARCH=`array "$ARCH_LIST" "$aselect"`
				MIDAS_PROD_SELECTED=""
				MIDAS_PROD_INSTALLED=""
                        case "$MIDAS_ARCH" in
                                $ARCH_ALPHA)    MIDAS_ARCH_PREFIX=alphaosf;;
                                $ARCH_HP700)    MIDAS_ARCH_PREFIX=hp700;;
                                $ARCH_IBM)      MIDAS_ARCH_PREFIX=rs6000;;
                                $ARCH_SGI5)     MIDAS_ARCH_PREFIX=sgirix5;;
                                $ARCH_SUNOS)    MIDAS_ARCH_PREFIX=sparcsun;;
                                $ARCH_SOLARIS)  MIDAS_ARCH_PREFIX=sparcsol;;
                                $ARCH_LINUX)    MIDAS_ARCH_PREFIX=pclinux;;
			esac
				;;
	esac

	isset_arch
}

pick_midashome()
{
	#
	#  Pick the MIDASHOME directory; this is inherited from the environment,
	#  if already set.
	#

	clear > /dev/tty

	echo "                                                           " > /dev/tty
	echo "  All  ESO-MIDAS products  for  the same  architecture     " > /dev/tty
	echo "  are installed in the same directory.  The location of    " > /dev/tty
	echo "  this directory is stored in the environment variable     " > /dev/tty
	echo "  MIDASHOME. You will need to designate the location of    " > /dev/tty
	echo "  this directory, and possibly create it before you can     " > /dev/tty
	echo "  install any ESO-MIDAS products.                                  " > /dev/tty
	echo "                                                           " > /dev/tty
	echo "  Type in 'R' at the prompt below if you want to return    " > /dev/tty
	echo "  to the main menu. " > /dev/tty

	while true; do
		echo "                                                           " > /dev/tty
		echo 'Enter the name of the directory in which to install ESO-MIDAS products.' > /dev/tty
		dir=`ask_response 'MIDASHOME' $MIDASHOME`
		if [ "$dir" = "r" -o "$dir" = "R" ]; then
			break
		else
			if valid_dir "$dir"; then
				if [ ! -d "$dir" ]; then
					echo "Directory $dir does not exist. " > /dev/tty
					if ask_yn "OK to create new directory $dir"; then
						mkdir $dir
						if [ $? -ne 0 ]; then
							error "Unable to create directory $dir."
							continue
						fi
					else
						continue
					fi
				fi
				if ask_yn "OK to install in directory $dir"; then
					MIDASHOME=$dir
					export MIDASHOME
					break
				fi
			fi
		fi
	done

	isset_midashome
}

pick_mandir()
{
  echo "                                                                        " > /dev/tty
  echo "Enter the name of the directory in which to install ESO-MIDAS man-pages," > /dev/tty
  echo "or type in 'R' at the prompt below if you want to skip                  " > /dev/tty 
  echo "                                                                        " > /dev/tty
  while true; do
    dir=`ask_response 'MIDAS_MANDIR' $MIDAS_MANDIR`
      if [ "$dir" = "r" -o "$dir" = "R" ]; then
	break
      else
	if valid_dir "$dir"; then
	  if [ ! -d "$dir" ]; then
	    echo "Directory $dir does not exist. " > /dev/tty
	    continue
	  fi
	if ask_yn "OK to copy ESO-MIDAS man-pages in directory $dir"; then
	  man_suffix=`expr "$dir" : '.*man\(.\)'`
	  for manpage in inmidas gomidas helpmidas readline
	  do 
	    cp $MIDASHOME/$MIDVERS/system/unix/manl/$manpage.l $dir/$manpage.$man_suffix
	    echo "cp $MIDASHOME/$MIDVERS/system/unix/manl/$manpage.l $dir/$manpage.$man_suffix" >/dev/tty
	  done
	  MIDAS_MANDIR_DONE="yes"
	  break
	fi
      fi
    fi
  done
}

pick_bindir()
{
  echo "                                                                      " > /dev/tty
  echo "Enter the name of the directory in which to install ESO-MIDAS scripts," > /dev/tty
  echo "or type in 'R' at the prompt below if you want to skip                " > /dev/tty 
  echo "                                                                      " > /dev/tty
  while true; do
    dir=`ask_response 'MIDAS_BINDIR' $MIDAS_BINDIR`
    if [ "$dir" = "r" -o "$dir" = "R" ]; then
      break
    else
      if valid_dir "$dir"; then
	if [ ! -d "$dir" ]; then
          echo "Directory $dir does not exist. " > /dev/tty
	    continue
	fi
	if ask_yn "OK to copy ESO-MIDAS scripts in directory $dir"; then
	  MIDASHOME_0=`echo MIDASHOME0=$MIDASHOME | sed 's/\//\\\\\//g'`
	  MIDVERS_0=`echo MIDVERS0=$MIDVERS`
 	  for script in inmidas gomidas helpmidas
	  do 
	    cp $MIDASHOME/$MIDVERS/system/unix/$script $dir
	    echo "cp $MIDASHOME/$MIDVERS/system/unix/$script $dir" >/dev/tty
	  done
	  for file in $dir/inmidas $dir/helpmidas
	  do
	    echo "Editing $file ..."
	    cat $file | sed  \
              -e 's/^MIDASHOME0=.*/'$MIDASHOME_0'/' \
              -e 's/^MIDVERS0=.*/'$MIDVERS_0'/' \
              > $file.t
	    chmod +xr $file.t
	    mv -f $file.t $file
	  done
	  MIDAS_BINDIR_DONE="yes"
	  break
        fi
      fi
    fi
  done
}

pick_libdir()
{
  echo "                                                                      " > /dev/tty
  echo "Enter the name of the directory in which to install ESO-MIDAS libraries," > /dev/tty
  echo "or type in 'R' at the prompt below if you want to skip                " > /dev/tty 
  echo "                                                                      " > /dev/tty
  while true; do
    dir=`ask_response 'MIDAS_LIBDIR' $MIDAS_LIBDIR`
    if [ "$dir" = "r" -o "$dir" = "R" ]; then
      break
    else
      if valid_dir "$dir"; then
	if [ ! -d "$dir" ]; then
          echo "Directory $dir does not exist. " > /dev/tty
	    continue
	fi
	if ask_yn "OK to link ESO-MIDAS libraries in directory $dir"; then
 	  for library in libmidas libgmidas
	  do 
	    case "$MIDAS_ARCH" in
		$ARCH_ALPHA|$ARCH_SOLARIS)
		  rm -f $dir/$library.so
		  ln -s $MIDASHOME/$MIDVERS/lib/$library.so $dir/$library.so
		  echo "ln -s $MIDASHOME/$MIDVERS/$library.so $dir/$library.so" >/dev/tty
		  ;;
		$ARCH_SUNOS)
		  rm -f $dir/$library.so.$MIDAS_SHID
		  ln -s $MIDASHOME/$MIDVERS/lib/$library.so.$MIDAS_SHID $dir/$library.so.$MIDAS_SHID
		  echo "ln -s $MIDASHOME/$MIDVERS/lib/$library.so.$MIDAS_SHID $dir/$library.so.$MIDAS_SHID" >/dev/tty
		  /usr/etc/ldconfig
		  ;;
		*)
		  error "No ESO-MIDAS shared libraries expected for $MIDAS_ARCH"
		  return
		  ;;
            esac
	  done
	  MIDAS_LIBDIR_DONE="yes"
	  break
        fi
      fi
    fi
  done
}

pick_product()
{
	#
	#  Pick the list of products to install, based on the architecture
	#

	if check_precond_product; then
		clear > /dev/tty

		build_prodlist

		echo "                                            "  > /dev/tty
		echo "Please select any of the following products."  > /dev/tty
		echo "If you want to install more than one product," > /dev/tty
		echo "enter them in a comma-separated list."		 > /dev/tty
		echo "                                            "  > /dev/tty

		ctr=1
		plist=
		change_ifs ","
		for i in $MIDAS_PROD_LIST; do
			echo " ${ctr}. ${i}.         " > /dev/tty
			plist=`addto "$plist" "$ctr"`
			ctr=`expr $ctr + 1`
		done
		restore_ifs

		echo " A. Install ALL products." > /dev/tty
		echo " R. $RETURN_MSG." > /dev/tty
		plist="$plist,a,A,r,R"
		echo "                         " > /dev/tty
		pselect=`ask_multi 'Enter your list' "$plist"`

		case "$pselect" in
			[rR])	false; return
					;;
			[aA])	# Put all entries in pselect
					j=1
					pselect=
					while [ $j -lt $ctr ]; do
						pselect=`addto "$pselect" "$j"`
						j=`expr $j + 1`
					done
					;;
		esac

		#
		#  go back through $MIDAS_PROD_LIST and create MIDAS_PROD_SELECTED
		#  the list of selected products
		#

		change_ifs ","
		MIDAS_PROD_SELECTED=
		for j in $pselect; do
			ctr=1
			for i in $MIDAS_PROD_LIST; do
				if [ "$ctr" -eq "$j" ]; then
					case "$i" in
						$PROD_BIN)	[ -f $MIDASHOME/$MIDVERS/monit/prepa.exe ];;
						$PROD_SRC)	[ -f $MIDASHOME/$MIDVERS/monit/prepa.c ];;
						$PROD_GUI)	[ -f $MIDASHOME/$MIDVERS/gui/exec/help.exe ];;
						$PROD_DEMO)	[ -f $MIDASHOME/demo ];;
						$PROD_CALIB)	[ -f $MIDASHOME/calib ];;
					esac
					if [ $? -eq 0 ]; then
						echo "                                    " > /dev/tty
						echo "*** Directory $MIDASHOME seems to have" > /dev/tty
						echo "*** product $i already installed." > /dev/tty
						echo "                                    " > /dev/tty
						if ask_yn 'Do you want to overwrite this copy' n; then
							MIDAS_PROD_SELECTED=`addto "$MIDAS_PROD_SELECTED" "$i"`
						fi
					else
						MIDAS_PROD_SELECTED=`addto "$MIDAS_PROD_SELECTED" "$i"`
					fi
					break
				fi
				ctr=`expr $ctr + 1`
			done
		done
		restore_ifs
	fi

	isset_product
}

setup_product()
{
	# 
	# setup. Install links for shared libraries.
	# scripts and man pages.
	#
	if check_precond_setup; then
		clear > /dev/tty
		banner1
		if isset_su ; then
			if ask_yn 'OK to complete MIDAS installation'; then
				pick_mandir
				pick_bindir
        			case "$MIDAS_ARCH" in
                			$ARCH_ALPHA|$ARCH_SUNOS|$ARCH_SOLARIS)
				  		pick_libdir
						;;
					$ARCH_LINUX)
						echo $MIDASHOME/$MIDVERS/lib | cat /etc/ld.so.conf - | sort -u > /etc/ld_so_conf
						mv /etc/ld.so.conf /etc/ld.so.conf~
						mv /etc/ld_so_conf /etc/ld.so.conf
		  				/sbin/ldconfig
	  					MIDAS_LIBDIR_DONE="yes"
					  	;;
					$ARCH_HP700|$ARCH_SGI5)
						if [ "$MIDASHOME" != "/midas" ]; then
						  mv /midas /midas.org
						  rm -f midas.org 2>/dev/null
					  	  ln -s $MIDASHOME /midas
						fi
	  					MIDAS_LIBDIR_DONE="yes"
					  	;;
                        	esac
				if [ -n "$MIDAS_LIBDIR_DONE" -a -n "$MIDAS_BINDIR_DONE" ]; then
				  echo " " >/dev/tty
				  echo "ESO-MIDAS installation completed successfully." >/dev/tty
				  echo "Exit and type \"inmidas\" to start ESO-MIDAS" >/dev/tty
				  echo " " >/dev/tty
				  MIDAS_SETUP="OK"
				fi
				ask_return 
			fi
		else
			echo "Sorry. You need to be superuser to run this option" >/dev/tty
			ask_return
		fi	
	
	fi

	isset_setup_installed
}

install_product()
{
	#
	#  Install the chosen products.  
	#

	if check_precond_install; then
		clear > /dev/tty

		show_state
		if ask_yn 'OK to install this configuration'; then
			save_dir=`pwd`
			cd $MIDASHOME
			change_ifs ","

			MIDAS_PROD_INSTALLED=
			echo " " > /dev/tty
			for prod in $MIDAS_PROD_SELECTED; do
				case "$prod" in
					$PROD_BIN)		suffix=bin;;
					$PROD_SRC)		suffix=src;;
					$PROD_GUI)		suffix=gui;;
					$PROD_DEMO)		suffix=demo;;
					$PROD_CALIB)		suffix=calib;;
				esac
				if [ "$suffix" != "src" ]; then
					ifile=`ls $MIDAS_CDDIR|grep -i ${MIDAS_ARCH_PREFIX}.$suffix`
				else
					ifile=$MIDAS_SOURCES
				fi
				if [ -z "$ifile" ]; then
					error "Can't find product file ${MIDAS_ARCH_PREFIX}.$suffix for $prod"
					sleep 1
				else
					necho "Installing $prod ..." > /dev/tty
					COMMAND="zcat < $MIDAS_CDDIR/$ifile | tar xf -"
					if sh -c "$COMMAND" ; then
						MIDAS_PROD_INSTALLED=`addto "$MIDAS_PROD_INSTALLED" "$prod"`
					fi
					echo " " > /dev/tty
				fi
			done
			echo " " > /dev/tty

			restore_ifs
			cd $save_dir

			MIDAS_PROD_SELECTED=`difference "$MIDAS_PROD_SELECTED" "$MIDAS_PROD_INSTALLED"`
		fi
	fi

	isset_prod_installed
}

quit()
{
	ask_yn 'Are you sure you want to quit' y
}

infer_cddir()
{
	ifile=`ls |grep -i install.sh`
	if [ -n "$ifile" ]; then
		MIDAS_CDDIR=`pwd`
	else
		ifile=`ls /cdrom |grep -i install.sh`
		if [ -n "$ifile" ]; then
			MIDAS_CDDIR="/cdrom"
		fi
	fi
}


get_arch()
{
  case $1 in
    $REL_ALPHA)   MIDAS_ARCH=$ARCH_ALPHA   ; MIDAS_ARCH_PREFIX=alphaosf;;
    $REL_HP700)   MIDAS_ARCH=$ARCH_HP700   ; MIDAS_ARCH_PREFIX=hp700;;
    $REL_IBM)     MIDAS_ARCH=$ARCH_IBM     ; MIDAS_ARCH_PREFIX=rs6000;;
    $REL_SGI5)    MIDAS_ARCH=$ARCH_SGI5    ; MIDAS_ARCH_PREFIX=sgirix5;;
    $REL_SUNOS)   MIDAS_ARCH=$ARCH_SUNOS   ; MIDAS_ARCH_PREFIX=sparcsun;;
    $REL_SOLARIS4) MIDAS_ARCH=$ARCH_SOLARIS ; MIDAS_ARCH_PREFIX=sparcsol;;
    $REL_SOLARIS5) MIDAS_ARCH=$ARCH_SOLARIS ; MIDAS_ARCH_PREFIX=sparcsol;;
    $REL_LINUX)   MIDAS_ARCH=$ARCH_LINUX   ; MIDAS_ARCH_PREFIX=pclinux;;
  esac
}

infer_arch()
{
  MIDAS_PROD_SELECTED=""
  MIDAS_PROD_INSTALLED=""

  UNAME=`(uname) | sed 's/\//-/g' 2>/dev/null`
  URELS=`(uname -r) 2>/dev/null`

  get_arch "${UNAME}_$URELS"
  if [ -z "$MIDAS_ARCH" ] ;then
    get_arch "$UNAME"
  fi
}


select_action()
{
	#  The main routine.  It just keeps prompting the user to perform
	#  one of the listed actions.  The loop is broken when the user
	#  affirms the quit.

	MIDAS_STATE=1
	while true; do
		clear > /dev/tty

		set_state
		show_state

		pre1=" "
		pre2=" "
		pre3=" "
		pre4=" "
		pre5=" "
		pre6=" "
		pre7=" "
		preQ=" "

		echo "                                                                " > /dev/tty
		echo "  Choose one of the following actions:                       " > /dev/tty

		case "$MIDAS_STATE" in
			1)	pre1=">";;
			2)	pre2=">";;
			3)	pre3=">";;
			4)	pre4=">";;
			5)	pre5=">";;
			6)	pre6=">";;
			7)	pre7=">";;
			Q)	preQ=">";;
		esac

		echo "                                                                " > /dev/tty
		echo "${pre1}1. Enter the CD mount point.                             " > /dev/tty
		echo "${pre2}2. Select architecture.                                  " > /dev/tty
		echo "${pre3}3. Enter the installation directory (MIDASHOME).         " > /dev/tty
		echo "${pre4}4. Select ESO-MIDAS product(s) to install.               " > /dev/tty
		echo "${pre5}5. Install ESO-MIDAS product(s).                         " > /dev/tty
		echo "${pre6}6. Complete ESO-MIDAS installation in your system.       " > /dev/tty
		echo "${preQ}Q. Quit.                                                 " > /dev/tty
		echo "                                                                " > /dev/tty
		select=`ask_list 'Enter your selection' "1,2,3,4,5,6,q,Q" $MIDAS_STATE`
		case "$select" in
			   1) if pick_cddir;       then MIDAS_STATE=`expr $select + 1`; fi;;
			   2) if pick_arch;        then MIDAS_STATE=`expr $select + 1`; fi;;
			   3) if pick_midashome;   then MIDAS_STATE=`expr $select + 1`; fi;;
			   4) if pick_product;     then MIDAS_STATE=`expr $select + 1`; fi;;
			   5) if install_product;  then MIDAS_STATE=`expr $select + 1`; fi;;
			   6) if setup_product;    then MIDAS_STATE="$STATE_QUIT"; fi;;
			[qQ]) if quit; then break; fi;;
			   *) fatal_error "Improper selection $select in select_action";;
		esac
	done
}

##############################################################################
#
#  Main Processing here
#
##############################################################################

banner
ask_return 

infer_cddir
infer_arch
select_action

post_install

exit 0