#!/bin/bash
#Script for copying local files to labdoo
#Version 0.02 / 22/07/2018
#Pending at VERY least
# Put the correct order


############
# PART0: CONFIGURATION: Take the language of your choice
############
# RENAME THE 'HD_AVAILABLE_CONTENT_EN' to 'HD_AVAILABLE_CONTENT' Is the only one that will be executed



HD_AVAILABLE_CONTENT=(
	"EN,en-afristory.tar.gz,/home/labdoo/Public/wikis/EN/en-afristory,EN/en-afristory.tar.gz,480,15"
	"EN,en-algebra2go.tar.gz,/home/labdoo/Public/wikis/EN/en-algebra2go,EN/en-algebra2go.tar.gz,1200,14"
	"EN,en-boks-videos.tar.gz,/home/labdoo/Public/wikis/EN/en-boks-videos,EN/en-boks-videos.tar.gz,550000,13"
	"EN,en-catdogbooks.tar.gz,/home/labdoo/Public/wikis/EN/en-catdogbooks,EN/en-catdogbooks.tar.gz,256,16"	
	"EN,en-coreknowledge.tar.gz,/home/labdoo/Public/wikis/EN/en-coreknowledge,EN/en-coreknowledge.tar.gz,2400,3"
	"EN,en-fantasticphonics-adult.tar.gz,/home/labdoo/Public/wikis/EN/en-fantasticphonics-adult,EN/en-fantasticphonics-adult.tar.gz,7400,16"
	"EN,en-fantasticphonics-child.tar.gz,/home/labdoo/Public/wikis/EN/en-fantasticphonics-child,EN/en-fantasticphonics-child.tar.gz,3200,17"
	"EN,en-GCF2015.tar.gz,/home/labdoo/Public/wikis/EN/en-GCF2015,EN/en-GCF2015.tar.gz,12000,18"
    "EN,en-iicba.tar.gz,/home/labdoo/Public/wikis/EN/en-iicba,EN/en-iicba.tar.gz,29,4"
	"EN,en_language_training.tar.gz,/home/labdoo/Public/wikis/EN/english_language_training,EN/en_language_training.tar.gz,868,110"
	"EN,en-mustardseedbooks.tar.gz,/home/labdoo/Public/wikis/EN/en-mustardseedbooks,EN/en-mustardseedbooks.tar.gz,39,11"
	"EN,en-openstax.tar.gz,/home/labdoo/Public/wikis/EN/en-openstax,EN/en-openstax.tar.gz,2900,112"	
	"EN,en-rachelcourses.tar.gz,/home/labdoo/Public/wikis/EN/en-rachelcourses,EN/en-rachelcourses.tar.gz,24000,2"
    "EN,en-rachel.tar.gz,/home/labdoo/Public/wikis/EN/RACHEL-EN,EN/en-rachel.tar.gz,28000,1"
	"EN,en-saylor.tar.gz,/home/labdoo/Public/wikis/EN/en-saylor,EN/en-saylor.tar.gz,4300,14"
	"EN,en-tanzanian_exams.tar.gz,/home/labdoo/Public/wikis/EN/en-tanzanian_exams,EN/en-tanzanian_exams.tar.gz,43,11"
	"EN,en-wassce.tar.gz,/home/labdoo/Public/wikis/EN/en-wassce,EN/en-wassce.tar.gz,20,16"
	#Wikipedia for schools is included in RACHEL     "EN,en-wikipedia_for_schools.tar.gz,/home/labdoo/Public/wikis/EN/wikipedia_for_schools-en,EN/en-wikipedia_for_schools.tar.gz,6400,17"
	"EN,en-women-in-african-history.tar.gz,/home/labdoo/Public/wikis/EN/en-women-in-african-history,EN/en-women-in-african-history.tar.gz,1,8"
    # Beware wikis, $3 is what i use to chek if they are there, or where do i uncimpress them??
	"EN,en.xowa-wikis-without-wikipedia.tar.gz,/home/labdoo/Public/xowa/wiki/en.wikinews.org,xowa-wikis/4.x/EN/en.xowa-wikis-without-wikipedia.tar.gz,9000,20"
	"EN,en.xowa-wikipedia.tar.gz,/home/labdoo/Public/xowa/wiki/en.wikipedia.org,xowa-wikis/4.x/EN/en.xowa-wikipedia.tar.gz,42000,111"

)


HD_AVAILABLE_CONTENT_ES=(
	"ES,es-biblioteca.tar.gz,/home/labdoo/Public/wikis/ES/es-biblioteca,ES/es-biblioteca.tar.gz,480,5"
	"ES,es-boks-videos.tar.gz,/home/labdoo/Public/wikis/ES/es-boks-videos,ES/es-boks-videos.tar.gz,1200,4"
	"ES,es-cnbguatemala.tar.gz,/home/labdoo/Public/wikis/ES/es-cnbguatemala,ES/es-cnbguatemala.tar.gz,480,5"
	"ES,es-educalab.tar.gz,/home/labdoo/Public/wikis/ES/es-educalab,ES/es-educalab.tar.gz,1200,4"
	"ES,es-mustardseedbooks.tar.gz,/home/labdoo/Public/wikis/ES/es-mustardseedbooks,ES/es-mustardseedbooks.tar.gz,480,5"
	"ES,es-rachel.tar.gz,/home/labdoo/Public/wikis/ES/es-rachel,ES/es-rachel.tar.gz,1200,4"
#Wikipedia for schools is included in RACHEL     "ES,es-wikipedia_for_schools.tar.gz,/home/labdoo/Public/wikis/ES/es-wikipedia_for_schools,ES/es-wikipedia_for_schools.tar.gz,6400,17"
# Bewarw wikis, $3 is what i use to chek if they are there, or where do i uncimpress them??
	"ES,es.xowa-wikis.tar.gz,/home/labdoo/Public/xowa/wiki,xowa-wikis/4.x/ES,7500,1000"
)



