shithub: riscv

ref: b0a059537147237e868b4a8f07922ae8a349e55c
dir: /rc/bin/inst/mountdist/

View raw version
#!/bin/rc

# prereq: mountfs
# desc: locate and mount the distribution

fn domount{
	if(! test -e $mountmedia(2))
		logprog $srvmedia
	unmount /n/distmedia >[2]/dev/null
	logprog $mountmedia
}

fn exitifdone{
	if(test -d /n/dist/dist/plan9front)
		exit
}

fn havedist {
	test -d $1/dist/plan9front
}

switch($1){
case checkready
	if(! ~ $#mountmedia 0 1){
		if(domount){
			mountdist=done
			export mountdist
			if(./mountdist checkdone)
				exit
		}
		srvmedia=()
		mountmedia=()
		mountdist=ready
		export srvmedia mountmedia mountdist
	}
		
case go
	fat=()
	x9660=()

	echo Please wait... Scanning storage devices...

	parts=`{ls /dev/sd*/* /srv/sd*/*.iso >[2]/dev/null |
		grep -v '/(plan9.*|nvram|ctl|log|led|raw|smart)$'}
	for (i in $parts) {
		echo -n '	'^$i
		t=`{fstype $i}
		switch($t){
		case dos
			fat=($fat $i)
		case 9660
			x9660=($x9660 $i)
		}
		echo
	}
	echo
	echo The following storage media were detected.
	echo Choose the one containing the distribution.
	echo
	for(i in $parts){
		switch($i){
		case $fat
			echo 	'	'^$i^' (microsoft fat)'
		case $x9660
			echo 	'	'^$i^' (iso9660 cdrom)'
		}
	}
	echo

	mountstatus=x
	while(! ~ $mountstatus ''){
		default=()
		if(! ~ $#cdboot 0)
			default=(-d /)
		prompt $default 'Distribution disk' $fat $x9660 $fs /
		disk=$rd

		srvmedia=()
		mountmedia=()
		switch($disk){
		case /
			mountmedia=(mount /srv/boot /n/distmedia)
		case $fs
			mountmedia=(bind /n/newfs /n/distmedia)
		case $fat
			srvmedia=(dossrv)
			mountmedia=(mount /srv/dos /n/distmedia $disk)
		case $x9660
			srvmedia=(9660srv)
			mountmedia=(mount /srv/9660 /n/distmedia $disk)
		case *
			echo Unknown disk type '(cannot happen)'
			exit oops
		}
		export srvmedia mountmedia
		domount
		mountstatus=$status
	}

	first=yes
	dir=/
	while(~ $first yes || ! havedist /n/distmedia/$dir){
		if(~ $first yes){
			echo
			echo Which directory contains the distribution?
			echo
			first=no
		}

		prompt -d $dir 'Location of archives'
		dir=$rd
		if(~ $#dir 0)
			dir=/
		if(! ~ $#dir 1)
			dir=$"dir
		if(! havedist /n/distmedia/$dir)
			echo 'No distribution found in '^`{cleanname /$dir}
	}

	distmediadir=$dir
	export distmediadir

case checkdone
	if(! ~ $#distmediadir 1){
		mountdist=notdone
		export mountdist
		exit notdone
	}
	if(! havedist /n/distmedia/$distmediadir && ! havedist /n/newfs/dist){
		mountdist=notdone
		export mountdist
		exit notdone
	}

	exitifdone

	if(test -d /n/distmedia/$distmediadir/dist/plan9front){
		bind /n/distmedia/$distmediadir /n/dist
		exitifdone
		mountdist=notdone
		export mountdist
		exit notdone
	}

	mountdist=notdone
	export mountdist
	exit notdone
}