#!/bin/ash 


#   mkimg           Macht ein SYS_Linux.img -Image fuer ein USB-Key oder eine Festplatte mit einer bootbaren Partition
#                                 mit dem Installer / Rettungs-System von  SYS_Linux  
#                   Damit kann auch SYS von einer Datei SYS_Linux.iso installiert werden, die entweder in dieselbe
#                                 Partition auf USB-key oder Festplatte hinzukopiert wird, oder aber sich auf
#                                 irgendeiner anderen Partition befindet   ( durch Aufruf von installiso )
#
#                   mkimg  muss vom Ordner oberhalb ./root des SYS-Installers  in ( SYS_Linux-<Version>.tgz )  oder
#                                 des als loop (mount ...  -t udf -o loop,ro) gemounteten SYS_Linux.iso
#                                 ( wo es sich auch normalerweise befindet ) gestartet werden. 
#                          Es macht erst eine initrd.cpio.gz in ./root falls nicht schon vhd,  richtet dann eine
#                                 Datei SYS_Linux.img  mit 5 GB ein ( sodass das SYS_Linux.iso nachtraeglich noch auf
#                                 die damit bekopierte Festplatte kopiert werden kann ) , macht darauf eine Partition,
#                                  kopiert dorthin ./root , macht es bootbar,  schneidet es hinter den relevanten
#                                 Daten ab ( sodass Installer-Rescue-System alleine auch auf einen kleineren USB-key 
#                                 kopierbar wenn SYS_Linux.iso von woanders gelesen werden kann ), zipt es.

#   Falls wir von einem gemounteten Install DVD oder von einem loopgemounteten SYS_Linux.iso aus starten, 
#   achtung nur read-only,  daher kein chroot direkt dorthin, sondern zu einer Kopie des gesamten Ordners (ohne 
#   SYS_Linux.ext3.lzma und ohne den Ordnern ausser ./boot); laeuft SYS selbst, inclusive das Install-DVD / Rescue, 
#   ist dorthin chroot unnoetig und cd ausreichend  

SIZE=7800           # Groesse der auf dem USB-key einzurichtenden Partition. Sollte ca. 6000 MB sein, wegen SYS_Linux.iso
             # Falls zum download produziert, kleiner machen als aktueller USB-key, da andere USB-k. evtl. kleiner sind
SIZE00=180          # wieviel vor Kopieren der Daten mit 0 zu initialisieren ist, etwas mehr als Groesse (diese ist 170MB
                                        #   falls Dateisystem 6 GB gross)

#   abfragen welcher USB-key  benutzt werden soll

echo "SYS Installer erzeugen"  > /tmp/logfile 
rm -f logfile  > /dev/zero    2>&1      # kann ein logfile von mkcdiso sein
rm -f /tmp/scr /tmp/scr0 >> /tmp/logfile    2>&1
fdisk -l | grep "Disk /"  > /tmp/scr0
sort -o /tmp/scr -n -k 5 /tmp/scr0 
read UNW KLEINSTE REST < /tmp/scr 
KLEINSTE=${KLEINSTE%:}
echo -e "\a"
dialog --title "Devices :" --no-collapse --colors --textbox  /tmp/scr 0 0 
#fdisk -l | grep "/dev/" | grep "Linux" > scr
#dialog --title "partitions" --no-collapse --colors --textbox  scr 0 0 
dialog --title "whereto install " --no-collapse --colors --inputbox "

        \Z1 A qual unidade escrever    
      Auf welches Geraet schreiben  \Zn

"  0 0  $KLEINSTE  2> /tmp/scr   


read NACH REST < /tmp/scr  

fdisk -s $NACH > /tmp/scr0
read SIZEDEV REST < /tmp/scr0
#SIZEDEV=$(( $SIZEDEV / 976 ))        # Groesse in MB
SIZEDEV=$(( $SIZEDEV / 1050 ))     
SIZEDEV=$(( $SIZEDEV - 8 ))     
if test $SIZEDEV -lt $SIZE ; then SIZE=$SIZEDEV ; fi ;
echo " Groesse: " $SIZE MB  >> /tmp/logfile  2>&1 
dialog --title "install to .." --no-collapse --colors --infobox "

   \Z1 Escrever para $NACH 
    Auf $NACH schreiben \Zn

"  0 0 



