#!/bin/bash
#GNU script by The GALPon MiniNo Team

clear
echo " "
echo -e "\033[\033[1;31m## #n ## Instalación de GALPon MiniNo ##\033[0m\n"
echo " "
echo "Para instalar el sistema es necesaria una partición Linux (ext2 ó ext3)"
echo "y una partición de intercambio SWAP."
echo "Si ya tiene particionado el disco duro responda no, « n » a la siguiente"
echo "cuestión."
echo -n "¿Quiere particionar el disco duro? (s, n): "
read particionar
if   [ "$particionar" = "s" ]; then
		export LANG="en_EN.UTF-8"
		cfdisk
		export LANG="es_ES.UTF-8"
fi
clear
echo " "
echo "Partición donde se va a instalar GALPon MiniNo: "
echo -n "Ej. hda1: "
read partidestino
discoduro=`echo $partidestino |tr -d [:digit:]`   #en nuestro ejemplo, hda
swap=`cfdisk -P s /dev/$discoduro|grep "swap"|cut -f2 -d " "`  #extrae el número de la partición swap
clear
echo -e "\033[\033[1;31m## ¡¡¡ PRECAUCIÓN !!! ##\033[0m\n"
echo " "
echo "Esta operación no tiene vuelta atrás, al formatear se"
echo "perderán todos los datos de las particiones en cuestión."
echo "Así ha quedado la tabla de particiones: "
echo " "
fdisk -l /dev/$discoduro
echo " "
echo "Vamos a formatear la partición $partidestino para el"
echo "sistema y la partición $discoduro$swap para el área"
echo "de intercambio swap."
echo " "
echo -n "¿Está conforme? (s, n): "
read borra
if   [ "$borra" = "s" ]; then
	    mkfs.ext3 /dev/$partidestino
	    mkswap /dev/"$discoduro$swap"
fi
echo " "
echo "Copia de GALPon MiniNo realizandose en $partidestino"
echo "sea paciente..."
echo " "
sleep 4
mkdir /mnt/$partidestino
mount -t auto -o rw /dev/$partidestino /mnt/$partidestino
cd /mnt/image
cp -v -a * /mnt/$partidestino
cd /mnt/$partidestino
mkdir proc sys tmp
mkdir /mnt/$partidestino/var/cache/apt/archives
mkdir /mnt/$partidestino/var/cache/apt/archives/partial
mkdir /mnt/$partidestino/var/tmp
#chmod ugo=rwx /mnt/$partidestino/tmp
#chmod ugo=rwx /mnt/$partidestino/var/tmp
chmod 777 /mnt/$partidestino/var/tmp
chmod 777 /mnt/$partidestino/tmp
clear


# CREAMOS EL /etc/fstab y el mtab acorde con los datos de particiones
#

swap=`cfdisk -P s /dev/$discoduro|grep "swap"|cut -f2 -d " "`  #extrae el número de la partición swap

########## ARCHIVO FSTAB
cat <<EOF >/mnt/$partidestino/etc/fstab
# /etc/fstab: static file system information.
# Format:
#  <file system>         <mount point>   <type>  <options>      <dump>  <pass>
/dev/$partidestino  /  ext3  defaults,errors=remount-ro,noatime,commit=60  0  1
/dev/hda$swap  none  swap  sw  0  0
proc /proc  proc  defaults  0  0
/dev/cdrom /media/cdrom0 udf,iso9660 user,noauto  0  0
/dev/fd0 /floppy auto rw,user,noauto  0  0

EOF

########## ARCHIVO MTAB
cat <<EOF >/mnt/$partidestino/etc/mtab
/dev/$partidestino / ext3 rw,errors=remount-ro 0 0
tmpfs /lib/init/rw tmpfs rw,nosuid,mode=0755 0 0
proc /proc proc rw,noexec,nosuid,nodev 0 0
sysfs /sys sysfs rw,noexec,nosuid,nodev 0 0
procbususb /proc/bus/usb usbfs rw 0 0
udev /dev tmpfs rw,mode=0755 0 0
tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
devpts /dev/pts devpts rw,noexec,nosuid,gid=5,mode=620 0 0

