#!/bin/ash

echo " *** copysys:   macht Installierung  incl. lilo *** "

mkdir -p /von  ; mkdir -p /nach 


# 1. Einlesen der Install-Details von  installinfo  ,  insbesondere nach welcher Partition NACH auf Platte NACO0 installieren

read TST SIZE NACH0  NACH  VON  KLEINER  WINDOWS REST  <  installinfo 
echo " Install-Platte:  "  $NACH0  "  Install-Partition:   " $NACH   " Groesse: " $SIZE 

echo  " Installieren  nach Partition  "   $NACH   "  auf Platte   "   $NACH0     


# 2. Suchen in welcher device VON das Install-CD/DVD ist  

VON=" "
for I0 in hda hdb hdc hdd sda sdb sdc sdd ; do
#test /dev/$I0 = $NACH ; if test $? -ne 0 ; then       # Abfrage z.Ausschluss v. Hard-Disk gibt Probleme / Halten / Rueckfrage 
/bin/mount /dev/$I0 /von -t iso9660 -o ro  ;  if test $? -ne 0 ; then VON=/dev/$IO ; fi  
#fi ; 
done
#if test $VON = " " ; then echo " Problem Install-Quelle " ; sleep 20 ; ./rescue ; fi ;        #  wurde keine Quelle gefunden 
# Ignorieren da a) Fehlermeldung falls schon gemountet und b) CD jedenfalls zum Start der Install da war
#echo  " 3. Installieren  von:  "   /dev/$IO 

# Jetzt ist jedenfalls das CD von dem zu installieren ist nach /von gemountet ...  auch wenn device VON unbestimmt ist :(


# 3. kompaktiertes System SYS_Linux.ext3.lmza in device VON   nach  Partition  NACH  deskompaktieren / kopieren
cd /von 

ls *.ext3.lzma > /scr       # Achtung wir sind auf read-only FS  vom CD/DVD  = /von , schreiben also nach /... 
#ls *.ext3.lrz > /scr       # lzo, lzr ist nicht in busyboz
read NV REST < /scr    #  NV ist meist SYS_Linux[-Version].ext3.lzma .   Ggf Art .lzma , .bz2 abspalten, nachf. nutzen 
echo " Auf CD/DVD kompaktierte Datei: "  $NV "  gefunden.  "

#/bin/dd if=/von/SYS_Linux.ext3 of=$NACH                # falls normal kopiert
#/bin/bunzip2 -c /von/SYS_Linux.ext3.bz2 > $NACH         # falls kompaktiert  mit bzip2 -9 -fck $VON > /SYS_linux.ext3.bz2 
/bin/lzmacat $NV > $NACH                 #   "   lzma -7 -fk  
sync 
resize2fs -fp $NACH
sync
e2fsck -fy $NACH 
e2fsck -y $NACH    #  2. mal nur noch wenn unclean 
sync 
/bin/mount $NACH /nach    ; if test $? -ne 0 ; then echo " Problem Install-Ziel " ; sleep 20; ./problem ; fi ;
#ROOT=/nach upgradepkg --install-new --reinstall /von/"*".tgz
#/von/'*'.sh   
#echo  "System kopiert  und als  /nach  gemountet"

echo " was jetzt alles gemountet ist:  " 
/bin/mount
echo " gerade installiertes System:    device: "   $NACH  "gemountet: /nach"    
/bin/ls  /nach


# 4. fstab , lilo.conf   der Installierung   anpassen,  lilo im MBR von Platte   NACH0  installieren 


echo " 4. lilo.conf  und fstab  auf "  $NACH " einrichten:     Partition-Nr mit altem System: "  $WINDOWS
 
cd /nach/etc


# 4.1. fstab machen



#mkdir  -p /nach/"$NACH0"1 ; mkdir -p /nach/"$NACH0"2 ; mkdir -p /nach/"$NACH0"3 ; mkdir -p /nach/"$NACH0"4   # kann schon vorher in SYS gemacht werden  
mkdir -p /nach/hda1 ; mkdir -p /nach/hda2 ; mkdir -p /nach/hda3 ; mkdir -p /nach/hda4 
mkdir -p /nach/sda1 ; mkdir -p /nach/sda2 ; mkdir -p /nach/sda3 ; mkdir -p /nach/sda4 
 
