#!/bin/sh
#############################
# mu: muLinux builder
#############################
# (C) 1998 by M. Andreoli
#
# Note: this script use some ideas 
# inspired  by tomsrtbt (Tom Oesher) distribution. 

#set -x

# Syntax

usage()
{
cat <<END
mu : mulinux floopy manager

Options:
	

	-unpack|-u		unpack  muLinux images in this dir.
	-rebuild|-r		rebuild muLinux images	
	-x			rebuild X11 add-on images	
	-install|-i [model]	installation Menu	
	-tgz			make muLinux tgz	
	-lsm			make muLinux LSM	

	-login|-l		start a "muLinux Hard-Disk session" 
				Use with care! You can leave "core" around
				in muLinux file-system!
END
exit 1
}

[ $# = 0 ] && usage

#
# Local (configurable) data
#
TREE=tree	# mulinux fs tree
BZIP2="`pwd`/bzip2"
LILO="`pwd`/lilo"
SUPERFORMAT="`pwd`/fdformat"
DIR=`pwd`

# Include some mulinux basic config file

. mu.cnf 

#

case $STARTUP_DEVICE in
*fd0H1722*) PASS=1722;;
*fd0*|*fdH1440*) PASS=1440;;
esac

mount_point=$DIR/mnt
super_floppy=/dev/fd0H1722

TGZ=mulinux-$VERSION.tgz

umount $mount_point 2>/dev/null

[ -b $super_floppy ]||mknod $super_floppy b 2 60||{ echo ERROR;exit; }
[ -d $mount_point ]||mkdir $mount_point       ||{ echo ERROR;exit; }


#--------------------------
# Utilities 
#--------------------------


abort()
{
if [ $# -ne 0 ] ; then
	echo "Aborting $@ ..."
else
	echo "Some error occurred. Abort."
fi
exit 1
}


msg()
{
echo "-----------------------------------"
echo "$@"
echo "-----------------------------------"
}

filesize()
{
set -- `du -k $1`
echo $1
}

Format()
{
device=$1
shift 1
msg=$*

case "$device" in
*/dev/fd0H1722*)
	op=superformat; size=1722K; cmd="$SUPERFORMAT"
	;;
*/dev/fd0H1440*|/dev/fd0)
        op=format; size=1440K; cmd=$SUPERFORMAT
        ;;
*)
	echo "$device not supported"
	exit
	;;
esac

if [ -z "$msg" ] ; then
	msg="Insert your best $size-diskette."
fi

echo
echo $msg 
echo -n "Do You want ${op}ting it (y/n) ? [y]> "
read ans

  if [ "${ans}" = y ] || [ -z "$ans" ] ; then
  msg "$op:  please wait ..."
  eval $cmd $device
  else
  echo "Good: this floppy is already ${op}ted."
  sleep 1
  echo
  fi

}


prerequisites()
{
mkdir /tmp/fs.tmp 2>/dev/null
dd if=/dev/zero of=/tmp/fs bs=1k count=100
mkfs.ext2 -F -q /tmp/fs
mount -o loop /tmp/fs /tmp/fs.tmp
rc=$?

umount /tmp/fs 2>/dev/null

rm -r /tmp/fs /tmp/fs.tmp 2>/dev/null

if [ $rc != 0 ] ; then
cat <<END
Sorry: missing loopback device support. 
Recompile Your LINUX  kernel and answer YES at CONFIG_BLK_DEV_LOOP prompt.
Exit.
END
        exit
fi


}


#-------------------------
# Configuration functions
#-------------------------


# build a lilo.conf

make_lilo()
{
pass=$1
STARTUP_DEVICE=/dev/fd0H$pass

cat <<END
#
# lilo.conf for muLinux (build by mu script)
#
boot=$STARTUP_DEVICE
install=/boot/boot.b
map=/boot/map
message=/boot/mulinux.msg
backup=/dev/null

read-write
vga=normal
prompt
delay=40
timeout=50

image=/boot/mulinuz
label=mulinux
root=$STARTUP_DEVICE
append="INFO=$pass load_ramdisk=1 prompt_ramdisk=0 ramdisk_size=4600 \
ramdisk_start=$ROOT_OFFSET $KERNEL_BOOT_PARAMETERS"



END
}

