summaryrefslogblamecommitdiffstats
path: root/vmware/runvmware
blob: baf35257a2f428f00fd1f654cfc04da2534aec9a (plain) (tree)
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


































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                
#!/bin/sh
#
# Description:  Script for preparing VMware environment Diskless X Stations
#               (v4.0)
#
# Author(s):    Dirk von Suchodoletz <dirk@goe.net>, 05-08-2005
#               Michael Janczyk <mj0@uni-freiburg.de>, 10-03-2006
# Copyright:    (c) 2003, 2006 - RZ Universitaet Freiburg
#
VERSION=pre0.14; LAST_CHANGES=10.03.06
################################################################################




### VERBOSE? ###################################################################
# set verbose mode (set -x(v))
#set -xv



# TO BE CHANGED!!!!
# TO BE CHANGED!!!!
# TO BE CHANGED!!!!
# TO BE CHANGED!!!!
# TO BE CHANGED!!!!
# TO BE CHANGED!!!!
# TO BE CHANGED!!!!
# TO BE CHANGED!!!!
# TO BE CHANGED!!!!
# TO BE CHANGED!!!!
# TO BE CHANGED!!!!
# TO BE CHANGED!!!!
# TO BE CHANGED!!!!
# TO BE CHANGED!!!!
# TO BE CHANGED!!!!
# TO BE CHANGED!!!!



### HEADER SECTION #############################################################
# VMware guestOS variable declaration
ostypeheader ()
 {
 echo '
 # This configuration can be changed in runvmware

 ##### 
 # For OSType use the first part of the image-name.
 # -OSType-     | -VMWare name- | -Description (use "-" for space)-
 #####
 windows98	win98		Windows-98
 windows2000	win2000pro	Windows-2000-Professional
 windowsxp	winxppro	Windows-XP-Professional
 suse		suse		Novell-SuSe-Linux
 kubuntu	other26xlinux	kubuntu
 debian         other26xlinux   debian

 #####
 # What to boot if none of the obove is found, equivalent to *
 # -else-       | -VMWare name- | -Description (use "-" for space)-
 #####

 else		other		Unspecified-OS
 '
 }


# runvmware alias header
aliasheader ()
 {
 echo '
 # This configuration can be changed in runvmware
 #
 # -NO DUPLICATE ENTRIES!!!-

 ###### 
 # -OSType-     | -aliases separated through spaces-
 #####
 windows98	win98
 windows2000	win2k
 windowsxp	winxppro winxp
 suse		suse93

 #####
 # -Spec names- | -aliases separated through spaces-
 #####
 kursversion	kurs k
 '
 }


# runvmware.conf
runvmwareconfheader ()
 {
 echo "
 # This configuration file was generated by 'runvmware', dont use it for your
 # own configurations - it will be overwritten

 apmSuspend = \"FALSE\"
 config.version = \"$CONFVER\"
 debug = \"FALSE\"
 disable_acceleration = \"FALSE\"
 displayName = \"$DISPLAYNAME\"
 ethernet0.address = \"00:50:56:0D:$MAC\"
 ethernet0.addressType = \"static\"
 ethernet0.connectionType = \"bridged\"
 ethernet0.present = \"TRUE\"
 $FLOPPYA
 floppy1.fileName = \"$FILENAME\"
 floppy1.fileType = \"file\"
 floppy1.present = \"TRUE\"
 floppy1.startConnected = \"TRUE\"
 guestOS = \"$VMOSTYPE\"
 hard-disk.enableIBR = \"FALSE\"
 ide0:0.fileName = \"$TMPDIR/disk\"
 ide0:0.mode = \"$NP\"
 ide0:0.present = \"TRUE\"
 isolation.tools.dnd.disable = \"TRUE\"
 logging = \"FALSE\"
 memsize = \"$MEM\"
 parallel0.bidirectional = \"FALSE\"
 parallel0.fileName = \"/dev/parport0\"
 parallel0.present = \"FALSE\"
 priority.grabbed = \"normal\"
 priority.ungrabbed = \"normal\"
 redoLogDir = \"$TMPDIR\"
 resume.repeatable = \"FALSE\"
 scsi0:0.deviceType = \"cdrom-raw\"
 scsi0:0.fileName = \"$CDR_1_NAME\"
 scsi0:0.present = \"$CDR_1\"
 scsi0:1.deviceType = \"cdrom-raw\"
 scsi0:1.fileName = \"$CDR_2_NAME\"
 scsi0:1.present = \"$CDR_2\"
 scsi0.present = \"$VMSCSI\"
 serial0.fileName = \"/dev/ttyS0\"
 serial0.present = \"TRUE\"
 sharedFolder0.enabled = \"TRUE\"
 sharedFolder0.expiration = \"never\"
 sharedFolder0.guestName = \"home\"
 sharedFolder0.hostPath = \"$HOME\"
 sharedFolder0.present = \"TRUE\"
 sharedFolder0.readAccess = \"TRUE\"
 sharedFolder0.writeAccess = \"TRUE\"
 sharedFolder.maxNum = \"1\"
 sound.present = \"TRUE\"
 suspendToDisk = \"TRUE\"
 tmpDirectory = \"$TMPDIR\"
 tools.syncTime = \"TRUE\"
 usb.present = \"TRUE\"
 virtualHW.version = \"4\"
 " \
 >$CONFILE

 # Set the appropriate permissions for the vmware config file
 chmod u+rwx $CONFILE >/dev/null 2>&1
 }


