#!/bin/ash
#
#
#
 . /usr/share/grub/mini/getname.sh
 FORMAT=none
 rm /tmp/hdd.tmp 2>/dev/null
 rm /tmp/fdisk.tmp 2>/dev/null
 rm /tmp/fdisk1.tmp 2>/dev/null
 rm /tmp/format.tmp 2>/dev/null
 rm /tmp/mnt.tmp 2>/dev/null
 rm /tmp/swap.tmp 2>/dev/null
 rm /tmp/target.tmp 2>/dev/null
#
#
#
# *** INTRO TEXT ***
 clear
 echo ""
 echo " Welcome to $NAME installation script!"
 echo ""
 echo " You will be asked to choose a target partition, on which your new $NAME"
 echo " will be installed. The target partition must be at least 1 GB in size."
 echo ""
 echo " You can choose any kind of the target partition (even NTFS or Linux swap),"
 echo " but it will be formatted to ext4 filesystem, meaning that all the data on"
 echo " this partition will be lost."
 echo ""
 echo " NOTE: it strongly recommended, so that you will make a backup of all your"
 echo " important data before installing any new operating system in your PC."
 echo ""
 echo -n " Press Enter to continue..."; read
#
#
#
# *** TARGET DISK ***
 if [ -e /dev/nvme0n1 ]; then
   clear
   echo ""
   echo "Please remove all USB disks."
   echo -n "Press ENTER when you are done."
   read
 fi
 rm /dev/sda* 2>/dev/null
 mdev -s
 echo ""
 if [ ! -e /dev/sda ]; then
   echo "ERROR: the disk '/dev/sda' not found."
   exit
   else
   if [ ! -e /dev/sda1 ]; then
     echo "No partitions found on /dev/sda."
     read -p  "Should I create them for you (y/n)?: " ANSWER
     ANSWER=y$ANSWER
     if [ $ANSWER = yy ]; then
       echo "Working, please wait..."
       if [ -e /sys/firmware/efi ]; then
         parted -s /dev/sda mklabel gpt
         parted -s /dev/sda mkpart fat32 0% 5%
         parted -s /dev/sda mkpart ext2 5% 40%
         parted -s /dev/sda mkpart ext2 40% 90%
         parted -s /dev/sda mkpart linux-swap 90% 100%
         parted -s /dev/sda set 1 esp on
         parted -s /dev/sda set 4 swap on
         mdev -s
         mkdosfs /dev/sda1
         mkfs.ext4 /dev/sda3
         mkswap /dev/sda4
         DEV=sda2
         else
         parted -s /dev/sda mklabel msdos
         parted -s /dev/sda mkpart primary ext2 0% 40%
         parted -s /dev/sda mkpart primary ext2 40% 90%
         parted -s /dev/sda mkpart primary linux-swap 90% 100%
         parted -s /dev/sda set 3 swap on
         mdev -s
         mkfs.ext4 /dev/sda2
         mkswap /dev/sda3
         DEV=sda1
       fi
       clear
       echo ""
       echo "It is recommended to install 4MLinux on /dev/$DEV."
       echo -n "Press ENTER to continue..."
       read
       else
       exit
     fi
   fi
 fi
#
#
#
# *** TARGET PARTITION ***
 clear
 fdisk -l /dev/sda
 echo ""
 echo "On which partition should your new $NAME be installed?"
 read -p "Give the name (e.g., sda1) of the target partition: " TARGET
 echo ""
 if [ x$TARGET = x ]; then
   echo "ERROR: no partition has been chosen."
   exit
 fi
 if [ $TARGET = sda ]; then
   echo "ERROR: you must choose a partition (and not a disk)."
   exit
 fi
 if [ ! -b /dev/$TARGET ]; then
   echo "ERROR: the block device '/dev/$TARGET' does not exist."
   exit
 fi
#
#
#
# *** HDD ERROR ***
 echo $TARGET | grep sda > /tmp/hdd.tmp
 if [ ! -s /tmp/hdd.tmp ]; then
   echo "ERROR: the target partition must be placed on /dev/sda."
   exit
 fi