if test $WINDOWS -ne 0 ; then   mkdir -p /nach/Windows ; fi ;  

cat fstab > fstab.old
echo $NACH "   /  auto  defaults  0  1  " > fstab
echo "$NACH0"1  /hda1 "  auto   defaults    0    0 " >> fstab                 #     dgl.
echo "$NACH0"2  /hda2 "  auto   defaults    0    0 " >> fstab                 #     dgl.
echo "$NACH0"3  /hda3 "  auto   defaults    0    0 " >> fstab                 #     dgl.
echo "$NACH0"4  /hda4 "  auto   defaults    0    0 " >> fstab                 #     dgl.
if test $WINDOWS -ne 0 ; then echo $NACH0$WINDOWS "  /Windows  ntfs-3g   defaults   0   0   " >> fstab ; fi ;   # ntfs-3g muss in SYS vhd sein     
grep -v " / " fstab.old >> fstab 
sync 


# 4.2.  lino.conf , lilo


rdev /nach/boot/vmlinuz $NACH ; rdev -R /nach/boot/vmlinuz 1       # geht nur wenn link vmlinuz / initrd.gz zu genauem Name vhd 
rdev /nach/boot/initrd $NACH ; rdev -R /nach/boot/initrd 1  

echo "  lilo.conf / lilo auf " $NACH " mit boot-device " $NACH0 "  einrichten :  " 


cat lilo.conf > lilo.conf.old      
echo "  root = " $NACH  "  "  > lilo.conf     # neue root-Zeile
grep -v "root" lilo.conf.old  >> lilo.conf    # Kopie von lilo.conf  OHNE  alter  root -Zeile
if test $WINDOWS -ne 0 ; then  echo "#other = "  $NACH0$WINDOWS >> lilo.conf ;    echo "#  label = Windows " >> lilo.conf ; fi ;

chroot /nach  /bin/mount proc /proc -t proc

chroot /nach   /sbin/lilo -b $NACH0    # letzteres weg falls Probleme

if test $? -ne 0  ; then  


cd /nach/boot        #  damit alles relativ zur root des Install-Systemes ausgegeben wird
# Hier werden nur  vmlinuz- ... und initrd.gz- ...   toleriert
ls vmlinuz-* > tmp0
sort -n tmp0 > tmp2     # den letzten Kernel nehmen     Achtung, Var. tmp nicht benutzen, ist hier System-Ordner
read KERNEL  REST < tmp2             # kernel-Name mit /boot
ls initrd.gz-* > tmp1
sort -r tmp1 > tmp2      
read INITRD  REST < tmp2
echo "Fuer lilo nehmen, in /boot gefunden:   kernel / initrd :  "   $KERNEL  $INITRD 
cd /nach  
echo -e "append=" \\c   ;  echo -e  '"' \\c  ;  echo -e "devfs=nomount noapic nolapic acpi=ht splash=silent" \\c ; echo -e '"' 
echo "
boot = $NACH0
root = $NACH
prompt
menu-scheme=Wb 
timeout = 50
change-rules
  reset
      " > tmp1 
# Erst genauen Kernel-Namen versuchen, falls erfolglos, vmlinuz / initrd
cat tmp1 > etc/lilo.conf
# $KERNEL=vmlinuz-2.6.24-rc3-git1-i486-1mn 
echo "
image = /boot/$KERNEL
initrd = /boot/$INITRD   
  label = SYS
  read-only
             " >> etc/lilo.conf    
chroot /nach   /sbin/lilo -b $NACH0   # Falls nachfolgendes nicht da, ist Lilo schon installiert 
#if test $? -ne 0  ; then  
#cat tmp1 > etc/lilo.conf
#echo "
#image = /boot/vmlinuz
#initrd = /boot/initrd
#  label = SYS0
#  read-only
#             " >> etc/lilo.conf    
#chroot /nach   /sbin/lilo -b $NACH0 
#fi ;
fi ;

cat etc/lilo.conf      # gut das anzusehen  


chroot /nach sync 
#echo " lilo  auf erzeugter Part. nach chroot, vergleiche mit oben"
#chroot /nach ls -l  /sbin/lilo



# 5. Konfiguration  

echo " Alle sonstigen / komplizierteren Konfigs  im Zielsystem machen "

# /bin/umount  /von  
echo " *** Ende copysys " 