preferencesheader ()
 {
 echo "
 # This configuration file was generated by 'runvmware', dont use it for your
 # own configurations - it will be overwritten

 hint.buslogic.needDriver = \"FALSE\"
 hint.cpuid.unknownfeature = \"FALSE\"
 hint.disklib.lockerror = \"FALSE\"
 hint.guestos.xp = \"FALSE\"
 hint.gui.poweroff = \"FALSE\"
 hint.gui.reset = \"FALSE\"
 hint.mks.fullscreen = \"FALSE\"
 hint.mks.notLocal = \"FALSE\"
 hint.moninit.newKernel = \"FALSE\"
 hint.nfsmounted.persistent = \"FALSE\"
 hint.nfsmounted.undoable = \"FALSE\"
 hint.nologging = \"FALSE\"
 hint.scsi.needDriver = \"FALSE\"
 hint.sound.openMixerFailed = \"FALSE\"
 hint.tools.toolsImage = \"FALSE\"
 hint.upgrade.legacyVM = \"FALSE\"
 hint.usbgLinux.altuhci = \"FALSE\"
 hint.vmui.poweroff = \"FALSE\"
 hint.xkeymap.notLocal = \"FALSE\"
 pref.allowInsanePkgs = \"FALSE\"
 pref.autoFitFullScreen = \"fitHostToGuest\"
 pref.autoRaise = \"FALSE\"
 pref.exchangeSelections = \"TRUE\"
 pref.grabOnKeyPress = \"FALSE\"
 pref.hotkey.shift = \"true\"
 pref.motionGrab = \"TRUE\"
 pref.motionScrollBarrier = \"100000\"
 pref.motionUngrabBarrier = \"1\"
 pref.openedVM0.present = \"TRUE\"
 pref.openedVM.maxNum = \"1\"
 pref.syncTime = \"FALSE\"
 pref.tip.startup = \"FALSE\"
 pref.toolbarIcons = \"FALSE\"
 pref.view.navBar.type = \"favorites\"
 pref.vmplayer.exit.vmAction = \"poweroff\"
 pref.vmplayer.fullscreen.autohide = \"TRUE\"
 pref.vmplayer.webUpdateOnStartup = \"FALSE\"
 prefvmx.defaultVMPath = \".vmware\"
 prefvmx.mru.config = \"$CONFILE:\"
 prefvmx.mru.suspended = \"\"
 tmpDirectory = \"$CONFDIR\"
 webUpdate.checkLast = \"1120235802\"
 webUpdate.checkPeriod = \"manual\"
 " \
 >$VMDIR/preferences
 }


# Create Windows 98 config files
win98header ()
 {
 echo -e "\tREGEDIT4\r

 [HKEY_CURRENT_USER\Networ\Persistent\H]\r
 \"RemotePath\"=\"\\\\192.168.1.2\\$USER\"\r
 \"UserName\"=\"$USER\"\r
 \"ProviderName\"=\"Microsoft Network\"\r" \
 > /media/loop0/home.reg

 echo -e "\tREGEDIT4\r

 [HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VxD\VNETSUP]\r
 \"ComputerName\"=\"$HOSTNAME\"\r
 \"Workgroup\"=\"$WORKGROUP\"\r
 \"Comment\"=\"$COMMENT\"\r
 \"StaticVxD\"=\"vnetsup.vxd\"\r
 \"Start\"=hex:00\r
 \"NetClean\"=hex:01\r

 [HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\\\
ComputerName]\r
 \"ComputerName\"=\"$HOSTNAME\"\r" \
 > /media/loop0/rechner.reg

 echo -e "\tREGEDIT4\r

 [HKEY_LOCAL_MACHINE\Network\Logon]\r
 \"username\"=\"$USER\"\r
 \"PrimaryProvider\"=\"Microsoft Network\"\r
 \"LMLogon\"=hex:00,00,00,00\r" \
 > /media/loop0/logon.reg
 }


