#!/bin/sh

expand_mes () {
    source /usr/lib/setup/SeTpartitions.mes
}

# SeTpartition user-friendly rewrite Fri Dec 15 13:17:40 CST 1995 pjv
crunch () { # remove extra whitespace
    read STRING;
    echo $STRING
}
T_PX=/mnt
if [ ! -r /tmp/SeTplist ]; then
    # Give warning?
    echo "exit"
fi
dialog --title "Scanning HDD.." --infobox \
       "$scan_mes" 6 60

expand_mes
cat << EOF > /tmp/tmpscript
#!/bin/sh
dialog --title "Select root partition for Linux" --menu \\
"$select_mes" 15 70 5 \\
EOF
export COUNT=0
cat /tmp/SeTplist | while [ 0 ]; do
    read PARTITION;
    if [ "$PARTITION" = "" ]; then
	break;
    fi
    NAME=`echo $PARTITION | crunch | cut -f 1 -d ' '`
    SIZE=`echo "$PARTITION" | tr -d "*" | tr -d "+" | crunch | cut -f 4 -d ' '`
    echo "\"$NAME\" \"Linux, ${SIZE}K\" \\" >> /tmp/tmpscript
done
echo "\"---\" \"$no_partition\" \\" >> /tmp/tmpscript
echo "\"---\" \"$no_partition\" \\" >> /tmp/tmpscript
echo "2> /tmp/return" >> /tmp/tmpscript
. /tmp/tmpscript
if [ $? = 1 -o $? = 255 ]; then
    rm /tmp/tmpscript
    exit 255 # user abort
fi
ROOT_DEVICE="`cat /tmp/return`"
rm /tmp/tmpscript
if [ "$ROOT_DEVICE" = "---" ]; then
    exit 255
fi
ROOT_SIZE=`probe -l | grep "$ROOT_DEVICE " | tr -d "*" | tr -d "+" | crunch | cut -f 4 -d ' '`
# 変数展開のために再 source する
expand_mes
dialog --title "Format partition ${ROOT_DEVICE}" \
       --menu "$format_mes" 18 76 3 \
       "Format" "$format_list_01" \
       "Check" "$format_list_02" \
       "No" "$format_list_03" 2> /tmp/format
if [ $? = 1 -o $? = 255 ]; then
    rm -f /tmp/format
    exit
fi
DOFORMAT="`cat /tmp/format`"
rm -f /tmp/format
if [ ! "$DOFORMAT" = "No" ]; then
    dialog --title "select FS for ${ROOT_DEVICE}" --menu \
	   "$fs_select_mes" 22 76 6 \
	   "ext4" "$fs_list_01" \
	   "ext3" "$fs_list_02" \
	   "ext2" "$fs_list_03" \
	   "btrfs" "$fs_list_04" \
	   "xfs"  "$fs_list_05" 2> /tmp/fstype
    if [ $? = 1 -o $? = 255 ]; then
	rm -f /tmp/fstype
	exit
    fi
    FSTYPE="`cat /tmp/fstype`"
    rm -rf /tmp/fstype
    ROOT_SYS_TYPE=$FSTYPE

    expand_mes
    dialog --title "Formatting ${ROOT_DEVICE}" --infobox "$format_mes2" 6 45
    if mount | grep "$ROOT_DEVICE " 1> /dev/null 2> /dev/null ; then
	umount $ROOT_DEVICE 2> /dev/null
    fi
    if [ "$FSTYPE" = "ext4" ]; then
	modprobe ext4
	if [ "$DOFORMAT" = "Check" ]; then
	    mkfs.ext4 -L rootfs -c  $ROOT_DEVICE
	    sleep 3
	else
	    mkfs.ext4 -L rootfs  $ROOT_DEVICE
	    sleep 3
	fi
    fi

    if [ "$FSTYPE" = "ext3" ]; then
	modprobe ext3
	if [ "$DOFORMAT" = "Check" ]; then
	    mkfs.ext3 -I 128 -L rootfs  -c $ROOT_DEVICE
	    sleep 3
	else
	    mkfs.ext3 -I 128 -L rootfs $ROOT_DEVICE
	    sleep 3
	fi
    fi
    if [ "$FSTYPE" = "ext2" ]; then
	modprobe ext2
	if [ "$DOFORMAT" = "Check" ]; then
	    mke2fs -I 128 -L rootfs -c $ROOT_DEVICE
	    sleep 3
	else
	    mke2fs -I 128 -L rootfs $ROOT_DEVICE
	    sleep 3
	fi
    fi
    if [ "$FSTYPE" = "btrfs" ]; then
	modprobe btrfs
	# btrfsの場合，まずパーティションに mkfs.btrfs しておいてから
        # btrfs.sh を起動して内部にsubvolumeを作る
	mkfs.btrfs -f -L rootfs  $ROOT_DEVICE    # labelは不要かな？
	sleep 3
	echo "$ROOT_DEVICE" > /tmp/btrfs_device
	echo "/" > /tmp/btrfs_mount_point
	btrfs.sh
        # /tmp/root_subvolume_name は今回インストールする先のsubvolume名
        root_subvolume_name=`cat /tmp/root_subvolume_name | sed "s|^/||" `
    fi
    if [ "$FSTYPE" = "xfs" ]; then
	modprobe xfs
        mkfs.xfs -f -L rootfs $ROOT_DEVICE
	sleep 3
    fi