EOF

## finalmente instalamos grub en el MBR y añadimos "quiet splash vga=788" a las líneas de carga de kernel

grub-install --root-directory=/mnt/$partidestino /dev/$discoduro
mv /mnt/$partidestino/boot/grub/menu.lst /mnt/$partidestino/boot/grub/menu.lst-original
cd /mnt
chroot $partidestino /sbin/update-grub -y

#en caso de que $partidestino sea igual a hda1 entonces asigna a rootgrub hd0,0
#en caso de que $partidestino sea igual a hda2 entonces asigna a rootgrub hd0,1

unidad=$discoduro
numero_unidad=`echo $partidestino|tr -d [hd][a][s]`

if   [ "$unidad" = "hda" ]; then
	    rootgrub="hd0,"
fi

if   [ "$unidad" = "hdb" ]; then
	    rootgrub="hd1,"
fi

if   [ "$unidad" = "sda" ]; then
	    rootgrub="hd0,"
fi

if   [ "$unidad" = "sdb" ]; then
	    rootgrub="hd1,"
fi


rootgrub=$rootgrub"$((numero_unidad-1))"

########## ARCHIVO MENU.LST
cat <<EOF >/mnt/$partidestino/boot/grub/menu.lst
# menu.lst - See: grub(8), info grub, update-grub(8)
#            grub-install(8), grub-floppy(8),
#            grub-md5-crypt, /usr/share/doc/grub
#            and /usr/share/doc/grub-legacy-doc/.

## default num
# Set the default entry to the entry number NUM. Numbering starts from 0, and
# the entry number 0 is the default if the command is not used.
#
# You can specify 'saved' instead of a number. In this case, the default entry
# is the entry saved with the command 'savedefault'.
# WARNING: If you are using dmraid do not change this entry to 'saved' or your
# array will desync and will not let you boot your system.
default		0

## timeout sec
# Set a timeout, in SEC seconds, before automatically booting the default entry
# (normally the first entry defined).
timeout		5

# Pretty colours
color cyan/blue white/blue
splashimage=($rootgrub)/boot/grub/galpon-minino.xpm.gz

## password ['--md5'] passwd
# If used in the first section of a menu file, disable all interactive editing
# control (menu entry editor and command-line)  and entries protected by the
# command 'lock'
# e.g. password topsecret
#      password --md5 $1$gLhU0/$aW78kHK1QfV3P2b2znUoe/
# password topsecret

#
# examples
#
# title		Windows 95/98/NT/2000
# root		(hd0,0)
# makeactive
# chainloader	+1
#
# title		Linux
# root		(hd0,1)
# kernel	/vmlinuz root=/dev/hda2 ro
#

#
# Put static boot stanzas before and/or after AUTOMAGIC KERNEL LIST

### BEGIN AUTOMAGIC KERNELS LIST
## lines between the AUTOMAGIC KERNELS LIST markers will be modified
## by the debian update-grub script except for the default options below

## DO NOT UNCOMMENT THEM, Just edit them to your needs

## ## Start Default Options ##
## default kernel options
## default kernel options for automagic boot options
## If you want special options for specific kernels use kopt_x_y_z
## where x.y.z is kernel version. Minor versions can be omitted.
## e.g. kopt=root=/dev/hda1 ro
##      kopt_2_6_8=root=/dev/hdc1 ro
##      kopt_2_6_8_2_686=root=/dev/hdc2 ro
# kopt=root=/dev/hda1 ro

## default grub root device
## e.g. groot=(hd0,0)
# groot=(hd0,0)

## should update-grub create alternative automagic boot options
## e.g. alternative=true
##      alternative=false
# alternative=true