#
#
#
# *** LBA ERROR ***
 fdisk -l /dev/sda | grep Ext | grep $TARGET > /tmp/hdd.tmp
 if [ -s /tmp/hdd.tmp ]; then
   echo "ERROR: '$TARGET' is not a valid partition."
   exit
 fi
#
#
#
# *** ROOTFS ERROR ***
 ERROR=no
 ROOTFS=$(cat /etc/rootfs)
 if [ $ROOTFS = /dev/$TARGET ]; then
   ERROR=yes
 fi
 if [ -h /dev/$TARGET ]; then
   if [ $ROOTFS = $(readlink /dev/$TARGET) ]; then
     ERROR=yes
   fi
 fi
 if [ $ERROR = yes ]; then
   echo "ERROR: '$TARGET' is already used by $NAME."
   echo "You have to choose another partition."
   exit
 fi
#
#
#
# *** SWAP WARNING ***
 fdisk -l /dev/sda | grep 'Linux swap' | grep /dev/$TARGET > /tmp/fdisk.tmp
 if [ -s /tmp/fdisk.tmp ]; then
   echo "WARNING: '$TARGET' is a swap partition that you may need."
   read -p  "Are you sure that you want to install $NAME on /dev/$TARGET (y/n)?: " ANSWER
   ANSWER=y$ANSWER
   if [ $ANSWER = yy ]; then
     touch /tmp/swap.tmp
     echo ""
     else
     echo "Bye !"
     exit
   fi
 fi
#
#
#
# *** NTFS WARNING ***
 if [ ! -s /tmp/fdisk.tmp ]; then
   fdisk -l /dev/sda | grep NTFS | grep /dev/$TARGET > /tmp/fdisk.tmp
   if [ -s /tmp/fdisk.tmp ]; then
     echo "WARNING: '$TARGET' is NTFS formatted. Your Windows may be there!"
     read -p  "Are you sure that you want to install $NAME on /dev/$TARGET (y/n)?: " ANSWER
     ANSWER=y$ANSWER
     if [ $ANSWER = yy ]; then
       echo ""
       else
       echo "Bye !"
       exit
     fi
   fi
 fi
#
#
#
# *** HFS WARNING ***
 if [ ! -s /tmp/fdisk.tmp ]; then
   fdisk -l /dev/sda | grep HFS | grep /dev/$TARGET > /tmp/fdisk.tmp
   if [ -s /tmp/fdisk.tmp ]; then
     echo "WARNING: '$TARGET' is HFS formatted. Your Mac may be there!"
     read -p  "Are you sure that you want to install $NAME on /dev/$TARGET (y/n)?: " ANSWER
     ANSWER=y$ANSWER
     if [ $ANSWER = yy ]; then
       echo ""
       else
       echo "Bye !"
       exit
     fi
   fi
 fi
#
#
#
# *** EFI WARNING ***
 if [ ! -s /tmp/fdisk.tmp ]; then
   fdisk -l /dev/sda | grep EFI | grep /dev/$TARGET > /tmp/fdisk.tmp
   if [ -s /tmp/fdisk.tmp ]; then
     echo "WARNING: '$TARGET' is labled EFI. Your boot manager may be there!"
     read -p  "Are you sure that you want to install $NAME on /dev/$TARGET (y/n)?: " ANSWER
     ANSWER=y$ANSWER
     if [ $ANSWER = yy ]; then
       echo ""
       else
       echo "Bye!"
       exit
     fi
   fi
 fi
#
#
#
# *** FAT WARNING ***
 if [ ! -s /tmp/fdisk.tmp ]; then
   fdisk -l /dev/sda | grep FAT | grep /dev/$TARGET > /tmp/fdisk.tmp
   if [ -s /tmp/fdisk.tmp ]; then
     echo "WARNING: '$TARGET' is FAT formatted. Your data may be there!"
     read -p  "Are you sure that you want to install $NAME on /dev/$TARGET (y/n)?: " ANSWER
     ANSWER=y$ANSWER
     if [ $ANSWER = yy ]; then
       echo ""
       else
       echo "Bye!"
       exit
     fi
   fi
 fi