else # DOFORMAT?
    ROOT_SYS_TYPE="";
    dialog --infobox "$mount_mes" 3 60
    for filesystem in ext2 ext3 ext4 btrfs xfs ; do
	mount -t $filesystem $ROOT_DEVICE $T_PX 1> /dev/null 2> /dev/null
	if [ $? = 0 ]; then
	    ROOT_SYS_TYPE=$filesystem
	    break
	fi
    done
    if [ "$ROOT_SYS_TYPE" = "" ]; then
	dialog --title "Couldn't mount ${ROOT_DEVICE}" --msgbox "$mount_err_mes" 14 70
	exit 1
    elif [ "$ROOT_SYS_TYPE" = "btrfs" ]; then
        echo "$ROOT_DEVICE" > /tmp/btrfs_device
        echo "/" > /tmp/btrfs_mount_point
	dialog --msgbox "$btrfs_mes" 14 70
	btrfs.sh
	root_subvolume_name=`cat /tmp/root_subvolume_name | sed "s|^/||" `
    fi
    umount $T_PX 1>/dev/null 2>/dev/null
fi

# Now, we need to mount the newly selected root device:
sync
if [ "$ROOT_SYS_TYPE" != "btrfs" ]; then
    mount -t $ROOT_SYS_TYPE $ROOT_DEVICE $T_PX 1> /dev/null 2> /dev/null
    echo "$ROOT_DEVICE       /        $ROOT_SYS_TYPE        defaults   1   1" > /tmp/SeTnative
    echo $ROOT_DEVICE > /tmp/SeTrootdev
else
    mount -t $ROOT_SYS_TYPE $ROOT_DEVICE $T_PX -o subvol=$root_subvolume_name 1>/dev/null 2>/dev/null
    echo "$ROOT_DEVICE       /      $ROOT_SYS_TYPE        subvol=$root_subvolume_name  1   1" > /tmp/SeTnative
    echo $ROOT_DEVICE > /tmp/SeTrootdev
fi
# done mounting the target root partition

# More than one Linux partition
if [ ! "`cat /tmp/SeTplist | sed -n '2 p'`" = "" ]; then 
    while [ 0 ]; do # next partition loop
	cat << "EOF" > /tmp/tmpscript
