#!/bin/bash

file="/usr/share/decrypt.tmp"
dirs_file="/usr/share/home_dirs.txt"
swap_file="/usr/share/swap_file.txt"

if [ -e "$file" ]; then

    systemctl stop fly-dm

    IS_KVADRAT="0"

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

    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

    cryptsetup close homevolopen

    if [ "${IS_KVADRAT}" -ne "1" ]; then

        echo "Y" | mkfs.ext4 "/dev/astramvg/homevol"

        sed -i 's@mapper/\/homevolopen@astramvg\/homevol@' /etc/fstab
        sed -i 's@homevolopen /dev/astramvg/homevol none luks,initramfs@@'  /etc/crypttab
        sed -i 's/quiet/quiet splash/' /etc/default/grub
        sed -i "s/ unl0kr-root=\/dev\/astramvg\/homevol unl0kr-root-name=homevolopen//" /etc/default/grub

        update-grub
    else
        HOME_DEV="$(blkid -t PARTLABEL=home -o device)"
        echo "Y" | mkfs.ext4 "${HOME_DEV}"

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

        sed -i 's@\/dev\/mapper\/homevolopen@PARTLABEL=home@' /etc/fstab
        sed -i '2d'  /etc/crypttab
        sed -i 's/CRYPTSETUP=y/CRYPTSETUP=n/' /etc/cryptsetup-initramfs/conf-hook

        cmdline=$(cat /boot/cmdline)
        searchstring="unl0kr-root-uuid"
        temp=${cmdline%%$searchstring*} && indexOf=$(echo "${cmdline%%$searchstring*}" | echo ${#temp})
        res=${cmdline:0:${indexOf}-1}

        > /boot/cmdline
        echo "${res}" > /boot/cmdline

    fi

    rm $file

    update-initramfs -u

    echo "Restart system......"

    reboot

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 decrypt.service