winresheader ()
 {
 echo -e "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r
 <settings>\r
   <eintrag>\r
     <resolution_x param=\"$X_PARAM\">\r
     </resolution_x>\r
     <resolution_y param=\"$Y_PARAM\">\r
     </resolution_y>\r
     <username param=\"$USER\">\r
     </username>\r
   </eintrag>\r
 </settings>\r" \
 > /media/loop0/config.xml
 }




### VARIABLES SECTION ##########################################################

# Declaration of default variables

# Hardware variables
# Temporary disk space for logs, etc...
export TMPDIR=/tmp/vmware_temporary/$USER/tmp
# ;)
MAC=
declare -i MEM=0
declare -i MEMTEST=0
MEMOPT=
PERMEM=66
# Virtual Devices
VMSCSI=FALSE
CDR_1=FALSE
CDR_2=FALSE
FLOPPYA="floppy0.present = \"FALSE\""
# Resolution
HOSTRES=`xvidtune -show | grep -ve "^$"`
X_PARAM=`echo "$HOSTRES" | awk '{print $3}'`
Y_PARAM=`echo "$HOSTRES" | awk '{print $7}'`


# VMware variables
# VMware start options
VMOPT="-X -q"
# Config directory and file
CONFDIR=/tmp/vmware_temporary/$USER/vmware
CONFILE=$CONFDIR/runvmware.conf
VMDIR=$HOME/.vmware
# Zenity
SESS_ZEN="/etc/X11/sessions"
# VMware version
VM_VER=5
# Special Variables
#UNDOPOINT="keep" 
CONFVER=7
NP="independent-nonpersistent"
# Set hostname: using original hostname and adding string "-vm"
HOSTNAME=`hostname`"-vm"
# Set samba workgroup
WORKGROUP="tux-net"
# Place for the virtual floppy "B:"
FILENAME=`mount | grep -i "$HOST-1440.img" | awk '{print $1}'`
COMMENT="Diskless Workstation VMware-Image"
# Image Variables
IMGDIR="/usr/share/vmware"
ALIAS=
SPEC=
ENDING="vmdk"
DISKFILE=
RIGHTSFILE=
NOIMAGE=0
OSTYPE=
DISPLAYNAME="Unknown OS"
# Image checking variable
FILECHECK=


# Command line variables
# Command line options, transform to lower case
LINEOPTS=( `echo $@ | tr [A-Z] [a-z]` )
SILENT=
DEBUG=0
INTER=
COMDEF=
NARG=




### FUNCTIONS SECTION ##########################################################

# Function to write to stdout/logfile
# Logs labeled with $2=0 write to stdout, if debug set, write to logfile
writelog ()
 {
 # Write to stdout
 ! [ $SILENT ] && [ "$2" = "0" ] && echo -e "$1"

 # Check if range given e.g. 0 0
 if [ -z "$2" ] || [ -z "$3" ];then
    echo -e "\n\nNO RANGE IN WRITELOG STATEMENT:\n$1\n\tPlease correct!\n"
    exit 1
 fi

 # Log if level greater than 1 and log only from level $2 to level $3
 [ "$DEBUG" -ge "1" ] && [ "$DEBUG" -ge "$2" ] && [ "$DEBUG" -le "$3" ] && \
 echo -e "$1" >>$CONFDIR/runvmware.$DEBUG.log
 }