umount "$NACH"* >> /tmp/logfile    2>&1
echo -e "u \nd \n1 \nd \n2 \nd \n3 \nd \n4 \nn \np \n1 \n129  \n+"$SIZE"M  \na \n1 \np \nw " > /tmp/fdisk.machen    
fdisk $NACH < /tmp/fdisk.machen  >> /tmp/logfile  2>&1  
sync 
sleep 5                 # damit udev das findet und $NACH1 einrichtet
fdisk -l    >> /tmp/logfile  2>&1 
NACH1="$NACH"1          #  NACH1 Name der 1. Partition auf Ziel-Geraet NACH


dialog --title "initialize partition .." --no-collapse --colors --infobox "

   \Z1 Escrever 0's para $NACH1 
    Auf $NACH1 0'en schreiben \Zn

"  0 0 

echo " Nullen schreiben: " $SIZE00 "MB"  auf $NACH1  >> /tmp/logfile   2>&1
dd if=/dev/zero of=$NACH1 bs=1M count=$SIZE00  

dialog --title "format partition .." --no-collapse --colors --infobox "

   \Z1 Formatar $NACH1 
    $NACH1 formatieren \Zn

"  0 0 

umount $NACH1   >> /tmp/logfile    2>&1
mke2fs -q -j -m 0 -b 4096 -F -O large_file,sparse_super $NACH1  >> /tmp/logfile   2>&1
sync
tune2fs -c 0 -e continue $NACH1  >> /tmp/logfile   2>&1
sync ; sleep 5
mkdir -p /nach 
mount $NACH1 /nach 







# Sachen auf USB-key $NACH1 gemountet unter /nach kopieren


# Falls vom Ordner bzw. Install-DVD aus gestartet, ist   ./initrd  bzw   initrd.cpio.gz  vhd .
# Aber falls dieses Programm gerade schon mal lief, sowie da das Vorhandensein des einen oder anderer in mkinitrd 
# als Kriterium benutzt wird, noetig vorige initrd.cpio.gz und ./initrd loeschen, das muss hier erfolgen
mkdir -p /nach/boot
rm -f .initrd.cpio.gz   >> /tmp/logfile   2>&1     # falls wir vom Ordner aus starten, ggf altes initrd.cpio.gz unbedingt loeschen 
rm -fR /nach/boot/initrd.cpio.gz  /nach/boot/initrd  >> /tmp/logfile   2>&1
cd boot        >> /tmp/logfile  2>&1 
cp -faR  vmlinuz vmlinuz-* vmlinuz.LIESMICH* initrd.cpio.gz initrd mkird  initrd.LIESMICH* memtest   /nach/boot    >> /tmp/logfile   2>&1    
cd -              >> /tmp/logfile  2>&1 # cd .. 

if test -d /nach/boot/initrd ; then
#   initrd.cpio.gz in /nach/boot machen , nicht in ./boot da evtl read-only
cd /nach/boot       >> /tmp/logfile  2>&1  
chmod +x mkird  >> /tmp/logfile   2>&1
./mkird   >> /dev/zero   2>&1      # ProgBar
sleep 10                           # da Progressbar von mkinitrd noch "weiterlaeuft"
cd -                >> /tmp/logfile  2>&1  
fi ; 
rm -fR /nach/boot/initrd   /nach/boot/mkird   >> /tmp/logfile   2>&1

mkdir -p /nach/tools
cp -faR   mkbootcd mkcdiso mkimg mkpackage auto.SlackBuild*  slack-desc* vorbereitung* autorun.inf installiso.LIESMICH*   /nach/tools  >> /tmp/logfile    2>&1
cp -faR   LIESMICH* *.txt* *.rtf* *.pdf* *.png* SYS_Linux.img.LIESMICH*     /nach  >> /tmp/logfile    2>&1   # evtl *.gz 
mkdir -p /nach/tools/boot
cp -faR   ./boot/mkird ./boot/DOS ./boot/isolinux   /nach/tools/boot  >> /tmp/logfile    2>&1
sync ; sleep 5 

chmod +x /nach/boot/vmlinuz /nach/tools/mkbootcd /nach/tools/mkcdiso /nach/tools/mkimg mkpackage /nach/tools/boot/mkird  >> /tmp/logfile  2>&1   
chmod +x /nach/tools/boot/DOS/loadlin.exe /nach/tools/boot/DOS/linux.bat /nach/tools/boot/isolinux/isolinux.bin  >> /tmp/logfile  2>&1 


cd /nach/boot        >> /tmp/logfile  2>&1    # Wir koennen hier aber nicht dauernd bleiben, da spaeter umount / resize 

