#!/bin/bash

# startup cleaning :
rm -f sel_images
rm -f images_folder
rm -f index.html
rm -f sel_langs
rm -f langs
rm -f images
rm -f downloads
rm -f wikis
rm -f wikis_folders
rm -f wget-lo*
rm -f kiwix
rm -f kiwix_folders
rm -f files
rm -f choice
rm -f target_disk
rm -f choosen_downloads
rm -f labtix
echo "========================"
echo "folder cleaned "

#=======================================================================
# functions

cleanup_exit() {
#read -p "enter for cleanup"
	rm -f sel_images
	rm -f images_folder
	rm -f index.html
	rm -f sel_langs
	rm -f langs
	rm -f images
	rm -f downloads
	rm -f wikis
	rm -f wikis_folders
	rm -f wget-lo*
	rm -f kiwix
	rm -f kiwix_folders
	rm -f files
	rm -f choice
	rm -f target_disk
	rm -f choosen_downloads
	rm -f labtix
exit 
}
	

additional_content() {
	echo -e \
"ftp.labdoo.org/download/install-disk/Installationsanleitung_DE.pdf"\
"\nftp.labdoo.org/download/install-disk/Labtix/"\
"\nftp.labdoo.org/download/install-disk/Labtix_manuals_tutorials/"\
"\nftp.labdoo.org/download/install-disk/README-DE.txt"\
"\nftp.labdoo.org/download/install-disk/README-EN.txt"\
"\nftp.labdoo.org/download/install-disk/change-log-22_04_LTS.txt"\
"\nftp.labdoo.org/download/install-disk/installation_guide_EN.pdf"\
>> downloads
echo "==============================="
#cat downloads

#read -p "enter"
}

downloadlist () {
	    echo "============================="
    echo "selected Downloadlist"
 #   read -p "enter"
	name="$(whoami)"
	downloadlist="$(yad --title="Select Downloadlist" --file --filename=/home/$name/ --width=600 --height=400 --button="OK" )"
	cat $downloadlist > downloads
	sed -i 'i \true' downloads
	yad --height=600 --width=700 --title=DOWNLOADS --text-align=center --text "\\n selected Downloadlist \\n" --list --checklist --column= --column=Selected-Downloads < downloads  > choosen_downloads
	if [[ $? -eq 1 ]]
		then
		echo "cleanup"
		cleanup_exit
	fi
	cat choosen_downloads | cut -d"|" -f 2 > downloads
	rm -f choosen_downloads
	echo "==========================="
	#cat downloads
	download
}

download () {
#cat downloads
#read -p "downloads4"	
mapfile -t download_array < downloads
for file in ${download_array[@]}
do
#timeout 20 wget -Q 100k -N -r -np -nH --cut-dirs=2 -P $target $file
wget -N -r -np -nH --cut-dirs=2 -P $target $file
rm -f wget-lo*
done

# download downloads end
#=======================================================================
# READY and cleaning files

yad \
--title="READY" \
--center \
--fixed \
--borders=30 \
--button="OK" \
--text=" \
\\n You've downloaded, updated or installed labdoo  \
\\n content to your install-disk(-folder). \
\\n \
\\n If you used an external harddisk as target, \
\\n please remove it and connect it again to the system \
\\n to get access to it . \
\\n \
\\n In your target folder a downloadlist was created. It \
\\n can be used to repeat the download or to update an \
\\n existing install-disk(-folder). \
\\n 
"
# remove index files
echo "----------------------"
echo "removing index.html"
find $target -type f -name "index.htm*" -exec rm {} \;
echo "----------------------"
echo "existing files and folders in target :"
ls $target
echo "----------------------"
read -p "Enter for cleanup"
if [[ $choice == 1 ]]
then
sudo umount /dev/${target_disk}1
fi

cleanup_exit
}

