#!/bin/sh

#
# set these variables as desired....
#

buildDIR=/home/Cobalt-MIPS
mountDIR=$buildDIR/image
LOG=$buildDIR/install.log
VERSION=deb-RaQ-base-0.02.tgz
URL=http://fsn.raqware.com/Mips-Debian/build-base-2.4.27


echo "========================================================="
echo "This script will install a Debian Base System for;"
echo "  Cobalt MIPS servers with serial console port"
echo ""
echo "Directories are created at;"
echo "  $mountDIR where /dev/hdx2 will be mounted"
echo "  $mountDIR/boot where /dev/hdx1 will be mounted"
echo "  $mountDIR/home where /dev/hdx5 will be mounted"
echo "  Where in hdx 'x' is the drive arguement the target drive"
echo "Drive image is stored at $buildDIR/$VERSION"
echo ""
echo "Credits to the debian-mips development team"
echo "Particularly Peter Horton and Martin Michlmayr"
echo ""
echo "Script developed by Front Street Networks - RaQware"
echo "please send comments to gwaugh@frontstreetnetworks.com"
echo "========================================================"
echo ""

echo "install log at $LOG"
echo "Building Debian MIPS system `date`" > $LOG

# Check if arguement provided
#
if [ ! $1 ] 
then
  echo ""
  echo "Oops! arguement missing"
  echo "Usage: `basename $0` hdb, hdc or hdd"
  echo ""
  exit 1
fi

#
# Check if arguement is valid and setup drive variables

if [ $1 == hdb ]
then
  drive=/dev/hdb
  drive1=/dev/hdb1
  drive2=/dev/hdb2
  drive3=/dev/hdb3
  drive5=/dev/hdb5
elif [ $1 == hdc ]
then
  drive=/dev/hdc
  drive1=/dev/hdc1
  drive2=/dev/hdc2
  drive3=/dev/hdc3
  drive5=/dev/hdc5
elif [ $1 == hdd ]
then
  drive=/dev/hdd
  drive1=/dev/hdd1
  drive2=/dev/hdd2
  drive3=/dev/hdd3
  drive5=/dev/hdd5
else
  echo""
  echo "Usage `basename $0` drive-argument"
  echo "Oops! drive arguement must be: hdb, hdc, or hdd"
  echo ""
exit 1
fi

echo ""
echo "============================================================="
echo "Are you sure you want to install Debian on Hard Drive $drive"
echo "Any data currently on the drive $drive will be destroyed"
echo "============================================================="
echo ""

#
# check user input for y/n for continue
#

TESTIN=0
while [ $TESTIN == 0  ]; do
  echo -n "Continue (y/n): "
  read ACCEPT
  if [ $ACCEPT == "y" ]; then
    TESTIN=1
  elif [ $ACCEPT == "n" ]; then
    TESTIN=1
    echo "Aborting the install at your request"
    exit 0
  fi
done

#
# Checking if image exist on this system"
#
echo "Checking if drive image exist on this system"

if [ -f $buildDIR/$VERSION ]
then
  echo "file $VERSION exist"
else
  echo "" 
  echo "Attempting to get file $VERSION"
  wget $URL/$VERSION
  wget $URL/$VERSION.asc
  if [ -f $buildDIR/$VERSION ]
  then
    echo ""
    echo "download of $VERSION successful"
    echo "Cecking md5sum of download"
    if md5sum deb-RaQ-base-0.02.tgz | cmp deb-RaQ-base-0.02.tgz.asc
    then
      echo "md5sum for deb-RaQ-base-0.02.tz is OK"
    else
      echo "md5sum for deb-RaQ-base-0.02.tz is bad"
      echo "Suggest redownload..."
      exit
    fi
    echo ""
  else
    echo ""
    echo "download of $VERSION failed"
    echo "Option is download $VERSION from $URL/$VERSION"
    echo "and install in this system in $buildDIR"
    echo ""
    exit 
  fi
fi
echo ""

#
# create partitions on drive
# part1 = 100MB  (boot)
# part2 = 4000MB (root)
# part3 = 256MB  (swap)
# part4 = Extended
# part5 = rest of drive (home)

echo "creating partitons on $drive"
if sfdisk $drive -uM <<EOF >> $LOG 2>&1 
0,100,L
,4000,L
,256,S
,,E
,,L
EOF
then 
   echo "partitions created successfully"