help ()
 {
 cat <<EOH

USAGE: $0 [--options]

Image options:
	-i			interactive mode with image selection
	-o <ostype>		start ostype, e.g. windowsxp, suse,...
				(first part of image name)
	-s <specification>	specify your image, e.g. standard
				(second part of image name)
	-a 			use aliases in runvmware header
	--silent		no stdout from runvmware
	--displayaliases	aliases you can use with option -a
        --mem <n(M(h)),n(G(h))>	override autoallocation of memory, in percent
                                m,M megabyte, g,G gigabyte, h,H reserve f. host
	--delay <n seconds>	delay the start of the script n seconds
	--image <directory>	specify image directory
	--persistent		use persistent mode
	--define-vm-ostype <os>	overwrite vmware ostype
VMware options:
        --run <programm>        specify programm, e.g. vmware, vmplayer, ...
	--windowed		start in windowed mode
	--edit			only open config file in vmware (only VMware)
	--donotquit		do not quit after image stopped (only VMware)
runvmware options:
	-h|--help		this help
	--version
	--debug <level>		level 1 -> print stdout to file

		Dirk von Suchodoletz,     Michael Janczyk
		  <dirk@goe.net>,      <mj0@uni-freiburg.de>

EOH

 # Print Version , remove path
 echo -e "$0: LAST CHANGES $LAST_CHANGES | VERSION $VERSION\n" |\
 awk -F "/" '{print "\t\t"$NF}'

 exit 0
 }


checkalias ()
 {
 writelog "Checking alias for $OSTYPE${SPEC:+", $SPEC"}...\c" 0 1
 # OSTYPE=
 OSALIAS=`aliasheader | grep -v "#.*" | grep -w "$OSTYPE" | awk '{print $1}'`
 [ -n "$OSALIAS" ] && OSTYPE=$OSALIAS

 # SPEC=
 [ -n "$SPEC" ] && \
 SPECALIAS=`aliasheader | grep -v "#.*" | grep -w "$SPEC" | awk '{print $1}'`
 [ -n "$SPECALIAS" ] && SPEC=$SPECALIAS

 writelog "finished\nResult:\n\tOS=$OSTYPE${SPEC:+"\\\n\\\tSPEC=$SPEC"}\n" 0 1
 }


x_dialog ()
 {
 DESKTOPS=( `ls $SESS_ZEN 2>/dev/null | sed -e "s,.desktop,,"` )
 DESKTOPSNR=`ls $SESS_ZEN 2>/dev/null | wc -l`
 (( NARG=0 ))
 while [ "$DESKTOPSNR" -gt "$NARG" ]; do
    MENU="$MENU ${DESKTOPS[$NARG]} ${DESKTOPS[$NARG]}"
    (( NARG=$NARG+1 ))
 done

 # --stderr because of 1>/dev/nul
 Xdialog --title "VMware Image Menu" \
         --stderr \
         --no-tags \
         --ok-label "Run Image" \
         --cancel-label "Abort Runvmware" \
         --menubox "Please choose the image you would like to run:" 24 51 6 \
         $MENU 1>/dev/null
 }


filecheck ()
 {
 FILECHECK=`LANG=us ls -lh $DISKFILE 2>&1`
 writelog "Filecheck:\n$FILECHECK\n" 0 1
 NOIMAGE=`echo $FILECHECK | grep -i "No such file or directory" | wc -l`
 RIGHTSFILE=$DISKFILE

 # Check if link
 if [ -L "$DISKFILE" ]; then
    writelog "Vmware Image Problem:\c" 0 1
    writelog "\tThe image you've specified is a link.\n" 0 1
    writelog "\tContinuing to start VMware, but it may cause problems!\n" 0 0

    # Take link target
    RIGHTSFILE=`ls -lh $DISKFILE 2>&1 | awk -F "->" '{print $2}'`
    FILECHECK=`LANG=us ls -lh $RIGHTSFILE 2>&1`
 fi

 # Does file exist
 if [ "$NOIMAGE" -ge "1" ]; then
    writelog "Vmware Image Problem:\c" 0 0
    writelog "\tThe image you've specified doesn't exist." 0 0
    writelog "Filecheck says:\c" 0 1
    writelog "\t\t$DISKFILE:\n\t\t\tNo such file or directory" 0 1
    writelog "Hint:\c" 0 0
    writelog "\t\t\tCompare spelling of the image with your options.\n" 0 0
    exit 1
 fi

 # Readable?
 if ! [ -r "$DISKFILE" ]; then # $RIGHTSFILE see above
    writelog "Vmware Image Problem:\c" 0 1
    writelog "\tThe image you've specified has wrong rights." 0 1
    writelog "Filecheck says:\t\t`echo $FILECHECK |\
              awk '{print $1" "$3" "$4}'` $RIGHTSFILE" 0 1
    writelog "Hint:\t\t\tChange rights with: chmod a+r $RIGHTSFILE\n" 0 0
    exit 1
 fi
 }