update_find () {
	user="$(whoami)"

folder_path="$(yad --title="Select install-disk(-folder)" --file --filename=/home/$user/ --directory --width=600 --height=400)"

echo "============folder_path:"
echo $folder_path
echo "============="

#find $folder_path  -maxdepth 1 -type f -exec stat -c '%n' {} + > existing_files
find $folder_path  -maxdepth 1 -type f | grep -v Downloadlist > files
#echo "============="
#echo "files"
cat files >> downloads
rm -f files
find $folder_path -type d -path "*images/*" > images
sed -i 's/$/\//' images
#echo "============="
#echo "images"
cat images >> downloads
rm -f images
find $folder_path -type d -path "*wikis/*" > wikis
sed -i 's/$/\//' wikis
#echo "============="
#echo "wikis"
cat wikis >> downloads
rm -f wikis
find $folder_path -maxdepth 1 -type d -path "*Labtix*" > labtix
sed -i 's/$/\//' labtix
#echo "============="
#echo "Labtix"
cat labtix >> downloads
rm -f labtix
#cat downloads
sed -i "s|$folder_path||g" downloads
sed -i "s/^/ftp.labdoo.org\/download\/install-disk/" downloads
sed -i '$aftp.labdoo.org/download/install-disk/wiki-archive/content_list/' downloads
sed -i 'i \true' downloads
yad --height=600 --width=700 --title="Update Folder And Files" --text-align=center --text "\\n files and folders to update: \\n" --list --checklist --column= --column=Selected-Downloads < downloads  > choosen_downloads
if [[ $? -eq 1 ]]
	then
	echo "cleanup"
	cleanup_exit
fi

cat choosen_downloads | cut -d"|" -f 2 > downloads
rm -f choosen_downloads
echo "==========================="
echo "files and folders to update::"
#cat downloads
echo "==========================="
target=$folder_path
#sed -i "s/^/ftp.labdoo.org\/download\/install-disk/" downloads
#read -p "downloads 2:"
#cat downloads
download
}


# functions end
#=======================================================================
# README
yad \
--title="README" \
--center \
--fixed \
--borders=30 \
--text=" \
\\n This is Labdoos <b>Install-Disk-Maker</b> to create an install-disk \
\\n and / or to download or update install-disk contents from \
\\n ftp.labdoo.org/download/install-disk/          to a \
\\n specified target ( e.g. install-disk, local folder, local server ) \ 
\\n \
\\n With this tool you will be able to :\\n \
\\n - prepare and use an external disk as install-disk for labdoo \
\\n - choose or create an existing folder as download-target \
\\n - select images, wikis, kiwix-files and additional contents \
\\n   from the labdoo-server and download them \
\\n - update contents in your install-disk(-folder) \
\\n - use an existing downloadlist from a previous download \
\\n   for downloads and updates \
\\n    \
\\n On updates only files with newer timestamps or changed sizes \
\\n will be downloaded if the file already exists !!! \
\\n \
\\n <b>Be sure there is enough space on your target !!! \
\\n \
\\n The downloads may take hours depending on your \
\\n internet-connection and the amount of downloads !!!</b> \
\\n
"
if [[ $?  -eq 1 ]]
	then
		echo "cleaning up"
		cleanup_exit
fi
# README end
#=======================================================================
# disk or folder ?

yad \
--title=Install-Disk-Maker \
--on-top \
--center \
--height=320 \
--width=550 \
--borders=20 \
--text-align=center \
--text="<b>What do you want to do ? </b>\
\\n \
\\nUse a filemanager to mount or unmount disks \
\\nor shares in the local network !!! \
\\n" \
--list --no-headers --column=number --column=choice \
1 "format and use an <b>unmounted</b> harddisk as target" \
2 "choose or create and use a folder as target" \
3 "update an existing install-disk(-folder)" \
4 "update an install-disk(-folder) with an existing Downloadlist" \
> choice

out=$?

cat choice | cut -d"|" -f 1 > choice1

echo "out : $out"

if [[ $out  -eq 1 ]]
	then
		echo "cleaning up function"
		rm choice
		rm choice1
		cleanup_exit
fi

choice="$(cat choice1)"
echo $choice
rm choice
rm choice1


