#!/bin/sh
#***********************************************************************
#ifdef __USAGE
#
#%C duplicates QNX hard drives.
#
# File: QNX_drive_copy
# Description:
#   This program creates logical copies of hard drives, as opposed to "dd"
#   image copies.  Therefore it does not rely on matching drive geometeries,
#   does not copy unused parts of the drive and the copy is defragmented.
#   This script assumes the drives use Logical Block Addressing (LBA) mode,
#   and are only one partition, and that partition is QNX of course.
#   Licenses and "bootability is preserved.  A log of is saved in /tmp/log.
# Keywords: duplicate hard drive disk copy image format master
# Version: 1.0
# Entered-date: 12-Jan-1998
# Author: Maurice Cinquini <cinquini@alamar-usa.com> of Philips DVS.
# Credits:
#   Robert Rutherford and Anthony Wei of Philips DVS,
#   with hints from Karl Heinz of Hughes, Soenke Nielsen
#   and the output of the QNX install process.
# Copying-policy: This program is public domain, but using this program
#   to distribute the QNX OS and other software requires permission from,
#   and/or royalites to, the legal owners of that software.
# Supplemental:
#   Only tested with QNX 4.24, single partition, LBA, IDE drives only.
#   The complete procedure to copy drives involves the following steps:
#   1. Insert the master (source) and slave (blank) hard drives.
#      Depending of the machine setup, slave drives usually needs to
#      have a jumper removed.
#   2. Boot QNX in a safe mode where there are no programs running that
#      may asynchronously write to the hard drive during the following
#      procedure.  If you can't guarantee this, you should use a QNX
#      boot floppy and mount the hard drive manually.
#   3. Login as root.
#   4. Verify the Master drive has the kernel built to force LBA option
#      during boot up.  Check /boot/build/hard.?? has the line
#      "Fsys.eide fsys -h 64,63", (i.e, sectors,heads for LBA mode.)
#   5. Run this script.
#
#endif
#***********************************************************************

LOG=/tmp/log # Must be an absolute path.

read ANS?"Completely overwrite /dev/hd0.1?  Press "y" to continue."
[ x"$ANS" = x"y" ] || exit

exec 4>&1   # Use file descriptor 4 to remember where stdin used to go to.


{
    # Sometimes the existing partition info will cause this script to fail
    echo "Clearing the slave partition disk."
    #echo "ERASED" | dd ibs=1k conv=sync of=/dev/hd0.1

    set -x # to show commands as they are exectued.

    # Display number of cylinders free.
    fdisk /dev/hd0.1 QUERY FREE

    # Creating default entries on the partition table.
    #  1.  Creating a single partition table using all available disk space
    #  2.  QNX Loader
    #  3.  Setting the boot flag

    fdisk /dev/hd0.1 add -f4 QNX ALL BOOT QNX LOADER
    mount -p /dev/hd0.1
    dinit -h /dev/hd0.1t77
    dinit -hb /dev/hd0.1t77
    if mount /dev/hd0.1t77 /hd0.1 ; then
        :
    else
        echo "############ ERROR #############"
        use $0
        exit
    fi

    # Output of dcheck has been left out of the log file, due to to enormous
    # amount of information dcheck outputs.  Also you can remove it for modern
    # big drives, where I've never seen it fail and it takes a long time.
    #dcheck -m /dev/hd0.1t77 1>&4

    # Copy files from master to slave drive, except for those to do with
    # drive geometries and the current growing log file.
    cp -Rpv -P!/.inodes -P!/.bitmap -P!$LOG / /hd0.1

    # Due to the encrypting mechanism on /etc/licenses we use license program.
    rm -rf /hd0.1/etc/licenses/*
    license /etc/licenses /hd0.1/etc/licenses

    fdisk /dev/hd0.1 LOADER BOOT QNX   # Install QNX boot loader.
    fdisk /dev/hd0.1 QUERY BOOT LOADER # Display # of cylinders.

} 2>&1 | tee $LOG

cp -v $LOG /hd0.1$LOG    # Place the final full log file on the copied drive.
exit 