EXEC_DIR=$PWD
DEST_DIR="/home/labdoo/Public"
MB_TO_BE_LEFT=15000




#DEFINE FUNCTIONS I WILL USE
install_content_selected_language(){
    #######
    #PART1: Select Only the things that are still not installed, 
    ########

    #Iterate on the instalable content and store in a new variable everything that is still not installed
    for line in "${HD_AVAILABLE_CONTENT[@]}"
    do
	    installed_directory=$(echo $line | awk -F',' '{print $3}')
    
	    if test -d "$installed_directory" 
        then
            echo -e "CONTENT ALREADY INSTALLED: $installed_directory" 

	    else
		    HD_AVAILABLE_NON_INSTALLED_CONTENT+=("$line")	
	    fi
    done


    #Show it
    echo  -en "ADDITIONALLY INSTALLABLE CONTENT IN HD: LANGUAGE,FILENAME,INSTALLATION_PATH,STORAGE_PATH (relative to <usb>/labdoo),SIZE,PRIORITY"		
    for line in "${HD_AVAILABLE_NON_INSTALLED_CONTENT[@]}"
    do
	    echo -e "$line"
    done


    #Show Free MBs
    cd $DEST_DIR
    echo "MBs FREE WHEN STARTING"
    myFreeHD=$(df  . | tail -1 | awk '{print $4}')
    myFreeHD=$((myFreeHD/1024))  # in MBs
    echo -e "\e[5m$myFreeHD  \e[0m"
    REMAINING_INSTALLABLE_MB=$((myFreeHD-MB_TO_BE_LEFT))


    #######
    #PART2: Install additional contents 
    ########

    #Iterate over the ordered elements on the list, check if I have still available size (available - margin configured as MB_TO_BE_LEFT) if it fits install
    for i in {1..30}
        do
            for line in "${HD_AVAILABLE_NON_INSTALLED_CONTENT[@]}"
                do
                    #IS THE CURRENT ORDER?	            
                    line_installation_order=$(echo $line | awk -F',' '{print $6}') 
                    if [ "$i" == "$line_installation_order" ]; then
                         
                        #DOES IT FIT
                        line_installation_size=$(echo $line | awk -F',' '{print $5}')                    
                        if [ "$REMAINING_INSTALLABLE_MB" -gt "$line_installation_size" ]; then
                            installation_filename=$(echo $line | awk -F',' '{print $4}') 
                            installation_dest_dir=$(echo $line | awk -F',' '{print $3}')
                            echo "I will install: $EXEC_DIR/$installation_filename"
                            mkdir $installation_dest_dir
                            tar -xf "$EXEC_DIR/$installation_filename" -C $installation_dest_dir/.. >/dev/null
                            

                            myFreeHD=$(df  . | tail -1 | awk '{print $4}')
                            myFreeHD=$((myFreeHD/1024))  # in MBs
                            REMAINING_INSTALLABLE_MB=$((myFreeHD-MB_TO_BE_LEFT))
                            echo "NEW REMAINING_INSTALLABLE_MB: $REMAINING_INSTALLABLE_MB after installing $EXEC_DIR/$installation_filename"
                        else
                            echo -e "I NOT INSTALL because NO fits $line" 
                            HD_NON_INSTALLED_IN_THE_END+=("$line")   #Add it in the list of things that will not be installed, to remove it form the index file
                        
                        fi
	                fi
                done
        done

    cp $EXEC_DIR/EN/index-en.html $DEST_DIR/wikis/EN/index-en.html
#Iterate on that content and store in a new variable everything that is still not installed
    for line in "${HD_NON_INSTALLED_IN_THE_END[@]}"
    do
	    file_name=$(echo $line | awk -F',' '{print $2}' | awk -F'.' '{print $1}')
    
        echo -e "REMOVING: $file_name from INDEX html file" 
        grep -v $file_name  $DEST_DIR/wikis/EN/index-en.html > $DEST_DIR/wikis/EN/index-en_temp.html
        mv $DEST_DIR/wikis/EN/index-en_temp.html $DEST_DIR/wikis/EN/index-en.html
    done


}






########################################
########################################
# START ################################
########################################
########################################






echo -e "\e[31mWELCOME TO THE ADDITIONAL LABDOO CONTENT INSTALLER  \e[0m" 

HD_AVAILABLE_NON_INSTALLED_AVAILABLE_CONTENT=()
HD_NON_INSTALLED_IN_THE_END=()







install_content_selected_language

#######
#PART4: Execute the update permissions script 
########

echo -en "\nNOW WE EXECUTE /home/labdoo/Desktop/set-rights-folder-files-Public-correct.sh to correct permissions"
bash /home/labdoo/Desktop/set-rights-folder-files-Public-correct.sh >/dev/null 2>&1

echo -en "\nCopying finished successfully\n "


echo -en "\nLook how many Gbs you have free NOW...."
myFreeHD=$(df -h . | tail -1 | awk '{print $4}')
echo -en "\e[5m$myFreeHD  \e[0m"
echo -en "\e[31m\n\nEnjoy your day I go to sleep\e[0m"

echo 'labdoo' | sudo -S pm-suspend