# disk or folder ? end
#=======================================================================
# prepare target disk
if [[ $choice == 1 ]]
then
#Target-Disk:
disks="$(lsblk -r -o Name,Size,Type | grep disk | cut -d" " -f 1,2)"
echo $disks
#read -p "enter"
yad --on-top --center --image --fixed --borders=20 \
--title="Install-Disk-Maker" --entry \
--entry-text="sd" \
--text-align=center \
--text="  \\n <b>AVAILABLE DISKS:\
</b> \\n\\n <b>$disks</b>\\n  \
\\n <b>ALL CONTENT ON THE TARGET-DISK \
\\n WILL BE DESTROYED !!! \
\\nBE SURE ABOUT YOUR TARGET !! \
\\n THE TARGET-DISK MUST BE UNMOUNTED !!!!</b> \
\\n \
\\n Enter your sudo-password in the terminal when asked !\
\\n Use Gparted to controll the disks partitions ! \
\\n Gparted closes after 10 seconds ! \
\\n \
" 2> /dev/null > target_disk

out=$?

if [[ $out -eq 1 ]]
	then
	echo "cleaning up"
	rm target_disk
	cleanup_exit
fi

#cat target_disk
target_disk="$(cat target_disk)"
echo "target disk:"
echo $target_disk
rm -f target_disk

sudo umount /dev/${target_disk}1
sudo umount /dev/${target_disk}2
sudo umount /dev/${target_disk}3
sudo umount /dev/${target_disk}4
sudo umount /dev/${target_disk}5

sudo wipefs -a -f /dev/$target_disk

sudo dd if=/dev/urandom of=/dev/$target_disk bs=1M count=16

echo 'type=83' | sudo sfdisk /dev/$target_disk #linux supported

part=$(echo "${target_disk}1")

echo $part

sudo mkntfs -Q -v -F -L install-disk /dev/$part
sudo mount /dev/${target_disk}1 /mnt
echo "close gparted window"
sudo timeout 10 gparted /dev/$target_disk
target=/mnt
echo $target
fi

# prepare target disk end
#======================================================================
# choose folder

if [[ $choice == 2 ]]
then
#exit
name="$(whoami)"
target="$(yad --title="Choose Target" --file --filename=/home/$name/ --directory --width=600 --height=400)"
echo "----------------------"
echo "target:"
echo $target
fi

if [[ $choice == 3 ]]
then
update_find
fi

if [[ $choice == 4 ]]
then
name="$(whoami)"
target="$(yad --title="Choose Target" --file --filename=/home/$name/ --directory --width=600 --height=400)"
echo "----------------------"
echo "target:"
echo $target
downloadlist
fi


# choose folder end
#=======================================================================
#=======================================================================
# selecting images folders

# Downloadlist or server ?
yad \
--height=120 --width=400 --title=Downloadlist? \
--borders=30 \
--fixed \
--text-align=center \
--text="<b>Do you want to use an existing Downloadlist\
\\nor choose files from the Labdooserver ? </b> \
\\n" \
--button="Downloadlist" \
--button="Server"
out=$?
echo $out
#read -p "after Downloadlist ?"


if [[ $out -eq 0 ]]
then
	downloadlist
fi


# get images folders info from server
wget --no-remove-listing ftp.labdoo.org/download/install-disk/

cat index.html | grep href | cut -d"<" -f 7 | cut -d">" -f 2 | grep images | cut -d"/" -f 1 > images_folder

# adding Downloadlist-option
#sed -i '1i use existing Downloadlist ' images_folder
#sed -i '/Download/i \false' images_folder

echo "----------------------------"
#cat images_folder

sed -i '/image/i \false' images_folder


yad --height=300 --width=400 --title=Image-Languages --button="OK" --list --checklist --column= --column=Image-Folders < images_folder | cut -d"|" -f 2 > sel_langs
rm -f images_folder
echo " "
echo "--------------------"
echo "sel_langs:"
#cat sel_langs



cat sel_langs | cut -d"_" -f1 > langs
#rm -f sel_langs
echo "--------------------"
echo "Choosen Image Folders:"
cat langs
rm -f langs