#### INTERPRET COMMAND LINE OPTIONS ############################################
# Print help if no clo
if [ $# = "0" ]; then
   help
   exit 0
fi

# Start
(( NARG=0 ))
while [ $# -gt "$NARG" ]; do
 case "${LINEOPTS[$NARG]}" in
  -h|--help)
     help
  ;;
  --version)
     echo -e "\n$0: LAST CHANGES $LAST_CHANGES | VERSION $VERSION\n" |\
     awk -F "/" '{print "\t"$NF}'
     exit 0
  ;;
  -i)
     INTER="yes"
  ;;
  -o)
     (( NARG=$NARG+1 )) # Needed for checking unknown options
     OSTYPE=${LINEOPTS[$NARG]}
  ;;
  -s)
     (( NARG=$NARG+1 ))
     SPEC=${LINEOPTS[$NARG]}
  ;;
  -a)
     ALIAS="yes"
  ;;
  --silent)
     SILENT=true
  ;;
  --displayaliases)
     aliasheader; exit 0
  ;;
  --delay)
     (( NARG=$NARG+1 ))
     sleep ${LINEOPTS[$NARG]}
  ;;
  --image)
     (( NARG=$NARG+1 ))
     IMGDIR=${LINEOPTS[$NARG]}
  ;;
  --persistent)
     NP="independent-persistent"
  ;;
  --define-vm-ostype)
     (( NARG=$NARG+1 ))
     VMOSTYPE=${LINEOPTS[$NARG]}
     COMDEF="yes"
  ;;
  --mem)
     (( NARG=$NARG+1 ))
     MEMOPT=${LINEOPTS[$NARG]}
  ;;
  --run)
     (( NARG=$NARG+1 ))
     VMRUN=${LINEOPTS[$NARG]}
  ;;
  --debug)
     (( NARG=$NARG+1 ))
     DEBUG=${LINEOPTS[$NARG]}
  ;;
  --windowed)
     VMOPT=`echo $VMOPT | sed -e "s,-X,-x,"`
  ;;
  --edit)
     VMOPT=""
  ;;
  --donotquit)
     VMOPT=`echo $VMOPT | sed -e "s,-q,,"`
  ;;
  *)
     echo -e "\n\tOption \"${LINEOPTS[$NARG]}\" not found, please correct.\n"
     sleep 1
     help
  ;;
 esac
 (( NARG=$NARG+1 ))
done




### STARTING ###################################################################

# Logo for console
cat <<EOL

           .----.--.--.-----.--.--.--------.--.--.--.---.-.----.-----.
           |   _|  |  |     |  |  |        |  |  |  |  _  |   _|  -__|
           |__| |_____|__|__|\___/|__|__|__|________|___._|__| |_____|
                Script for preparing VMware environment...(v$VERSION)


EOL
writelog "Starting...`echo $NP|sed -e \"s,i.*-,,g\"|tr [a-z] [A-z]`-mode\n" 0 0




### CHECK MACHINE SETUP ########################################################

