#!/bin/bash

file="/usr/share/password.txt"
dirs_file="/usr/share/home_dirs.txt"
swap_file="/usr/share/swap_file.txt"

if [ -e "$file" ]; then
    IS_KVADRAT="0"

    if grep -q KVADRAT /etc/astra/machine_type ; then
        IS_KVADRAT="1"
    fi

    systemctl stop fly-dm

    swapinfo=$(swapon --noheadings)
    swapsize=0

    if [ "$swapinfo" != "" ]; then
        swapsize=$(( $(stat -c %s /home/swapfile)/1024 ))

        echo $swapsize > $swap_file

        swapoff -a
    fi

    dirs=($(find /home -maxdepth 1 -type d))
    length=${#dirs[@]}

    for (( i=1; i<${length}; i++ ));
    do
        d=${dirs[$i]}
        num=$(stat -c "%a" $d)
        str=${d:6}

        echo $str $num >> $dirs_file
    done

    umount /home

    hash=$(cat $file)

    if [ "$hash" != "" ]; then

        passwd=$(/usr/bin/decode.sh "$hash")

        if [ "${IS_KVADRAT}" -ne "1" ]; then
            echo $passwd | cryptsetup luksFormat "/dev/astramvg/homevol"
            echo $passwd | cryptsetup open "/dev/astramvg/homevol" "homevolopen"

            mkfs.ext4 "/dev/mapper/homevolopen"

            sed -i 's@astramvg\/homevol@mapper/\/homevolopen@' /etc/fstab
            echo "homevolopen /dev/astramvg/homevol none luks,initramfs" >> /etc/crypttab
            sed -i 's/splash//' /etc/default/grub
            sed -i "s/\(GRUB_CMDLINE_LINUX_DEFAULT=\".*\)\"/\1 unl0kr-root=\/dev\/astramvg\/homevol unl0kr-root-name=homevolopen\"/" /etc/default/grub

            update-grub
        else
            HOME_DEV="$(blkid -t PARTLABEL=home -o device)"

            echo $passwd | cryptsetup luksFormat "${HOME_DEV}"
            echo $passwd | cryptsetup open "${HOME_DEV}" homevolopen

            mkfs.ext4 /dev/mapper/homevolopen

            HOME_UUID="$(blkid -t PARTLABEL=home -o value -s UUID)"

            sed -i 's/PARTLABEL=home/\/dev\/mapper\/homevolopen/' /etc/fstab
            echo "homevolopen UUID=${HOME_UUID} none luks" >> /etc/crypttab
            sed -i 's/.*CRYPTSETUP.*/CRYPTSETUP=y/' /etc/cryptsetup-initramfs/conf-hook
            echo "$(cat /boot/cmdline) unl0kr-root-uuid=${HOME_UUID} unl0kr-root-name=homevolopen" > /boot/cmdline
        fi

        rm $file

        update-initramfs -u

        echo "Restart system......"

        reboot
    fi
else
    echo "Mounting /home......"

    mount -o rw /home

    admin_dir="/home/administrator"

    if [ ! -d "$admin_dir" ]; then

        if [ -e $swap_file ]; then
            echo "Restoring swap....."
            swapsize=$(cat $swap_file)

            fallocate -l "${swapsize}K" /home/swapfile
            dd if=/dev/zero of=/home/swapfile bs=1024 count=${swapsize} status=progress
            chmod 600 /home/swapfile
            mkswap /home/swapfile
            swapon /home/swapfile
        fi

        mkdir -p /home/.pdp

        declare -A home_dirs

        while IFS= read -r line
        do
            search=" "

            prefix=${line%%$search*}
            index=${#prefix}
            d=${line:0:index}

            perm=${line:index}

            home_dirs[$d]=$perm

        done < "$dirs_file"

        chmod -R ${home_dirs[".pdp"]} /home/.pdp

        echo "Restoring user folders......"

        cat /etc/passwd | while IFS=: read n x i g c d r
        do
            if [[ "$i" -ge 1000 && "$i" -le 65000 && ! -x "$d" ]]
            then
                search=$(echo $d | grep "/home/")

                if [ -n "$search" ];
                then
                    user_name=${d:6}
                    mkhomedir_helper $user_name
                    chown -R "$i:$g" "$d"
                    chmod -R ${home_dirs[$user_name]} "$d"
                fi
            fi
        done

        pdp-init-fs

        rm $dirs_file
        rm $swap_file
    fi
fi

systemctl disable encrypt.service
