shithub: riscv

ref: 297c6d8c490ea934a4cd968d9edc5656ecd209a4
dir: /rc/bin/inst/mountdist/

View raw version
#!/bin/rc

# prereq: mountfs configdist
# 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 || test -f $1/9front.iso || test -f $1/9front.iso.bz2
}

fn trycdimage{
	if(test -f $1){
		rm -f /srv/9660.dist
		unmount /n/dist >[2]/dev/null
		9660srv 9660.dist >[2]/dev/null
		logprog mount /srv/9660.dist /n/dist $1
		exitifdone
		mountdist=notdone
		export mountdist
		exit notdone
	}
}

fn trycdimagebz2 {
	if(test -f $1){
		mkdir -p /n/newfs/dist
		echo -n 'bunzip2 < '^$1^' >/n/newfs/dist/9front.iso'
		bunzip2 < $1 >/n/newfs/dist/_9front.iso &&
		mv /n/newfs/dist/_9front.iso /n/newfs/dist/9front.iso
		echo
		trycdimage /n/newfs/dist/9front.iso
		mountdist=notdone
		export mountdist
		exit notdone
	}
}


switch($1){
case checkready
	if(! ~ $distisfrom local && ! ~ $download done){
		mountdist=notdone
		export mountdist
	}
	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|raw)$'}
	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 'Any of the following will suffice (in order of preference):'
			echo '	- the root directory of the cd image'
			echo '	- the directory containing 9front.iso'
			echo '	- the directory containing 9front.iso.bz2'
			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
	}

	trycdimage /n/distmedia/$distmediadir/9front.iso
	trycdimage /n/newfs/dist/9front.iso

	trycdimagebz2 /n/distmedia/$distmediadir/9front.iso.bz2
	trycdimagebz2 /n/newfs/dist/9front.iso.bz2

	mountdist=notdone
	export mountdist
	exit notdone
}