#
#
#
# *** MOUNTPOINT ***
 mkdir /mnt/$TARGET 2>/dev/null
 umount /dev/$TARGET 2>/dev/null
 partprobe /dev/sda
 ntfs-3g /dev/$TARGET /mnt/$TARGET 2>/dev/null
 mount /dev/$TARGET /mnt/$TARGET 2>/dev/null
 rmdir /mnt/$TARGET 2>/dev/null
 mount > /tmp/mnt.tmp
#
#
#
# *** BTRFS FORMAT ***
 echo 'mkfs.btrfs -f -L $NAME /dev/$TARGET' >> /tmp/format.tmp
 if [ ! -s /tmp/fdisk.tmp ]; then
   cat /tmp/mnt.tmp |grep btrfs |grep /mnt/$TARGET > /tmp/fdisk.tmp
   if [ -s /tmp/fdisk.tmp ]; then
     FORMAT=btrfs
   fi
 fi
#
#
#
# *** JFS FORMAT ***
 echo 'mkfs.jfs -q -L $NAME /dev/$TARGET' >> /tmp/format.tmp
 if [ ! -s /tmp/fdisk.tmp ]; then
   blkid /dev/$TARGET | grep jfs > /tmp/fdisk.tmp
   if [ -s /tmp/fdisk.tmp ]; then
     FORMAT=jfs
   fi
 fi
#
#
#
# *** REISERFS FORMAT ***
 echo 'mkfs.reiserfs -f -f --label $NAME /dev/$TARGET' >> /tmp/format.tmp
 if [ ! -s /tmp/fdisk.tmp ]; then
   cat /tmp/mnt.tmp |grep reiserfs |grep /mnt/$TARGET > /tmp/fdisk.tmp
   if [ -s /tmp/fdisk.tmp ]; then
     FORMAT=reiserfs
   fi
 fi
#
#
#
# *** XFS FORMAT ***
 echo 'mkfs.xfs -f -L $NAME /dev/$TARGET' >> /tmp/format.tmp
 if [ ! -s /tmp/fdisk.tmp ]; then
   cat /tmp/mnt.tmp |grep xfs |grep /mnt/$TARGET > /tmp/fdisk.tmp
   if [ -s /tmp/fdisk.tmp ]; then
     FORMAT=xfs
   fi
 fi
#
#
#
# *** EXT2/3/4 FORMAT ***
 for i in 2 3 4; do
   echo 'mkfs.ext'$i' -F -L $NAME /dev/$TARGET' >> /tmp/format.tmp
   if [ ! -s /tmp/fdisk.tmp ]; then
     cat /tmp/mnt.tmp |grep ext$i |grep /mnt/$TARGET > /tmp/fdisk.tmp
     if [ -s /tmp/fdisk.tmp ]; then
       FORMAT=ext$i
     fi
   fi
 done
#
#
#
# *** FILESYSTEM ***
 LIST="none btrfs ext2 ext3 ext4 jfs reiserfs xfs"
 if [ $FORMAT = none ]; then
   FORMAT=ext4
   LIST="btrfs ext2 ext3 ext4 jfs reiserfs xfs"
 fi
 echo "/dev/$TARGET will be formatted to $FORMAT."
 read -p "Do you wish to change this (y/n)?: " ANSWER
 ANSWER=y$ANSWER
 if [ $ANSWER = yy ]; then
   echo ""
   read -p "Your choice ($LIST)?: " ANSWER
   for i in $LIST; do
     if [ $ANSWER = $i ]; then
       FORMAT=$i
     fi
   done
 fi