# selecting images folders end
#=======================================================================
# selecting images

mapfile -t array < sel_langs
rm -f sel_langs

for i in ${array[@]}
do
wget --no-remove-listing ftp.labdoo.org/download/install-disk/$i
echo "--------------------------"
#echo "EN_English_images:"
#cat $i
cat $i | grep href | cut -d"<" -f 7 | cut -d">" -f 2 | grep LTS | cut -d"/" -f 1 > files
#read -p "selecting images, look files"
sed -i "s/^/$i\//" files
sed -i 's/$/\//' files
#read -p "selecting images, look files after sed"
rm $i
cat files >> images
rm -f files
done

# adding false lines
sed -i '/LTS/i \false' images

yad --height=300 --width=500 --title=IMAGES --button="OK" --list --checklist --column= --column=Images < images | cut -d"|" -f 2 > sel_images
rm -f images
echo "---------------------"
#cat sel_images
sed -i "s/^/ftp.labdoo.org\/download\/install-disk\//" sel_images
cat sel_images > downloads
rm -f sel_images

# selecting images end
#=======================================================================
# select wikis

# get wikis folders info from server
rm index.html

wget --no-remove-listing ftp.labdoo.org/download/install-disk/wiki-archive/wikis/

cat index.html | grep href | grep folder | cut -d"<" -f 7 | cut -d">" -f 2 | cut -d"/" -f 1 > wikis_folders

sed -i 'i \false' wikis_folders

yad --height=400 --width=200 --title=WIKIs --button="OK" --list --checklist --column= --column=WIKI-Languages < wikis_folders | cut -d"|" -f 2 > wikis
rm -f wikis_folders
sed -i "s/^/ftp.labdoo.org\/download\/install-disk\/wiki-archive\/wikis\//" wikis

echo "--------------------------"
echo "wikis"
sed -i 's/$/\//' wikis
#cat wikis 
cat wikis >> downloads
rm -f wikis
echo " "
echo "--------------------"
echo "downloads:"
#cat downloads


# select wikis end
#=======================================================================
# select kiwix

# get kiwix folders info from server
rm index.html

wget --no-remove-listing ftp.labdoo.org/download/install-disk/wiki-archive/kiwix/wikis/

cat index.html | grep href | grep folder | cut -d"<" -f 7 | cut -d">" -f 2 | cut -d"/" -f 1 > kiwix_folders

sed -i 'i \false' kiwix_folders

yad --height=300 --width=200 --title=KIWIX --button="OK" --list --checklist --column= --column=KIWIX-Languages < kiwix_folders | cut -d"|" -f 2 > kiwix
rm -f kiwix_folders
sed -i "s/^/ftp.labdoo.org\/download\/install-disk\/wiki-archive\/kiwix\/wikis\//" kiwix
echo "--------------------------"
echo "kiwix"
sed -i 's/$/\//' kiwix
#cat kiwix
cat kiwix >> downloads
rm -f kiwix

# select kiwix end
#======================================================================
# creating downloads


sed -i '$aftp.labdoo.org/download/install-disk/wiki-archive/content_list/' downloads
echo " "
echo "--------------------"
echo "downloads:"
echo "---------------------"
echo "with additional content"
#cat downloads
additional_content
timestamp="$(date  +%m-%d-%y_%H-%M)"
cat downloads > /$target/00_Downloadlist_${timestamp}
sed -i 'i \true' downloads
#cat downloads

# creating downloads end
#=======================================================================
# last check


yad --height=600 --width=700 --title=DOWNLOADS --text-align=center --text "\\n  Saved list as Downloadlist_* to target \\n" --list --checklist --column= --column=Selected-Downloads < downloads  > choosen_downloads
if [[ $? -eq 1 ]]
then
echo "cleanup"
cleanup_exit
fi

cat choosen_downloads | cut -d"|" -f 2 > downloads
rm -f choosen_downloads
echo "choosen:"
#cat downloads


# last check end
#======================================================================
# download downloads 

download





