#!/bin/bash

VERS=$2
GPG=$3

URL=https://linux-libre.fsfla.org/pub/linux-libre/releases/

 if [[ $1 == "--kernel-version" || $1 == "-k" ]]; then

   if [[ ! -z "$VERS" ]]; then

     CVERS=$(echo $VERS | cut -c -3)

     echo "Check for deblob-$CVERS on linux-libre..."
 
     wget --method=HEAD "$URL$VERS-gnu/deblob-$CVERS" 2>/tmp/get-delob.log

     if [[ $(cat /tmp/get-delob.log | sed -n 6p | head -c 1) == "L" ]]; then
   
     echo "deblob-$CVERS found on linux-libre servers"

     echo "Download deblob-$CVERS..."
     wget "$URL$VERS-gnu/deblob-$CVERS" 2>/dev/null

       if [[ ! -z $3 ]]; then

         if [[ $3 == "--gpg-check" || $3 == "-g" ]]; then

           echo "Check signed deblob-$CVERS"

           wget "$URL$VERS-gnu/deblob-$CVERS.sign" 2>/dev/null
           gpg --verify deblob-$CVERS.sign deblob-$CVERS 2>/tmp/deblob-gpg.log

           CGPG=$(cat /tmp/deblob-gpg.log | sed -n 3p | sed 's/.*: \([A-Za-z]*\) .*/\1/')

           if [[ $CGPG != "Good" ]]; then
	
	     echo "Validation of signed deblob-$CVERS failed"
	     exit
        
           else
	   
	     echo "Gpg check success"

	   fi
  
         unlink /tmp/deblob-gpg.log 	  

         else

         echo "Unknown option $3, use --help or -h to show available options"
	 exit

         fi

       fi

     echo "Apply chmod to deblob-$CVERS"
     chmod +x "deblob-$CVERS"
 
     echo "Download deblob-check..."
     wget "$URL$VERS-gnu/deblob-check" 2>/dev/null

       if [[ ! -z $3 ]]; then

         if [[ $3 == "--gpg-check" || $3 == "-g" ]]; then

           echo "Check signed deblob-check"

           wget "$URL$VERS-gnu/deblob-check.sign" 2>/dev/null
           gpg --verify deblob-check.sign deblob-check 2>/tmp/deblob-gpg.log

           CGPG=$(cat /tmp/deblob-gpg.log | sed -n 3p | sed 's/.*: \([A-Za-z]*\) .*/\1/')

           if [[ $CGPG != "Good" ]]; then
	
	     echo "Validation of signed deblob-check failed"
	     exit
        
           else
	   
	     echo "Gpg check success"

	   fi
  
         unlink /tmp/deblob-gpg.log 	  

         else

	   echo "Unknown option $3, use --help or -h to show available options"
	   exit

         fi

       fi

    echo "Apply chmod to deblob-check"
    chmod +x "deblob-check"

    echo "Run ./deblob-$CVERS to clean the kernel source"
  
    else

      echo "File not found, given kernel version is probably incorrect"

    fi

   unlink /tmp/get-delob.log || true

   else

     echo "Kernel version missing, use --help or -h to show available options"

   fi

 elif [[ $1 != "--kernel-version" && $1 != "-k" && $1 != "--help" && $1 != "-h" ]]; then

   echo "Unknown option $1, use --help or -h to show available options"

 else

   echo "Usage : get-deblob --kernel-version {kernel version} [--optional] "
   echo
   echo "  Simple scripted process of https://wiki.gentoo.org/wiki/Kernel_Deblobing"
   echo
   echo "Options :"
   echo 
   echo "  -k, --kernel-version   Targeted kernel version"
   echo "  -g, --gpg-check   Check scripts with signed gpg file"
   echo "                  Make sure you have imported the GPG key of linux-libre first"
   echo "  -h, --help   Show this help"
   echo 
   echo "Example :"
   echo
   echo "  get-deblob -k 5.5.10"
   echo "  get deblob --kernel-version 5.5.rc7 -g"
   echo "  get deblob -k 4.9.24 --gpg-check"
   echo
   echo "More information :"
   echo 
   echo "  See http://devz3ro.tx0.org/sysadmin/get-deblob.php"
   echo

 fi

 