# Create environment
# Remove leftover locks from former runs, 
# problems may occure if you run more then one vmware with this script
rm -rf $TMPDIR >/dev/null 2>&1
rm -f $CONFDIR/*LOCK >/dev/null 2>&1
rm -f $CONFDIR/*.$DEBUG.log >/dev/null 2>&1

# Check if /tmp/vmware_temporary is existing
writelog "Checking /tmp/vmware_temporary directory...\c" 0 99
if ! [ -d "/tmp/vmware_temporary" ]; then
   writelog "\n/tmp/vmware_temporary does not exist, or is no directory!" 0 99
   writelog "\n\tExiting!\n" 0 99
   exit 1
fi
writelog "OK!" 0 99

# Create vmware directories
mkdir -p $TMPDIR >/dev/null 2>&1
mkdir -p $CONFDIR >/dev/null 2>&1
mkdir -p $VMDIR >/dev/null 2>&1



# Start logging, because now we have the directories created ;)
writelog "##################################################" 1 99
writelog "#\n# File created by $0 (v.$VERSION)\n# on `date`"  1 99
writelog "# LOGLEVEL=$DEBUG\n#" 1 99
writelog "##################################################\n" 1 99
# Repeat only for logfile ;)
writelog "Starting...`echo $NP|sed -e \"s,i.*-,,g\"|tr [a-z] [A-z]`-mode\n" 1 1

# Log disksetup
writelog "Directories:" 0 1
writelog "\tTMPDIR=$TMPDIR\n\tCONFDIR=$CONFDIR\n\tVMDIR=$VMDIR\n" 0 1
writelog "vmware_temporary:\n`ls -l /tmp/vmware_temporary`\n" 0 1



# Configuring MAC address: first four bytes are fixed (00:50:56:0D) the
# last two bytes are taken from the local network adaptor
writelog "Starting hardware/device detection...\c" 0 1

MAC=`/sbin/ifconfig eth0 | grep eth0 | sed -e "s, ,,g" | \
     awk -F : '{print $(NF-1)":"$NF}'`


# Gather memory info and set virtual machine memory size accordingly
MEMTEST=`cat /proc/meminfo | grep MemTotal | sed -e 's,[0-9][0-9][0-9] kB,,' \
                                             -e 's,MemTotal: *,,'`

# Check if command line option given and calculate memory for vmware
if [ -n "$MEMOPT" ]; then
  # Memory allocation for host or guest?
  MEMHOST=`echo $MEMOPT | grep -i "h"`\
     && MEMOPT=`echo $MEMOPT | sed -e "s,[h,H],,"`
  # Memory given in megabyte
  MEMMEG=`echo $MEMOPT | grep -i "m"`
  # Memory given in gigabyte
  MEMGIG=`echo $MEMOPT | grep -i "g"`
  # Remove m or M
  [ -n "$MEMMEG" ] && MEM=`echo $MEMOPT | sed -e "s,[m,M],,"`
  # Remove g or G and convert to megabyte
  [ -n "$MEMGIG" ] && MEM=`echo $MEMOPT | sed -e "s,[g,G],,"`\
     && MEM=$MEM*1024
  # If no m or g, then it is percent
  [ -z "$MEMMEG" ] && [ -z "$MEMGIG" ] && MEM=$MEMTEST/100*$MEMOPT/4*4
  # Memory allocation for host or guest? If host calculate for guest.
  [ -n "$MEMHOST" ] && MEM=$MEMTEST-$MEM
  # Else use PERMEM variable to calculate memory percentage for vmware
else MEM=$MEMTEST/100*$PERMEM/4*4
fi

# Check memory range
MEMTEST=$MEMTEST-128
if [ "$MEM" -lt "128" ] || [ "$MEM" -gt "$MEMTEST" ]; then
  writelog "\n\n\tYour memory is out of range: $MEM MB.\n\
     \tMin. 128 MB for host and guest!\n\tTry --mem option.\n" 0 1
  exit 1
fi


# Look for cdrom,dvd and floppy and add them to the vm config file
if [ -L /dev/cdrom1 ] ; then
   VMSCSI=TRUE
   CDR_1=TRUE
   CDR_1_NAME="/dev/cdrom1"
fi

if [ -L /dev/cdrom2 ] ; then
   VMSCSI=TRUE
   CDR_2=TRUE
   CDR_2_NAME="/dev/cdrom2"
fi


# Floppy
[ -d /sys/block/fd0 ] && FLOPPYA="floppy0.fileName = /dev/fd0"


writelog "finished\nResults:\n\tMAC=$MAC\n\tMEM=$MEM\n\tVMSCSI=$VMSCSI" 0 1
writelog "\tCDR_1=$CDR_1\n\tCDR_2=$CDR_2\n" 0 1




### SELECT VIRTUAL IMAGE #######################################################

# Interactive mode
if [ -n "$INTER" ]; then
   writelog "Starting interactive mode...\c" 0 0

   MENUTEST=`ls $SESS_ZEN 2>/dev/null | wc -l`

   # Check for session files
   if [ "$MENUTEST" = "0" ]; then
      writelog "\rNo sessions found in /etc/X11/sessions for option -i" 0 99
      writelog "Please use option -o/-s and check your configuration \n" 0 0 
      exit 1
   fi

   # Check for Xdialog
   if ! [ `which Xdialog 2>/dev/null` ]; then
      writelog "\rXdialog not found!\t\t" 0 99
      writelog "Please install Xdialog for interactive mode, \c" 0 0
      writelog "or use options -o/-s.\n" 0 0
      exit 1
   fi

   # Create Menu, etc...
   ARGS=`x_dialog 2>&1 | tr [A-Z] [a-z] | sed -e "s,-, ,"` # 2>&1, see function
   OSTYPE=`echo $ARGS | awk '{print $1}'`
   SPEC=`echo $ARGS | awk '{print $2}'`

   if [ -z "$ARGS" ]; then
      writelog "\rNo image selected, please retry or use option -o / -s\n" 0 0
      writelog "No image was selected (opton -i); return value is: $ARGS\n" 2 2
      exit 1
   fi

   writelog "finished.\nResult:\n\tostype=$OSTYPE${SPEC:+\n\tspec=$SPEC}\n" 0 1
fi


# Aliases
[ -n "$ALIAS" ] && checkalias


# Image allocation
writelog "Starting image detection...\c" 0 1

DISKFILE="$IMGDIR/$OSTYPE${SPEC:+"-$SPEC"}.$ENDING"

# Check if VMOSTYPE defined via command line
if [ -z "$COMDEF" ]; then
   VMOSTYPE=`ostypeheader | grep -v "#.*" | grep -w "$OSTYPE" | \
             awk '{print $2}'`
   DISPLAYNAME=`ostypeheader | grep -v "#.*" | grep -w "$OSTYPE" |\
                awk '{print $3}' | sed -e "s,-, ,g"`

   # If OSTYPE not found use else statement
   VMOSTYPE=${VMOSTYPE:-`ostypeheader | grep -v "#.*" | grep -w "else" | \
                         awk '{print $2}'`}
   DISPLAYNAME=${DISPLAYNAME:-`ostypeheader | grep -v "#.*" | \
                               grep -w "else" | awk '{print $3}' |\
                               sed -e "s,-, ,g"`}
fi

writelog "finished\nResults:\n\tDISKFILE=$DISKFILE\n\tVMOSTYPE=$VMOSTYPE" 0 1
writelog "\tDISPLAYNAME=$DISPLAYNAME\n" 0 1


# Link to windows image
writelog "Linking \"ln -fs $DISKFILE $TMPDIR/disk\"...\c" 0 1
ln -fs $DISKFILE $TMPDIR/disk
writelog "finished\nResult:\n`ls -l $TMPDIR/disk`\n" 0 1


# Check if image exists, etc...
filecheck




### WRITE CONFIGURATION FILES ##################################################

# Create preferences
preferencesheader

# Create VMware startup file
runvmwareconfheader

# Prepare config files for the clientOS (win)
[ "$VMOSTYPE" = "win98" ] && win98header # Windows 98 specific

# Set Resolution for Windows guest
winresheader




### DELETE IF OBSOLETE #########################################################
# old, delete when obsolete
xvidtune -show | grep -ve "^$"| \
awk {'print "[resolution]\r\nx="$3"\r\ny="$7"\r\n\r\n[username]\r\n\
      user_name="ENVIRON["USER"]"\r\n"'} \
   > /media/loop0/config.ini




### RUN VMWARE(-Player) ########################################################

# ...with the automatically written config file
if [ `which vmplayer 2>/dev/null` ] && [ -z "$VMRUN" ]\
  || [ "$VMRUN" = "vmplayer" ]; then
   writelog "\nStarting VMplayer..." 0 0
   cp /usr/share/dxs/VMware/nvram.5.0 $CONFDIR/nvram 2>&1 >/dev/null
   VMOPT=`echo $VMOPT | sed -e "s,-q,," -e "s,-x,,"` # Remove unsupported opts.
   vmplayer $VMOPT $CONFILE 2>&1 >/dev/null

elif [ `which vmware 2>/dev/null` ] && [ -z "$VMRUN" ]\
  || [ "$VMRUN" = "vmware" ]; then
   # Copy licence file and virtual bios to VMware directory
   writelog "copying nvram and licence..." 0 0
   cp /usr/share/dxs/VMware/license.ws.5.0 $VMDIR 2>&1 >/dev/null
   cp /usr/share/dxs/VMware/nvram.5.0 $CONFDIR/nvram 2>&1 >/dev/null

   # Run VMware
   writelog "\nStarting VMware..." 0 0
   vmware $VMOPT $CONFILE 2>&1 >/dev/null

else
   writelog "\nNo VMware/VMPlayer found!\n" 0 99
   exit 1

fi




writelog "\nBye.\n" 0 0
exit 0
################################################################################