#! /bin/sh
dialog --title "Select other linux partitions for /etc/fstab" --menu "$append_mes" 22 76 4 \
EOF
	cat /tmp/SeTplist | while [ 0 ]; do
	    read PARTITION;
	    if [ "$PARTITION" = "" ]; then
		break;
	    fi
	    SIZE=`echo "$PARTITION" | tr -d "*" | tr -d "+" | crunch | cut -f 4 -d ' '`
	    ALTNAME=""
	    DEVICE=`echo "$PARTITION" | tr -d "*" | crunch | cut -f 1 -d ' '`
	    if grep "$DEVICE " /tmp/SeTnative 1> /dev/null; then # it's been used
		ALTNAME="$DEVICE, for Linux, ${SIZE}K"
	    fi
	    NAME=`echo $PARTITION | crunch | cut -f 1 -d ' '`
	    if [ "$ALTNAME" = "" ]; then
		echo "\"$NAME\" \"for Linux, ${SIZE}K\" \\" >> /tmp/tmpscript
	    else
		echo "\"(used)\" \"$ALTNAME\" \\" >> /tmp/tmpscript
	    fi
	done
	echo "\"---\" \"$no_partition\" \\" >> /tmp/tmpscript
	echo "\"---\" \"$no_partition\" \\" >> /tmp/tmpscript
	echo "2> /tmp/return" >> /tmp/tmpscript
	. /tmp/tmpscript
	if [ $? = 1 -o $? = 255 ]; then
	    break;
	fi
	NEXT_PARTITION=`cat /tmp/return`
	expand_mes
	if [ "$NEXT_PARTITION" = "---" ]; then
	    break;
	elif [ "$NEXT_PARTITION" = "(in use)" ]; then
	    continue;
	fi 
        echo "$NEXT_PARTITION" > /tmp/btrfs_device
	SIZE=`probe -l | grep "$NEXT_PARTITION " | tr -d "*" | tr -d "+" | crunch | cut -f 4 -d ' '`
	dialog --title "format ${NEXT_PARTITION}" --menu "$next_mes" 20 76 3 \
	       "Format" "$next_list_01" \
	       "Check" "$next_list_02" \
	       "No" "$next_list_03" 2> /tmp/format
	if [ $? = 1 -o $? = 255 ]; then
	    rm -f /tmp/format
	    exit
	fi
	DOFORMAT="`cat /tmp/format`"
	rm -f /tmp/format
	FSTYPE=""
	if [ ! "$DOFORMAT" = "No" ]; then
            dialog --title "Select FS for ${NEXT_PARTITION}" --menu "$next_mes2" 22 74 6 \
		   "ext4" "$fs_list_01" \
		   "ext3" "$fs_list_02" \
		   "ext2" "$fs_list_03" \
		   "btrfs" "$fs_list_04" \
		   "xfs"   "$fs_list_05" 2> /tmp/fstype
	    if [ $? = 1 -o $? = 255 ]; then
		rm -f /tmp/fstype
		exit
	    fi
	    FSTYPE="`cat /tmp/fstype`"
	    rm -rf /tmp/fstype
	    expand_mes
	    dialog --title "Formatting.." --infobox "$format_mes3" 6 60
	    if mount | grep "$NEXT_PARTITION " 1> /dev/null 2> /dev/null ; then
		umount $NEXT_PARTITION 2> /dev/null
	    fi

	    if [ "$FSTYPE" = "ext4" ]; then
		modprobe ext4
		if [ "$DOFORMAT" = "Check" ]; then
		    mkfs.ext4 -c  $NEXT_PARTITION
		    sleep 3
		else
		    mkfs.ext4 $NEXT_PARTITION
		    sleep 3
		fi
	    fi

	    if [ "$FSTYPE" = "ext3" ]; then
		modprobe ext3
		if [ "$DOFORMAT" = "Check" ]; then
		    mkfs.ext3 -c $NEXT_PARTITION
		    sleep 3
		else
		    mkfs.ext3 $NEXT_PARTITION
		    sleep 3
		fi
	    fi

	    if [ "$FSTYPE" = "ext2" ]; then
		modprobe ext2
		if [ "$DOFORMAT" = "Check" ]; then
		    mke2fs -c $NEXT_PARTITION
		    sleep 3
		else
		    mke2fs  $NEXT_PARTITION
		    sleep 3
		fi
	    fi

	    if [ "$FSTYPE" = "btrfs" ]; then
		modprobe btrfs
                # rootfsの場合とは異なり，この時点ではパーティションのマウントポイントが不明なので，
                # btrfs.shの呼び出しはもうちょっと先にする
		mkfs.btrfs  $NEXT_PARTITION
		sleep 3
		echo "$NEXT_PARTITION" > /tmp/btrfs_device
	    fi
            if [ "$FSTYPE" = "xfs" ]; then
	        modprobe xfs
	        mkfs.xfs -f $NEXT_PARTITION
	        sleep 3
	    fi
	else # DOFORMAT ?  format しない場合の処理．一度 /tmpmnt にマウントして
	    #  ファイルシステムの種類を確かめる
	    if [ ! -d /tmpmnt ]; then
		mkdir /tmpmnt
	    fi
	    
	    FSTYPE="";
	    dialog --infobox "$mount_mes2" 3 60
	    for filesystem in ext2 ext3 ext4 btrfs xfs ; do
		mount -t $filesystem $NEXT_PARTITION /tmpmnt 1> /dev/null 2> /dev/null
		if [ $? = 0 ]; then
		    FSTYPE=$filesystem
		    umount /tmpmnt
		    break
		fi
	    done
	    if [ "$FSTYPE" = "" ]; then
		sleep 10
		dialog --title "Cannot mount ${NEXT_PARTITION}" --msgbox \
		       "$mount_err_mes2" 14 70
		exit
	    fi
	fi

	dialog --title "select mount point for {NEXT_PARTITION}" --inputbox "$mount_point_mes" 12 70 2> /tmp/return
	if [ $? = 255 -o $? = 1 ]; then 
	    exit 1
	fi
	MTPT=`cat /tmp/return`
	rm /tmp/return
	# Now, we need to mount the newly selected root device:
	sync
	if [ ! -d /mnt/$MTPT ]; then
	    mkdir -p /mnt/$MTPT
	fi

	if [ $FSTYPE = "" ]; then
	    for filesystem in ext2 ext3 ext4 btrfs xfs ; do
                mount -t $filesystem $NEXT_PARTITION /mnt/$MTPT 1> /dev/null 2>/dev/null
		if [ $? = 0 ]; then
		    FSTYPE=$filesystem
		    break
		fi
	    done

	    if [ "$FSTYPE" != "btrfs" ]; then
		echo "$NEXT_PARTITION       $MTPT        $FSTYPE     defaults  1   2" >> /tmp/SeTnative
	    else
                echo $MTPT > /tmp/btrfs_mount_point
                umount /mnt/$MTPT
		btrfs.sh
                root_subvolume_name=`cat /tmp/root_subvolume_name | sed "s|^/||" `
		mount -t btrfs $NEXT_PARTITION /mnt/$MTPT -o subvol=$root_subvolume_name 1>/dev/null 2>/dev/null
		echo "$NEXT_PARTITION       $MTPT        $FSTYPE     subvol=$root_subvolume_name 1   2" >> /tmp/SeTnative
	    fi
	else
	    if [ $FSTYPE != "btrfs" ]; then
		mount -t $FSTYPE $NEXT_PARTITION /mnt/$MTPT 1> /dev/null 2> /dev/null
		echo "$NEXT_PARTITION       $MTPT        $FSTYPE     defaults  1   2" >> /tmp/SeTnative
	    else
		echo $MTPT > /tmp/btrfs_mount_point
                umount /mnt/$MTPT
		btrfs.sh
		root_subvolume_name=`cat /tmp/root_subvolume_name | sed "s|^/||" `
		mount -t btrfs $NEXT_PARTITION /mnt/$MTPT -o subvol=$root_subvolume_name 1>/dev/null 2>/dev/null
		echo "$NEXT_PARTITION       $MTPT        $FSTYPE     subvol=$root_subvolume_name 1   2" >> /tmp/SeTnative
	    fi
	fi

    done # next partition loop
fi # more than one Linux partition
