#!/bin/ash

# Programm zum Starten der SYS Installation mit der .iso -Datei, ohne sie auf ein DVD zu brennen.     Geht nur im Text-Modus 
# Kann von irgendwo aus aufgerufen werden, zBsp vom bereits gemounteten .iso aus. 
# Oder in KDE als Standard-Anwendung (Text-Mode)  bei Anklicken einer .iso -Datei definiert werden,  dann Aufruf
# durch ./installiso  oder durch   mount %1 /cdrom -t udf -o loop ; cp /cdrom/installiso . : umount /cdrom ; ./installiso 
# Jedoch nicht innerhalb von chroot aufrufen, denn chrootet selbst und muss vorher alle unbenutzten Partitionen aussen unmounten 

# Sucht auf allen Partitionen eine Datei /SYS_Linux.iso , mountet sie , entpackt die initrd nach /initrd, chrootet dorthin
# mit Aufruf von init .  Macht temporaeren Zusatz zu /etc/rc.d/rc.local sodass nach reboot Installation automatisch fortsetzt  

### Alle nicht gebrauchten Partitionen vor chroot unmounten, da sonst im Installer nicht richtig behandelt und nicht redimensionierbar

#  Bis nach /initrd gegangen wird, sind wir evtl. im .iso , read-only


mkdir /von   >> /dev/zero 2>&1


dialog --title " search install .iso file .. "  --no-collapse --colors --infobox "
Procurar arquivo .iso p/a instalacao ...
      Install-.iso-Datei suchen ...
"  6 45 
sleep 2 
VON=-  
for J0 in 1 2 3 4  ; do
for I0 in sda hda sdb hdb sdc hdc sdd hdd ; do         # Auf  sr0 sr1 sr2 sr3  keine Partitionen mit .iso-Datei erwartet
if test $VON = - ; then                                # damit nichts anderes mehr gemountet wird  nachdem .iso gefunden 
mkdir /$I0$J0  >> /dev/zero 2>&1 ; /bin/mount /dev/$I0$J0 /$I0$J0 >> /dev/zero 2>&1         # Nur in initrd noetig, auerhalb Partition mit .iso als gemountet angesehen  
umount /von  >> /dev/zero 2>&1 ; mount /$I0$J0/SYS_Linux.iso  /von -t udf -o ro,unhide,loop   >> /dev/zero 2>&1  
if test $? -eq 0 ; then VON=/$I0$J0 ; fi ; 
# echo " getestet:  /dev/"$I0$J0   "    VON = " $VON   
fi ;
done ; done 

if test $VON = - ; then 
dialog --title " no install .iso file found "  --no-collapse --colors --infobox "
 Nao foi achado nenhum arquivo SYS_Linux.iso 
    Keine Datei SYS_Linux.iso gefunden
"  6 46
sleep 4 
exit ; fi ;   



#VON=$VON/SYS_Linux.iso      #  Da mountpoint in initrd / installer anders sein kann, besser nur PartName uebergeben statt loopdevice


dialog --title " install .iso file found "  --no-collapse --colors --infobox "
      Achado:     Gefunden:
       $VON/SYS_Linux.iso 
"  6 40
sleep 4 

# Falls installiso von /initrd aus gestartet wird, Annahme dass zu verwendender Installer schon nach /initrd
#  entpackt wurde;  dann wird dieser verwendet und NICHT der Installer in der initrd auf dem .iso -File SYS_Linux.iso
#  installiert wird aber das System SYS_Linux.ext3.lzma  von SYS_Linux.iso
if test $PWD = "/initrd" ; then
dialog --title " use installer in /initrd .. "  --no-collapse --colors --infobox "
Usar programa da instalacao em /initrd ...
 Install Program in /initrd benutzen ...
"  6 46 
sleep 4
fi ;  
if test $PWD != "/initrd" ; then
mv -f /initrd /initrd.0  >> /dev/zero 2>&1  
if test -d ./boot/initrd ; then
dialog --title " use copy of the installer in ../initrd .. "  --no-collapse --colors --infobox "
Usar uma copia do programa da instalacao em ../initrd ...
   Kopie von Install Program in ../initrd benutzen ...
"  6 64 
 cp -adpPR ./boot/initrd /initrd
 else 
dialog --title " unfold installer to /initrd .. "  --no-collapse --colors --infobox "
Desdobrar programa da instalacao do .iso para /initrd ...
    Install Program vom .iso nach /initrd entpacken ...
"  6 64 
#sleep 3 
mkdir /initrd  >> /dev/zero 2>&1                                   #### test, aktivieren sobald fertig / neue initrd in .iso ist 
cd /initrd
gzip -dc /von/boot/initrd.cpio.gz | cpio -id  --no-absolute-filenames   >> /dev/zero 2>&1     #### test, aktivieren sobald fertig / neue initrd in .iso ist  
fi ;   fi ; 
sync
cd /initrd 

#touch  /initrd/isofile 
#ls --block-size=512 -s $VON/SYS_Linux.iso  >  /initrd/isofile      # Kann aber muss nicht um Installer verwendet werden als Kriterium ob von .iso-Datei installieren  
# Installer sucht selbst ob .iso -Datei vhd

# 2. Teil der Installation /initrd/init wiederholen: 
#Nach reboot waehrend der Installation, bzw solange bis erfolgreich boot ins neu installierte System geht, /initrd beibehalten
cp -f /etc/rc.d/rc.local /etc/rc.d/rc.local.0  >> /dev/zero 2>&1 

echo '( for J0 in 1 2 3 4  ; do for I0 in sda hda sdb hdb sdc hdc sdd hdd ; do  umount  /dev/$I0$J0  >> /dev/zero 2>&1 ; done ; done )'  >>  /etc/rc.d/rc.local 
echo '( cd /initrd ; mv -f /etc/rc.d/rc.local.0 /etc/rc.d/rc.local  >> /dev/zero 2>&1 ; rm -f /etc/rc.d/rc.local.0  )'  >>  /etc/rc.d/rc.local 
echo '( init 2  >> /dev/zero 2>&1  ; chroot /initrd   ./init )' >>  /etc/rc.d/rc.local 
# Klammer noetig da sonst normales rc.local zurueckkopiert bevor zusaetzliche Anweisungen  wie chroot ./init ausgefuehrt wurden

# echo "***" ; echo "*** Nach initrd chrooten und Installer starten: " ; echo "***" 

dialog --title " unmount all unused partitions .. "  --no-collapse --colors --infobox "
 Desmontar todas particoes nao-usadas ...
Alle unbenutzten Partitionen unmounten ...
"  6 46
sleep 3 


for J0 in 1 2 3 4  ; do
for I0 in sda hda sdb hdb sdc hdc sdd hdd ; do    
umount  /dev/$I0$J0  >> /dev/zero 2>&1                 # Alle nicht gebrauchten Partitionen unmounten, sonst im Installer Probleme 
done ; done 

dialog --title " start installer .. "  --no-collapse --colors --infobox "
Lancar Instalacao, modo texto, chroot /initrd
Installer starten, Textmodus, chroot /initrd 
                   ...
"  7 50
#sleep 4  Unnoetig da installer starten sowieso dauert ... 


init 2  >> /dev/zero 2>&1 ; chroot /initrd   ./init  
#cd /initrd  ; ls .   # stattdessen zum testen

exit  