## should update-grub lock alternative automagic boot options
## e.g. lockalternative=true
##      lockalternative=false
# lockalternative=false

## additional options to use with the default boot option, but not with the
## alternatives
## e.g. defoptions=vga=791 resume=/dev/hda5
# defoptions=

## should update-grub lock old automagic boot options
## e.g. lockold=false
##      lockold=true
# lockold=false

## Xen hypervisor options to use with the default Xen boot option
# xenhopt=

## Xen Linux kernel options to use with the default Xen boot option
# xenkopt=console=tty0

## altoption boot targets option
## multiple altoptions lines are allowed
## e.g. altoptions=(extra menu suffix) extra boot options
##      altoptions=(single-user) single
# altoptions=(single-user mode) single

## controls how many kernels should be put into the menu.lst
## only counts the first occurence of a kernel, not the
## alternative kernel options
## e.g. howmany=all
##      howmany=7
# howmany=all

## should update-grub create memtest86 boot option
## e.g. memtest86=true
##      memtest86=false
# memtest86=true

## should update-grub adjust the value of the default booted system
## can be true or false
# updatedefaultentry=false

## should update-grub add savedefault to the default options
## can be true or false
# savedefault=false

## ## End Default Options ##

title		GALPon Minino, kernel 2.6.24-1-486
root		($rootgrub)
kernel		/boot/vmlinuz-2.6.24-1-486 root=/dev/$partidestino ro quiet splash vga=788
initrd		/boot/initrd.img-2.6.24-1-486

title		GALPon MiniNo, kernel 2.6.24-1-486 (single-user mode)
root		($rootgrub)
kernel		/boot/vmlinuz-2.6.24-1-486 root=/dev/$partidestino ro single
initrd		/boot/initrd.img-2.6.24-1-486

title		GALPon Minino, VESA 800x600
root		($rootgrub)
kernel		/boot/vmlinuz-2.6.24-1-486 root=/dev/$partidestino ro carga=800x600
initrd		/boot/initrd.img-2.6.24-1-486

title		GALPon Minino, VESA 1024x780
root		($rootgrub)
kernel		/boot/vmlinuz-2.6.24-1-486 root=/dev/$partidestino ro carga=1024x780
initrd		/boot/initrd.img-2.6.24-1-486

### END DEBIAN AUTOMAGIC KERNELS LIST

EOF


########## TERMINAMOS

cd
sync
umount -a
clear
echo "Se ha completado la instalación. El sistema se reiniciará en 10 segundos."
echo ""
echo "Si lo desea, puede cancelar el reinicio con «ctrl+c»"
echo -n ".";sleep "1";echo -n ".";sleep "1";echo -n ".";sleep "1";echo -n ".";sleep "1";echo -n ".";sleep "1";echo -n ".";sleep "1";echo -n ".";sleep "1";echo -n ".";sleep "1";echo -n ".";sleep "1";echo -n ".";sleep "1"; reboot



#cat /mnt/$partidestino/boot/grub/menu.lst |grep /boot/vmlinuz-2.6.24>/tmp/lineasdekernel
#cat /tmp/menu.lst-temporal|while read linea; do echo "$linea quiet splash vga=788">>lineasdekernel_ok;done

##numero el archivo menu.lst

#contador=1;
#exec</mnt/$partidestino/boot/grub/menu.lst
#while read linea
#do
#  echo -n "$contador: "
#  echo $linea
#  contador=$((contador+1))
#done>>/tmp/menu.lst-numerado

##averiguo cual es la línea del kernel

#cat /tmp/menu.lst-numerado |grep /boot/vmlinuz-2.6.24|cut -f1 -d ":"

##¿Cómo extraigo solo la primera línea?

#numerolinea=

## cambio esa línea con sed
# sed -i -e '3c \texto_que_va_en_la_linea_3' archivo

#sed -i -e '"$numerolinea"c \$linea' /mnt/$partidestino/boot/grub/menu.lst


