#!/bin/bash
minino-gobohide show
clear
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>

# (c) Antonio Sánchez León <introlinux[fix@]gmail[fix.]com>
# (c) Miguel Anxo Bouzada <mbouzada[fix@]gmail[fix.]com>
# (c) GALPon MinNo Team <minino[fix@]galpon[fix.]org>
# july 2012

## -

### Destino es un parámetro dado, por ejemplo sda1 formateado en ext4
### Montamos el destino

# ¿Estamos en modo UEFI o BIOS?
MODO=`[ -d /sys/firmware/efi ] && echo UEFI || echo BIOS`

partidestino=$1
umount /dev/$1
cd /media
mkdir $1
mount /dev/$1 /media/$1

# Si empieza por m entonces quita 2, si empieza por s quita solo uno mmcblk0p1 es mmcblk0 y si es sda1 es sda
# Presuponemos que el disco de instalación es el mismo en el que está la partición efi (pero no tiene por qué)

disktype=`echo $1|cut -c 1`

if [ "$disktype" = "s" ];then
    disk=`echo -n $1 |head -c -1`
else
    disk=`echo -n $1 |head -c -2`
fi


if [ "$disktype" = "s" ];then

    efipart=`gdisk -l /dev/$disk|grep EF00|awk '{print $1}'`
	numpartefi=$efipart
else
    efipart=`gdisk -l /dev/$disk|grep EF00|awk '{print $1}'`
	numpartefi=$efipart
    efipart="p"$efipart
fi

if [ "$efipart" = "" ];then
    echo "No se encuentra la partición EFI."
    exit 0
fi

efipart=$disk$efipart

echo "Disco: /dev/${disk}"
echo "Partición de destino: /dev/${partidestino}"
echo "Partición EFI: /dev/${efipart}"
echo "Número de partición efi: $numpartefi"
echo ""
echo "Se va a copiar todo en /media/${partidestino}"
echo ""
sleep 15

### Copiamos todo el sistema al destino

rsync -va --delete / /media/$1/ --exclude '/proc/*' --exclude '/dev/*' --exclude '/sys/*' --exclude '/tmp/*' --exclude '/media/*' --exclude '/run/*' --exclude '/lib/live/mount/*'

#Refind_linux.conf file
#echo -n '"Boot with standard options"  "ro root=/dev/' >/media/$1/boot/refind_linux.conf
#echo -n $1 >>/media/$1/boot/refind_linux.conf
#echo -n ' quiet splash zswap.zpool=zsmalloc"' >>/media/$1/boot/refind_linux.conf

echo -n '"Boot with standard options"  "ro root=' >/media/$1/boot/refind_linux.conf
uuid=`blkid -o export -s UUID /dev/$1`
echo -n $uuid >>/media/$1/boot/refind_linux.conf
echo -n ' quiet splash zswap.zpool=zsmalloc"' >>/media/$1/boot/refind_linux.conf



### Etiquetamos el destino para no andar con UUIDs
echo "Creando etiquetas..."
sleep 2

e2label /dev/$1 PICAROS-$1
#e2label /dev/$efipart EFI-$efipart
#mlabel -i /dev/$efipart ::EFI-$efipart


### Modificamos fstab para que el sistema encuentre dónde está la nueva partición
echo "Modificamos entradas en fstab"
sleep 2

cat <<EOF >/media/$1/etc/fstab
# /etc/fstab: static file system information.
# Format:
#  <file system>         <mount point>   <type>  <options>      <dump>  <pass>

proc /proc  proc  defaults  0  0

LABEL=PICAROS-${partidestino}  /  ext4  errors=remount-ro,noatime,commit=60  0  1
#LABEL=EFI-${efipart} /boot/efi vfat rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=utf8,shortname=mixed,errors=remount-ro	0 0
/SWAPFILE none swap sw 0 0

EOF

### Añadimos un archivo swap en el raíz del destino de 128M
#echo "Añadimos SWAPFILE..."
#sleep 2
#dd if=/dev/zero of=/media/$1/SWAPFILE bs=1M count=128
#mkswap -L SWAP /media/$1/SWAPFILE


### Montamos la partición EFI para instalar el grub en ella
#echo "Instalamos GRUB..."

#mkdir /media/$1/boot/efi
#mount /dev/$efipart /media/$1/boot/efi

#OJO REQUISITO IMPRESCINDIBLE TENER UN KERNEL 64BITS
### Instalamos GRUB
#mount -B /proc /media/$1/proc
#mount -B /sys /media/$1/sys
#mount -B /dev /media/$1/dev
#mount -B /run /media/$1/run

#chroot /media/$1 grub-install --efi-directory=/boot/efi
#chroot /media/$1 update-grub
#sync

#mkdir /media/$1/boot/efi/EFI/BOOT
#cp /media/$1/boot/efi/EFI/debian/grubx64.efi /media/$1/boot/efi/EFI/BOOT/bootx64.efi


# Desmontando que es gerundio...

#umount /media/$1/boot/efi
#umount /media/$1/dev
#umount /media/$1/proc
#umount /media/$1/sys
#umount /media/$1/run

echo "Instalamos refind"
cd /opt/refind/refind/
./refind-install --usedefault /dev/$efipart --alldrivers

#ESTO ES PARA PONER REFIND EN EL MENÚ, PERO DESGRACIADAMENTE NO FUNCIONA EN TODOS LOS CASOS
#mkdir /tmp/${efipart};mount /dev/${efipart} /tmp/${efipart};
#if [ -d /tmp/${efipart}/EFI/Boot ];then
#	efibootmgr -c -d /dev/$disk -p $numpartefi -w -L PicarOS -l \EFI\Boot\bootx64.efi
#elif [ -d /tmp/${efipart}/EFI/BOOT ];then
#	efibootmgr -c -d /dev/$disk -p $numpartefi -w -L PicarOS -l \EFI\BOOT\bootx64.efi
#elif [ -d /tmp/${efipart}/EFI/boot ];then
#	efibootmgr -c -d /dev/$disk -p $numpartefi -w -L PicarOS -l \EFI\boot\bootx64.efi
#fi
#umount /dev/$efipart

sync

umount /media/$1

echo "REINICIA"