else
   echo "failed to create partitions on $drive"
   echo "perhaps partitions are mounted (in-use)"
   exit 
fi

#
# Make part1 bootable
#
echo ""
echo "making $drive1 bootable"
fdisk $drive  <<EOF >> $LOG 2>1
a
1
w
EOF

#
# format drive
# part1 ext2, part2 ext3, part5 swap, part6 ext3
#
echo ""
echo "formating $drive partitions"

echo "Formating $drive2 ext3 root file system"
mkfs.ext3 $drive2 >> $LOG 2>&1
echo "done"

echo "Formating $drive5 ext3 home file system"
mkfs.ext3 $drive5 >> $LOG 2>&1
echo "done"

echo "Formating $drive1 ext2 boot file system"
mkfs.ext2 -r 0 $drive1 >> $LOG 2>&1
echo "done"

echo "Creating $drive3 swap partition"
mkswap $drive3 >> $LOG 2>&1
echo "done"

#
# Mount the required partitions 1/2/5
#

if [ -d /$mountDIR ];
then
  echo ""
  echo "==============================================="
  echo "WARNING about to remove directory $mountDIR"
  echo "And Create new clean directory $mountDIR"
  echo "==============================================="
  echo""
  
  TESTIN=0
  while [ $TESTIN == 0  ]; do
    echo -n "Continue (y/n)"
    read ACCEPT
    if [ $ACCEPT == "y" ]; then
      TESTIN=a
      echo "Removing Directory $mountDIR"
      rm -rf $mountDIR
    elif [ $ACCEPT == "n" ]; then
      TESTIN=1
      echo "Aborting the install at your request"
      exit 0
    fi
  done
fi

echo "Creating Directory $mountDIR"
if mkdir $mountDIR >> $LOG 2>&1
then
  echo "directory $mountDIR created"
else
  echo "make dir $mountDIR failed"
  exit 1
fi
	     
echo ""
echo "Mounting $drive2 root partition $mountDIR.............."
mount $drive2 $mountDIR
 
echo "Creating directory $mountDIR/boot ....................."
mkdir $mountDIR/boot
echo "Mounting $drive1 boot partition $mountDIR/boot ........"
mount $drive1 $mountDIR/boot

echo "Creating directory $mountDIR/home ....................."
mkdir $mountDIR/home
echo "Mounting $drive5 home partition $mountDIR/home ........"
mount $drive5 $mountDIR/home

echo ""

mount | grep $drive >> $LOG 2>&1

#
# write the drive partitions 1,2 and 5
#
echo ""
echo "Writing the Debian drive image to $drive"
echo "This will take several minutes"

tar -xzpsf $VERSION 

echo "********************************************************"
echo "If your Cobalt MIPS server does not have a serial port
select y below. This will allow a dhcp server to set the IP
address for the server. Make sure the network has a dhcp server.
Then access the server via SSH and configure as desred"
echo "user admin pass admin then su - to root"
echo "*******************************************************"
TESTIN=0
while [ $TESTIN == 0  ]; do
  echo -n "Use dhcp server (y/n): "
  read ACCEPT
  if [ $ACCEPT == "y" ]; then
    TESTIN=1
    cd /$mountDIR/
    cp etc/network/interfaces.dhcp etc/network/interfaces
    cp etc/inittab.dhcp etc/inittab
    cd $buildDIR
  elif [ $ACCEPT == "n" ]; then
    TESTIN=1
    echo "Server is using static IP"
    echo "login to the console port and edit /etc/network/interfaces"
    echo "to setup the network"
  fi
done

#
# umount the drive partitions 
#

echo ""
echo " unmounting partitions for $drive"
umount $drive1
umount $drive5
umount $drive2

echo "Removing directory $mountDIR"
rm -rf $mountDIR

#
# Finished
#
echo ""
echo "==============================================================="
echo "Install of Debian Base System on $drive is complete"
echo "Shutdown the system, remove $drive and install in the RaQ"
echo ""
echo "In the Cobalt tradition username: admin/root and password: admin"
echo "Edit /etc/apt/sources.list and choose a Debian Mirror close to you"
echo "Wise to run \"apt-get update\" and \"apt-get dist-upgrade\""
echo " and change the passwords for admin and root"
echo "================================================================"

exit 0


