#!/usr/local/bin/perl
# 
# $Header: cacheSuseRepository.pl 10-aug-2007.04:44:28 rattipal Exp $
#
# cacheSuseRepository.pl
# 
# Copyright (c) 2007, Oracle. All rights reserved.  
#
#    NAME
#      cacheSuseRepository.pl - <one-line expansion of the name>
#
#    DESCRIPTION
#      <short description of component this file declares/defines>
#
#    NOTES
#      <other useful comments, qualifications, etc.>
#
#    MODIFIED   (MM/DD/YY)
#    rattipal    08/09/07 - Creation
# 
use strict;
use LWP::Simple;

my $rpmUrl = shift;
my $stageDir = shift;
my $boot_dir = shift;


print " Printing parameters\n";
print "rpmUrl = $rpmUrl\n";
print "stageDir= $stageDir\n";

my $rpmMirrorTopDir = "OS";
my $suse_initrd = $stageDir."/suseInitrdDir";
createSuseInitrdDir();
my $repository_url = $rpmUrl;
#$rpmMirrorDir =~ s/(ftp|http|https)\:\/\///g;
my $repos_toplevel_path = $stageDir."/".$rpmMirrorTopDir;
system("cd $repos_toplevel_path;wget -r -np -N -q -l 1 $repository_url/yast/");
system("cd $repos_toplevel_path;wget -r -np -N -q -l 1 $repository_url/sles10/");
system("cd $repos_toplevel_path;wget -r -np -N -q -l 1 $repository_url/sles10/yast/");
system("cd $repos_toplevel_path;wget -r -np -N -q -l 1 $repository_url/sles10/media\.1/");
system("cd $repos_toplevel_path;wget -r -np -N -q -l 1 $repository_url/sles10/media\.2/");
                system("cd $repos_toplevel_path;wget -r -np -N -q -l 1 $repository_url/sles10/media\.3/");
                system("cd $repos_toplevel_path;wget -r -np -N -q -l 1 $repository_url/sles10/media\.4/");
                system("cd $repos_toplevel_path;wget -r -np -N -q -l 1 $repository_url/sles10/media\.5/");
                system("cd $repos_toplevel_path;wget -r -np -N -q -l 1 $repository_url/sles10/suse/i586/");
                system("cd $repos_toplevel_path;wget -r -np -N -q -l 1 $repository_url/sles10/suse/i686/");
                system("cd $repos_toplevel_path;wget -r -np -N -q -l 1 $repository_url/sles10/suse/noarch/");
                system("cd $repos_toplevel_path;wget -r -np -N -q -l 1 $repository_url/sles10/suse/setup/");
                system("cd $repos_toplevel_path;wget -r -np -N -q -l 1 $repository_url/sles10/boot/");
                system("cd $repos_toplevel_path;wget -r -np -N -q -l 1 $repository_url/sles10/boot/loader/");
                my @url_path_arr = split /\/+/ ,$repository_url;
                my $SuseRepositoryName;
                my $url_path_arr_len = $#url_path_arr;
                for(my $i=1;$i <= $url_path_arr_len;$i++){
                        $SuseRepositoryName = $SuseRepositoryName."$url_path_arr[$i]/";
                }
                $repos_toplevel_path = $repos_toplevel_path."/".$SuseRepositoryName;
                system("mkdir -p $repos_toplevel_path/sles10/suse/setup/descr");
                getstore($repository_url."/suse/setup/descr/packages",$repos_toplevel_path."/sles10/suse/setup/descr/packages");
                getstore($repository_url."/suse/setup/descr/EXTRA_PROV",$repos_toplevel_path."/sles10/suse/setup/descr/EXTRA_PROV");
                getstore($repository_url."/suse/setup/descr/MD5SUMS",$repos_toplevel_path."/sles10/suse/setup/descr/MD5SUMS");
                getstore($repository_url."/directory\.yast",$repos_toplevel_path."/directory\.yast");
                my $directory_yast = $repos_toplevel_path."/sles10/suse/setup/descr/directory\.yast";
                open DIRECTORY_YAST, "> $directory_yast" or die "cann't open directory\.yast in descr $!";
                print DIRECTORY_YAST "default.sel\n";
                print DIRECTORY_YAST "packages\n";
                print DIRECTORY_YAST "EXTRA_PROV\n";
                print DIRECTORY_YAST "selections\n";
                close DIRECTORY_YAST;

                my $selections = $repos_toplevel_path."/sles10/suse/setup/descr/selections";
                open SELECTIONS, "> $selections" or die "cann't open selections in descr $!";
print SELECTIONS "default.sel";
                close SELECTIONS;
                system("cd $repos_toplevel_path; ln -s sles10/boot boot");
                system("cd $repos_toplevel_path; ln -s sles10/content content");
                system("cd $repos_toplevel_path; ln -s sles10/suse suse");
                system("cd $repos_toplevel_path; ln -s sles10/media\.1 media\.1");
                system("cd $repos_toplevel_path; ln -s sles10/control\.xml control\.xml");
system("chmod -R 777 $repos_toplevel_path");
creatingsusedirectory();


sub createSuseInitrdDir(){
	my $m_exitStatus;
	system("mkdir -p $suse_initrd");
    $m_exitStatus = $? >> 8;
    if ($m_exitStatus != 0)
    {
        print "\nCould not create the suseInitrdDir directory under the $stageDir \n";
        exit $m_exitStatus;
    }
    system("chmod 777 $suse_initrd");
    $m_exitStatus = $? >> 8;
    if ($m_exitStatus != 0)
    {
        print "\nCould not change the permission of the $suse_initrd \n";
        exit $m_exitStatus;
    }

}
sub creatingsusedirectory(){
    my $m_bootServerPath=$boot_dir;
    my $m_exitStatus;
    my $m_initrdFilePath =$m_bootServerPath."/suse/";
    system("mkdir -p $m_initrdFilePath");
    $m_exitStatus = $? >> 8;
    if ($m_exitStatus != 0)
    {
        print "\nCould not create the suse directory under the $m_bootServerPath \n";
        exit $m_exitStatus;
    }
    system("chmod 777 $m_initrdFilePath");
    $m_exitStatus = $? >> 8;
    if ($m_exitStatus != 0)
    {
        print "\nCould not change the permission of the $m_initrdFilePath \n";
        exit $m_exitStatus;
    }
    system("cp $repos_toplevel_path/boot/loader/linux $m_bootServerPath/suse/");
    system("chmod 777 $m_bootServerPath/suse/linux");
}