ls vmlinuz-* > /tmp/scr                      # hier kann nicht umgeleitet werden
read VMLX REST < /tmp/scr   >> /tmp/logfile  2>&1
rm -f $VMLX   >> /tmp/logfile  2>&1
ln -sf vmlinuz $VMLX  >> /tmp/logfile  2>&1   # denn auf dem DVD ist der link oft schlecht 

cat << ENDOFTEXT >./lilo.reset.sh
#!/bin/ash 

fdisk -l | grep "Disk /"  > /tmp/scr0
sort -o /tmp/scr -n -k 5 /tmp/scr0 
read UNW KLEINSTE REST < /tmp/scr 
KLEINSTE=\${KLEINSTE%:}
dialog --title "Devices / Geraete / Unidades :" --no-collapse --colors --textbox  /tmp/scr 0 0 
dialog --title "whereto install " --no-collapse --colors --inputbox "

        \Z1 A qual unidade escrever    
      Auf welches Geraet schreiben  \Zn

"  0 0  \$KLEINSTE  2> /tmp/scr   

read NACH REST < /tmp/scr  

dialog --title "install to .." --no-collapse --colors --infobox "

   \Z1 Escrever para \$NACH 
    Auf \$NACH schreiben \Zn

"  0 0 

lilo -b \$NACH -C ./lilo.conf -s /tmp  -m ./lilo.map  # > /tmp/logfile 2>&1 
sync 

exit
ENDOFTEXT
chmod +x ./lilo.reset.sh 


# bootbar machen ...

cat << ENDOFTEXT >./lilo.conf
boot=$NACH 
prompt
timeout=40
lba32
compact
change-rules
reset
menu-title="SYS    Installer / Rescue System"
menu-scheme=Wb 

image=./vmlinuz
initrd=./initrd.cpio.gz
label=SYS
root=/dev/ram0
read-write
vga=791
append = "ramdisk_size=6666 edd=on sysrq_always_enabled=1 usbcore.autosuspend=-1 acpi=off noapic loglevel=1"

image=./vmlinuz
initrd=./initrd.cpio.gz
label="--notebook"
root=/dev/ram0
read-write
vga=788
append = "ramdisk_size=6666 edd=on sysrq_always_enabled=1 usbcore.autosuspend=-1 irqpoll acpi=off noapic loglevel=1"

image=./vmlinuz
initrd=./initrd.cpio.gz
label="--old-laptop"
root=/dev/ram0
read-write
vga=785
append = "ramdisk_size=6666 edd=off sysrq_always_enabled=1 usbcore.autosuspend=-1 irqpoll pci=off acpi=off noapic loglevel=1"

image=./vmlinuz
initrd=./initrd.cpio.gz
label="--scsi"
root=/dev/ram0
read-write
vga=785
append = "ramdisk_size=6666 edd=off scsi_mod.scan=async hda=scsi hdb=scsi hdc=scsi hdd=scsi sysrq_always_enabled=1 usbcore.autosuspend=-1 irqpoll pci=off nomce acpi=off noapic noapm loglevel=1"

image=./vmlinuz
initrd=./initrd.cpio.gz
label="--edd-off"
root=/dev/ram0
read-write
vga=785
append = "ramdisk_size=6666 edd=off sysrq_always_enabled=1 usbcore.autosuspend=-1 acpi=off noapic loglevel=1"

image=./vmlinuz
initrd=./initrd.cpio.gz
label="--broken-bios"
root=/dev/ram0
read-write
vga=0
append = "ramdisk_size=6666 edd=off sysrq_always_enabled=1 usbcore.autosuspend=-1 hwprobe=off acpi=off noapic loglevel=3"

image=./vmlinuz
initrd=./initrd.cpio.gz
label="--problem"
root=/dev/ram0
read-write
vga=0
append = "ramdisk_size=6666 edd=off scsi_mod.scan=async sysrq_always_enabled=1 usbcore.autosuspend=-1 hwprobe=off irqpoll nomce nobios pci=off initcall_debug acpi=off noapic noapm loglevel=5"

image=./memtest
label=memtest

ENDOFTEXT

#sleep 5
cd -          >> /tmp/logfile  2>&1 

# Datei-System auf NACH1 maximal verkleinern um zu defragmentieren,  wieder vergrĂssern, hinten mit Nullen fuellen,
# abschneiden damit es auch auf kleinere USB-keys passt, zippen

