#!/bin/sh
#(c) 2003/4 Barry kauler
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)

echo "This script was written for the particular situation of Puppy"
echo "installed on a USB memory device, usually a Flash card, and"
echo "you want to edit the file image.gz"
echo "The installation of Puppy on a Flash card is such that image.gz"
echo "is just a file on the card, along with vmlinuz and all the files"
echo "and folders of your home directory."
echo "When Puppy is booted, the Flash card is mounted on /root, and"
echo "image.gz can be found at /root/image.gz"
echo " "

#RAMDSKSIZE=49152
#RAMDSKSIZE=36864
RAMDSKSIZE="`cat /etc/ramdisksize`"

# the home device, probably sda1, was saved into /tmp/homedevice
# during bootup, by /etc/rc.d/rc.sysinit
# if mounted successfully, otherwise the file doesn't exist...
if [ -e /tmp/homedevice ];then
 SDX1="`cat /tmp/homedevice`"
 echo "Your home device is $SDX1"
else
 echo "Home device not accessable. Exiting."
 exit 0
fi

# The flash card has image.gz, which has the compressed Puppy
# root filesystem. This script enables you to copy any files
# to it, making them a permanent part of Puppy.

# echo "Loading kernel modules for USB Flash..."
# modprobe usb-storage
# modprobe usb-uhci
# ...already done in /etc/rc.d/rc.modules.

# echo "Making 48M minix f.s. on /dev/ram1..."
# mkfs.minix -c /dev/ram1 49152

#Puppy now mounts at boot, onto /root...
#echo "Mounting flash card /dev/sda1 on /mnt/flash..."
#mount -t umsdos /dev/sda1 /mnt/flash
#if [ "$?" -ne 0 ];then
#echo "FLASH CARD NOT AT /dev/sda1, CANNOT MOUNT"
#exit 0
#fi
#umssync /mnt/flash

echo "Uncompressing image.gz to /dev/ram1..."
gunzip -c /root/image.gz | dd of=/dev/ram1

echo "Need to be a bit cautious here. Older versions of Puppy, before"
echo "v0.7.6 I think, used the minix filesystem for image.gz, whereas"
echo "Puppy is now using ext2."
echo "this script assumes that image.gz is a ext2 filesystem -- if it"
echo "is minix or something else, you will need to edit this script."
echo '(be careful, the filesystem used for image.gz is not necessarily'
echo ' the same as the filesystem of the partition mounted on /root!)'
echo " "

echo "Mounting /dev/ram1 on /mnt/ram1..."
mount -t ext2 /dev/ram1 /mnt/ram1

echo "Use a file-manager such as Rox to copy any files to"
echo "/mnt/ram1. When finished, Press ENTER key to continue..."
echo -n '(or CTRL-C to abort): '
read VAL1

cd /
umount -f /mnt/ram1
echo "Copying /dev/ram1 to home device..."
# /dev/sda1 mounted at /root during boot...
dd if=/dev/ram1 bs=1k count=$RAMDSKSIZE | gzip - > /root/image.gz
sync
#umount /mnt/flash

# echo "Removing USB kernel modules..."
# rmmod usb-uhci
# rmmod usb-storage

echo " "
echo "Freeing memory used for /dev/ram1..."
freeramdisk /dev/ram1
echo "Done"