clean()
{
find $TREE -name core -exec rm -i \{} \;
rm -fr $TREE/tmp/*
rm -f $TREE/var/tmp/*
rm -f $TREE/var/log/*
rm -f $TREE/var/lock/*

}

rebuild()
{
msg "Check prerequisites ..."
prerequisites

clean

#-------------------------------
# USR partition
#-------------------------------

msg "building USR.bz2 ...please, standby."

# USR_RAW_SIZE is in mu.cnf 

dd if=/dev/zero of=USR bs=1k count=$USR_RAW_SIZE
echo
eval mkfs.ext2 -q -F -m 0 USR 
umount $mount_point 2>/dev/null
mount -o loop USR $mount_point
rm -r $mount_point/lost*

cd $TREE/usr
usr_list="`/bin/ls| grep -v X11`"
cp -a ${usr_list} $mount_point
df | grep USR
umount $mount_point
cd $DIR

rm USR.bz2 2>/dev/null
$BZIP2 USR

USR_SIZE=`filesize USR.bz2`

#-------------------------------
# BOOT partition
#-------------------------------

# BOOT_FREE is set in mu.cnf

msg "building BOOT.raw : please, standby ..."

set -- `du -k -s $TREE/startup`

ROOT_OFFSET=`expr $1 + $BOOT_FREE`


dd if=/dev/zero of=BOOT.raw bs=1k count=$ROOT_OFFSET
echo
eval mkfs.ext2 -F -q -m 0 BOOT.raw

mount -o loop BOOT.raw $mount_point
rm -r $mount_point/lost* 2>/dev/null

cp -af $TREE/startup/* $mount_point
umount $mount_point

BOOT_SIZE=`filesize BOOT.raw`

fsck.ext2 BOOT.raw

#-------------------------------
# ROOT partition
#-------------------------------


msg "building ROOT.gz ... please, standby."

# some link

cd $TREE/dev
rm root 2>/dev/null
ln -s $ROOT_DEVICE root 
cd $DIR

# ROOT_RAW_SIZE is set in mu.cnf

dd if=/dev/zero of=ROOT bs=1k count=$ROOT_RAW_SIZE
echo
eval mkfs.ext2 -F -q -m 0 -i ${ROOT_INODES}  ROOT  
mount -o loop ROOT $mount_point
rm -r $mount_point/lost*


cd $TREE
root_list=`/bin/ls | grep -v usr | grep -v startup| grep -v gcc`
cp -a ${root_list} $mount_point
cd  $DIR 
df | grep ROOT
umount $mount_point

fsck.ext2 ROOT

# ROOT size 
 
gzip -9 -f ROOT
rs=`filesize ROOT.gz`
ROOT_SIZE=`expr $rs + 1`	# 1=overlap security
USR_OFFSET=`expr $ROOT_OFFSET + $ROOT_SIZE`

# enlarge ROOT.gz to effective ROOT_SIZE

cp ROOT.gz  ROOT.tmp
dd if=ROOT.tmp bs=${ROOT_SIZE}k conv=sync count=1 of=ROOT.gz
rm ROOT.tmp

# make some lilo.conf

make_lilo 1440 > lilo.1440
make_lilo 1722 > lilo.1722

# some info (put it in BOOT )

cat > info <<END
VERSION=$VERSION
DATE="`date`"
# USR
USR_MEDIA=1722
USR_OFFSET=$USR_OFFSET
USR_UNCOMPRESS_PROGRAM="bzip2 -ds"

# X11
X11_MEDIA=1722
X11_OFFSET=0
X11_UNCOMPRESS_PROGRAM="bzip2 -ds|tar -xvf-"

#GCC
GCC_MEDIA=1722
GCC_OFFSET=0
GCC_UNCOMPRESS_PROGRAM="bzip2 -ds|tar -xvf-"

# on-the-fly
END

cp info tree/startup/boot/info

TOT=`expr $USR_OFFSET + $USR_SIZE`

echo "BOOT=$BOOT_SIZE, ROOT=$ROOT_SIZE, USR=$USR_SIZE, Total $TOT" |\
tee /dev/console | cat > free 

FREE=`expr 1722 - $TOT`

echo "$FREE k-bytes (uncompressed) free." |tee /dev/console | cat >> free
echo

#-------------------------------
#  Build Standard muLinux   
#-------------------------------

msg "Building BOOT+ROOT+USR base system .."
echo "[ Other model with 'mu -i' ]"

Format $STARTUP_DEVICE "Insert your best diskette"

echo "Copy BOOT.raw+ROOT+USR ..."
cat BOOT.raw ROOT.gz USR.bz2 > $STARTUP_DEVICE
msg "Add LILO ..."
mount $STARTUP_DEVICE $mount_point
# add info into BOOT
cp info  $mount_point/boot/info
cat lilo.$PASS | $LILO -r $mount_point -C -
umount $mount_point

# take this!
msg "Take BOOT ... Please, standby."
dd if=$STARTUP_DEVICE of=BOOT bs=1k count=$ROOT_OFFSET

msg "Done. Reboot now and have fun!"

}

#-------------------------------
# X11 addon
#-------------------------------

rebuild_x()
{

msg "building X11.tgz ...please, standby."

clean

# X11_RAW_SIZE is in mu.cnf

cd $TREE 
tar -cf- usr/X11R6 | $BZIP2 > $DIR/X11.tgz
cd $DIR
X11_SIZE=`filesize X11.tgz`
echo "`du -k X11.tgz`"
}

#-------------------------------
# GCC addon
#-------------------------------

rebuild_gcc()
{

msg "building GCC.tgz ...please, standby."

clean

cd $TREE
tar -cf- gcc | $BZIP2 > $DIR/GCC.tgz
cd $DIR
GCC_SIZE=`filesize GCC.tgz`
echo "`du -k GCC.tgz`"
}


install()
{

if [ $# = 0 ] ; then
clear
cat << END
			- Make floppies -
Menu:

0)	Boot-prompt-Parameters setting

1)	BOOT+ROOT+USR (1722K) 
2)	BOOT+ROOT (1440K) and USR (1440K)
3)	BOOT+ROOT (1440K) and USR (1722K)
4)	BOOT+ROOT (1722K) and USR (1722K)
x)	X11 (1722K)
g)	GCC (1722K)

q)	quit

NOTE:

- check the 'free' file for best choice. Image must fit in the floppy. 
- 1440K floppy boot faster, compared to 1722K.
- muLinux standard is : BOOT+ROOT+USR (1722K) and X11 (1722K)

END

	echo -n "Choice [1]> "
	read floppy 
	[ -z "$floppy" ] && floppy="1"
else
	floppy=$1
fi

echo
echo "Choice : $floppy."
echo

case $floppy in
q)
	exit
	;;
0)
	clear
	cat << END

	            - BOOT PROMPT PARAMETERS -	

 IMPORTANT NOTE TO MODULE USERS: Boot Prompt arguments typically only
 apply to hardware drivers that are compiled directly into the kernel.
		
Examples:
	mem=16M			# how much memory you have
	vga=normal|extended	# vga spec.
	aha152x=0x340,11,7,1	# SCSI for Iomega ZIP
	hd=cyls,heads,sects	# disk geometry
	msmouse=irq		# irq for M$ bus mouse
	root=/dev/hdc3		# mount this root	
	initrd=			# initial ramdisk argument

Blank separated list allowed.

END

	read -p "Enter boot-prompt string :" KERNEL_BOOT_PARAMETERS
	echo "You have entered: $KERNEL_BOOT_PARAMETERS"

	make_lilo 1440 > lilo.1440
	make_lilo 1722 > lilo.1722

	echo "Done."
	sleep 1 
	mu -i
	;;
1)
        Format /dev/fd0H1722 "Insert BOOT+ROOT+USR floppy"
        msg "copy BOOT+ROOT+USR ..."
        cat BOOT ROOT.gz USR.bz2 > /dev/fd0H1722
        echo "Done. Reboot now."
        ;;
2)
        Format /dev/fd0H1440 "Insert BOOT+ROOT floppy"
        msg "copy BOOT+ROOT ..."
        cat BOOT ROOT.gz > /dev/fd0H1440
        mount /dev/fd0H1440 $mount_point
	rm $mount_point/boot/map
        msg "Add LILO loader  ..."
        cat lilo.1440 | $LILO -r $mount_point -C -

	echo "USR_MEDIA=1440" >> $mount_point/boot/info
	echo "USR_OFFSET=0" >> $mount_point/boot/info

	umount $mount_point

        Format /dev/fd0H1440 "Insert USR floppy"
        msg "copy USR.bz2 ..."
        cat USR.bz2 > /dev/fd0H1440
        echo "Done. Insert boot floppy and Reboot."

        ;;
3)
        Format /dev/fd0H1440 "Insert BOOT+ROOT floppy"
        msg "copy BOOT+ROOT ..."
        cat BOOT ROOT.gz > /dev/fd0H1440
        mount /dev/fd0H1440 $mount_point
        rm $mount_point/boot/map
        msg "Add LILO loader  ..."
        cat lilo.1440 | $LILO -r $mount_point -C -

        echo "USR_MEDIA=1722" >> $mount_point/boot/info
        echo "USR_OFFSET=0" >> $mount_point/boot/info

        umount $mount_point

        Format /dev/fd0H1722 "Insert USR floppy"
        msg "copy USR.bz2 ..."
        cat USR.bz2 > /dev/fd0H1722
        echo "Done. Insert boot floppy and Reboot."
	;;
4)
        Format /dev/fd0H1722 "Insert BOOT+ROOT floppy"
        msg "copy BOOT+ROOT ..."
        cat BOOT ROOT.gz > /dev/fd0H1722
        mount /dev/fd0H1722 $mount_point

        echo "USR_MEDIA=1722" >> $mount_point/boot/info
        echo "USR_OFFSET=0" >> $mount_point/boot/info

        umount $mount_point

        Format /dev/fd0H1722 "Insert USR floppy"
        msg "copy USR.bz2 ..."
        cat USR.bz2 > /dev/fd0H1722
        echo "Done. Insert boot floppy and Reboot."
	;;
x)
	Format /dev/fd0H1722 "Insert X11 floppy"	
	msg "copy X11 ..."
	cat X11.tgz > /dev/fd0H1722
	echo "Done."
	;;
g)
        Format /dev/fd0H1722 "Insert GCC floppy"
        msg "copy GCC ..."
        cat GCC.tgz > /dev/fd0H1722
        echo "Done."
        ;;
*)
	echo "Model unsupported."
	sleep 1
	install	
	;;
esac
sync
}


unpack()
{

msg "Unpacking ...."
msg "Check prerequisites ..."
prerequisites

umount $mount_point 2>/dev/null

if [ -d $TREE ] ; then
	echo "Found a $TREE/ dir. Do You want continue anywhere?"
	read ans
fi 
rm -fr $TREE 
mkdir $TREE 
mkdir -p $TREE/startup

msg "unpack BOOT ..."
cp BOOT /tmp/BOOT || abort
mount -o loop /tmp/BOOT $mount_point
cp -af $mount_point/* $TREE/startup 
umount $mount_point
rm -f /tmp/BOOT

msg "unpack ROOT.gz ..."
cp ROOT.gz /tmp/ROOT.gz || abort

gzip -d /tmp/ROOT.gz 
mount -o loop /tmp/ROOT $mount_point
cp -af $mount_point/* $TREE 
umount $mount_point
rm -f /tmp/ROOT

msg "unpack USR.bz2"
cp USR.bz2 /tmp || abort
$BZIP2 -d /tmp/USR.bz2 
mount -o loop /tmp/USR $mount_point

mkdir -p $TREE/usr
cp -af $mount_point/* $TREE/usr
umount $mount_point
rm -f /tmp/USR

if [ -r X11.tgz ] ; then
msg "unpack X11.tgz"
cat X11.tgz| $BZIP2 -ds | tar -C $TREE -xvf-  
fi

echo
echo "Done."
}

make_tgz()
{

msg "Creating main $TGZ"

tar -cf mu.tar \
mu mu.cnf info free lilo.1440 lilo.1722 \
doc README.* INSTALL.* mulinux-$VERSION.lsm contrib sources \
bzip2 fdformat* lilo \
BOOT ROOT.gz USR.bz2 

gzip -c mu.tar > $TGZ 
rm mu.tar 

msg "Creating DOSTOOLS "
rm -f DOSTOOLS.zip
cd dos                                                                          
#tar -h -cf - * | gzip -9 > ../DOSTOOLS.tgz
zip ../DOSTOOLS.zip *
cd ..

echo "Done."
}


make_lsm()
{

cat > mulinux-${VERSION}.lsm <<ENDLSM
Begin3
Title:		muLinux	
Version:	${VERSION}	
Entered-date:	`date '+%d%b%y'`	
Description:	Full-configured, application-centric 1722K one-floppy Linux. 
		muLinux is a minimalistic, but almost complete, 
		script-based Linux distribution made in Italy, fitted 
		on a single 1722K floppy with  X11 and GCC floppy addon.
	        Work on PC 386-4M + swap and install in RAM, UMSDOS & EXT2.
        	SUPPORT: Internet connection (via PPP) or LAN (via eth);
        	e-mail processing (fetch, read and send), NFS, SAMBA, ftp, 
		IRC, finger, telnet, tcpdump, news and web browsing (lynx);
		PC-PC link via pppd; IP firewall & IP masqueranding; 
        	remote access via modem line (miterm); editing&printing;
        	fdisk,e2fsck, fdformat ; job scheduler (atd), ringd daemon,
        	WWW server, dialin server, serial server, fax server;
        	play & record WAV, CD listening (workbone), say, etc. :)
        	OTHER FEATURES: international keyboards support, BUS mouse,
        	PAP/CHAP authentication, permanent multi-configuration,
        	installation from LINUX/DOS/WINDOWS, PC speaker support,
		NFS-root booting for diskless Xterminal applications. 

Keywords:	QNX, linux, spaghetti-linux 
Author: 	andreoli@pisoft.it (Michele Andreoli)
Maintained-by:	andreoli@pisoft.it (Michele Andreoli)
Primary-site:	sunsite.unc.edu /pub/Linux/system/recovery
		`du -b mulinux-${VERSION}.tgz`	
		`du -b mulinux-${VERSION}.lsm`	
Alternate-site:	http://www4.pisoft.it/~andreoli/mulinux.html
		`du -b mulinux-${VERSION}.tgz`	
		`du -b X11.tgz`	
		`du -b GCC.tgz`	
		`du -b DOSTOOLS.zip`	
Platforms:	Dos/Win/Linux
Copying-policy:	GPL
End
ENDLSM

}


# Begin Main

msg "muLinux V${VERSION}"


case $1 in
-x)
	rebuild_x
	;;
-gcc)
	rebuild_gcc
	;;
-rebuild|-r) 
	rebuild
	;;
-install|-i) 
	shift 1
	install $1	
	;;
-unpack|-u) 
	unpack	
	;;
-lsm)
	make_lsm
	;;
-tgz) 
	make_lsm
	make_tgz	
	;;
-login|-l)
	echo "Welcome to muLinux V${VERSION}"
	gpm -k 2>/dev/null
	chroot $TREE -ash
	;;
-h)	usage
	
	;;
*)	usage	
    	;;
esac


# End