df -k $NACH1 | grep $NACH1 > /tmp/scr 
read UNW1 UNW2 SIZE0 REST < /tmp/scr
SIZE0=$(($SIZE0 / 960 )) 
SIZE0=$(($SIZE0 + 2 ))             # etwas mehr, sowie aufrunden 
#echo " SIZE0 : "  $SIZE0

dialog --title "optimize .." --no-collapse --colors --infobox "

   \Z1 optimalizar ... 
    optimieren ... \Zn

"  0 0 

umount /nach   >> /tmp/logfile    2>&1
resize2fs -FfM $NACH1 >> /tmp/logfile 2>&1 
sync ; sleep 30       #  lange Zeit lassen sonst Problkeme
echo "size: " $SIZE M    >> /tmp/logfile 2>&1 
#resize2fs -Ff $NACH1 "$SIZE"M  >> /tmp/logfile 2>&1 
resize2fs -Ff $NACH1  >> /tmp/logfile 2>&1 
sync ; sleep 30 

mount $NACH1 /nach 
# Aus komischen Gruenden muss lilo nach dem defragmentieren / resizen (nochmal) ausgefuehrt werden ...
cd /nach/boot        >> /tmp/logfile  2>&1 
lilo -C ./lilo.conf -S /tmp  -m ./lilo.map     >> /tmp/logfile  2>&1 
cd -                 >> /tmp/logfile  2>&1 
sync ; sleep 10   # syncen bei USB-Geraeten dauert lange , und comitt bei ext3fs ist jetzt 5 sek ...

# Eigentlich braeuchte nur ein bischen genullt werden hinter den Daten.  Aber man weiss nicht, ob dies sequentiell
# erfolgt, daher nullen wir genuegend.  ( 20 MB leere Datei nach den Daten)
touch /nach/leer
sync
sleep 1
echo " Nullen schreiben:  20 MB"  in /nach/leer  >> /tmp/logfile   2>&1
dd if=/dev/zero of=/nach/leer bs=1M count=20 >>/tmp/logfile    2>&1
sync ; sleep 10
rm -f /nach/leer 
sync ; sleep 10
# Daten wurden oben durch resize -M nach vorne gebracht, nach SIZE0 (sicherheitshalber etwas groesser, dahinter sind ja 0'en)
# abschneiden - allerdings Geraet NACH, nicht Partition NACH1, zippen
sync
umount /nach  >> /tmp/logfile     2>&1 

dialog --title "copy/zip to /SYS_Linux.img.gz .." --no-collapse --colors --infobox "

   \Z1 copiar/compactar para /SYS_Linux.img.gz 
 Nach /SYS_Linux.img.gz kopieren/kompaktieren \Zn

"  0 0 

dd if=$NACH of=/SYS_Linux.img bs=1M count=$SIZE0 >>  /tmp/logfile     2>&1
gzip -qf -9 /SYS_Linux.img
sync

( cd / ; md5sum SYS_Linux.img.gz > /SYS_Linux.img.gz.md5 ; ls -l SYS_Linux.img.gz >> /SYS_Linux.img.gz.md5 ; cd - ) # & 
sleep 5       # um ggf Fehlermeldungen zu lesen

# Nachdem das .img fertig / kopiert ist, bringen wir die Partition auf dem USB-key auf max. Groesse
echo -e "u \nd \n1 \nn \np \n1 \n129  \n  \na \n1 \np \nw " > /tmp/fdisk.machen    
fdisk $NACH < /tmp/fdisk.machen  >> /tmp/logfile  2>&1  
resize2fs -Ff $NACH1  >> /tmp/logfile  2>&1
sync


mount $NACH1 /nach                      #  Wir wollen im .img kein /var/logfile, kopieren das daher erst jetzt
mkdir -p /nach/var/log  >>/tmp/logfile    2>&1
cp /tmp/logfile /nach/var/log/logfile.mkimg  > /dev/zero  2>&1
cp /SYS_Linux.img.gz /nach  > /dev/zero 2>&1
cp /SYS_Linux.img.gz.md5 /nach > /dev/zero 2>&1
umount /nach   > /dev/zero  2>&1
rm /tmp/scr /tmp/scr0 /tmp/fdisk.machen  > /dev/zero  2>&1 # /tmp/logfile  


dialog --title "can be repaired with lilo.reset.sh" --no-collapse --colors --infobox "

        \Z1 Se o boot nao funcionar,
   executar lilo.reset.sh na chave-USB

      Wenn booten nicht funktioniert,
   lilo.reset.sh auf USB-key ausfuehren \Zn

"  0 0 

sleep 5
reset 


echo -e "\a"

exit