#!/usr/bin/perl -w
# This Script (linuxmuster-client-veracrypt-installer) 
# was created by Rüdiger Beck
# It is released under the GPL Version 3
# For Bugs send mail to (jeffbeck-at-web.de)

use strict;
use Getopt::Long;
use File::Basename;
use English;
use File::Path qw(remove_tree);

my $home=$ENV{'HOME'};
#my $UID_name=getpwuid($UID);
#my $EUID_name=getpwuid($EUID);

my $help=0;
my $info=0;
my $download_dir="/tmp";
my $uninstaller="/usr/bin/veracrypt-uninstall.sh";
my $keep_files=0;


# Parsen der Optionen
my $testopt=GetOptions(
           "help|h" => \$help,
           "keep-files" => \$keep_files,
           "info|i" => \$info,
          );

# Prüfen, ob Optionen erkannt wurden, sonst Abbruch
&check_options($testopt);



if ($help==1){
   print('

This script installs VeraCrypt

Options
  --keep-files
   do not delete downloaded files in /tmp
  -h  / --help  
   show this help

');
   print "\n";
   exit;
}


print "########## START: Downloading VeraCrypt\n";


# are there veracrypt mounts? --> exit with message ????

# Detecting architecture
my $arch="";
my $uname=`uname -m`;;
chomp($uname);
if ($uname eq "x86_64"){
    $arch="x64";
} elsif ($uname eq "i686"){
    $arch="x86";
} else {
    print "\nCould not detect architecture of the kernel from $uname.\n\n";
    exit;
}



# version number must be changed
my $version="1.23";

my $dirname="VeraCrypt%20".$version;
my $filename="veracrypt-".$version."-setup.tar.bz2";
my $key="VeraCrypt_PGP_public_key.asc";

my $download_file="https://sourceforge.net/projects/veracrypt/files/".
                  $dirname."/".$filename;
my $signature_file="https://sourceforge.net/projects/veracrypt/files/".
                  $dirname."/".$filename.".sig";
my $idrix_key_pub="https://www.idrix.fr/VeraCrypt/".$key;





my $command="cd $download_dir; wget -t 10 -c --timestamping $download_file";
print "\n$command\n";
system("$command");

$command="cd $download_dir; wget -t 10 -c --timestamping $signature_file";
print "\n$command\n";
system("$command");

$command="cd $download_dir; wget -t 10 -c --timestamping $idrix_key_pub";
print "\n$command\n";
system("$command");


# continue only if download was successful
my $install=$download_dir."/veracrypt-".$version."-setup.tar.bz2";

if (-f $install){
    print "\nSuccesfully downloaded $install\n\n";
} else {
    print "\n";
    print "ERROR: No download-file $install\n";
    print "       Exiting before uninstalling old Version\n\n";
    exit;
}


# uninstall old installation, if existing
if (-x $uninstaller){
    print "Detected installed version of VeraCrypt.\n";
    my $command="$uninstaller";
    print "$command\n";
    system("$command");
} else {
    print "\n$uninstaller not found\n";
    print "I guess VeraCrypt is already uninstalled on this system!\n\n";
}



# unpacking
print "Unpacking the following files:\n";
system("cd $download_dir; tar xjvf $filename");


# installing
my $install2=$download_dir."/veracrypt-".$version."-setup-gui-".$arch;
system($install2);


if ($keep_files==0){
    # removing temporary downloaded files
    my $remove="rm -f ".$download_dir."/veracrypt-*setup-*";
    system($remove);

    $remove="rm -f ".$download_dir."/".$filename;
    system($remove);

    $remove="rm -f ".$download_dir."/".$filename.".sig";
    system($remove);

    $remove="rm -f ".$download_dir."/".$key;
   system($remove);
}


print "########## DONE: downloading and installing VeraCrypt\n";


############################################################
# subs
############################################################
sub  check_options{
   my ($parse_ergebnis) = @_;
   if (not $parse_ergebnis==1){
      my @list = split(/\//,$0);
      my $scriptname = pop @list;
      print "\nYou have made a mistake, when specifying options.\n"; 
      print "See error message above. \n\n";
      print "... $scriptname is terminating.\n\n";
      exit;
   } else {
      #print "All options  were recognized.\n";
   }

}




print "";