#
#
#
# *** BOOT ***
 LOADER=LILO
 if [ $FORMAT = btrfs ]; then
   LOADER=SYSLINUX
 fi
 if [ -e /sys/firmware/efi ]; then
   LOADER=GRUBMINI
 fi
 clear
 echo ""
 read -p "Is $NAME to be the only operating system in your PC (y/n)?: " ANSWER
 echo ""
 ANSWER=y$ANSWER
 MBR=yes
 if [ $ANSWER = yy ]; then
   echo "A boot loader ($LOADER) will be installed for you."
   echo "You need $LOADER to boot your new $NAME."
   else
   echo "A new boot loader ($LOADER) will be installed."
   echo "It will replace the existing one !!!"
   echo ""
   echo "After booting your new $NAME, you can install GRUB."
   echo "This will enable you to access all other operating systems."
 fi
 echo ""
 echo -n "Press ENTER to continue..."; read
#
#
#
# *** SUMMARY ***
 clear
 echo ""
 echo "SUMMARY OF CHANGES:"
 echo "1) $NAME will be installed on /dev/$TARGET,"
 if [ $FORMAT = none ]; then
   echo "2) /dev/$TARGET will NOT be formatted,"
   else
   echo "2) /dev/$TARGET will be formatted to $FORMAT,"
 fi
 if [ $ANSWER = yy ]; then
   echo "3) A boot loader ($LOADER) will be installed for you."
   else
   echo "3) A new boot loader ($LOADER) will be installed for you."
 fi
 echo ""
 echo "WARNING: This is the last question:"
 read -p "Do you wish to start the installation right now (y/n)?: " ANSWER
 ANSWER=y$ANSWER
 if [ ! $ANSWER = yy ]; then
   echo "Bye!"
   exit
 fi
#
#
#
# *** INSTALLATION ***
 if [ -e /tmp/swap.tmp ]; then
   swapoff -a
   else
   umount /dev/$TARGET
 fi
 fdisk -l /dev/sda | grep Disklabel | grep dos > /tmp/fdisk1.tmp
 echo $TARGET > /tmp/target.tmp
 ID=$(cat /tmp/target.tmp | cut -c 4,5)
 if [ ! $FORMAT = none ]; then
   if [ -s /tmp/fdisk1.tmp ]; then
     sfdisk --part-type --color=never /dev/sda $ID 83
     else
     sfdisk --part-type --color=never /dev/sda $ID 0FC63DAF-8483-4772-8E79-3D69D8477DE4
   fi
   cat /tmp/format.tmp | grep $FORMAT > /tmp/format.tmp1
   mv /tmp/format.tmp1 /tmp/format.tmp
   . /tmp/format.tmp
 fi
 mkdir /mnt/$TARGET 2>/dev/null
 mount /dev/$TARGET /mnt/$TARGET
 mkdir /mnt/$TARGET/proc
 cd /var/$NAME/core
 export EXTRACT_UNSAFE_SYMLINKS=1
 for i in $(ls); do
   tar -xzv -f $i -C /mnt/$TARGET
 done
 cd ..
 for i in $(ls | grep addon); do
   echo "Copying $i"
   cp -a $i /mnt/$TARGET/etc/init.d
 done
 if [ -e /lib/firmware ]; then
   echo "Copying firmware. Please wait..."
   cp -a /lib/firmware /mnt/$TARGET/lib
 fi
 if [ -e /opt/basilisk ]; then
   echo "Copying Basilisk. Please wait..."
   mkdir -p /mnt/$TARGET/usr/lib
   cp -a /opt/basilisk /mnt/$TARGET/usr/lib
 fi
 if [ -e /usr/share/jwm/apps/inst2hd.sh ]; then
   . /usr/share/jwm/apps/inst2hd.sh
 fi
 cp -a core /mnt/$TARGET/etc/init.d
 cp -a /etc/inittab /mnt/$TARGET/etc
 . /usr/share/grub/mini/fsck.sh
 echo "Installing boot loader:"
 . /usr/share/grub/mini/install.sh
 if [ -e /tmp/srv.tmp ]; then
   mkdir -p /mnt/$TARGET/usr/apache2
 fi
 umount /dev/$TARGET 2>/dev/null
 echo "Installation is now complete."
