#!/usr/bin/perl -w
# This Script (linuxmuster-client-truecrypt-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=0;
my $download_dir="/tmp";
my $uninstaller="/usr/bin/truecrypt-uninstall.sh";



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

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



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

This script installs truecrypt

Options
  -h  / --help  
   show this help
  --download 
   download truecrypt only to /tmp
');
   print "\n";
   exit;
}


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


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


my $uname=`uname -m`;;
chomp($uname);
my $download_file="";
my $filename="";
my $version="7.1a";
my $arch="";
my $sha1="nonsense";

# download truecrypt to tmp 
if ($uname eq "x86_64"){
    $arch="x64";
    $sha1="086cf24fad36c2c99a6ac32774833c74091acc4d";
} elsif ($uname eq "i686"){
    $arch="x86";
    $sha1="0e77b220dbbc6f14101f3f913966f2c818b0f588";
} else {
    print "\nCould not detect architecture of the kernel from $uname.\n\n";
    exit;
}

$filename="truecrypt-".$version."-linux-".$arch.".tar.gz";
# old truecrypt site, offline June 2014
#$download_file="http://www.truecrypt.org/download/".$filename;
# New site truecrypt.ch
$download_file="http://download.truecrypt.ch/current/".$filename;

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

my $sha1_result=`sha1sum $download_dir/$filename`;
#print "sha1: >$sha1_result<\n";
my ($sha1_download,$file)=split(/\s/,$sha1_result);
#print "sha1: >$sha1_download<\n";

if ($sha1 eq $sha1_download){
    print "SHA1 verification valid\n";
} else {
    print "\nERROR: The downloaded file has not the expected SHA1 sum\n\n";
    exit;
}



if ($download==1){
    print "download finished\n";
    exit;
}

# continue only if download was successful ????

# uninstall old installation, if existing
if (-f $uninstaller){
    print "Detected installed version of truecrypt.\n";
    print "Uninstalling it ....\n";
    my $command="$uninstaller";
    print "$command ... \n";
    system("$command");
} else {
    print "$uninstaller not found.\n";
    print "  * I guess Truecrypt is not installed on this system!\n";
}


# unpacking
system("cd $download_dir; tar xzvf $filename");

# installing
my $install="/tmp/truecrypt-".$version."-setup-".$arch;
system($install);




print "########## DONE: downloading and installing truecrypt\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 "";











