S.L.R.C. - SuSE Local Repository Creator - This Example is setup for Packman

With the help and request of deano_ferrari (which was very much appreciated), I have written a bash script that can be used to download and create a local repository, usable by the openSUSE YaST / Software / Software Repository application. The script has been specifically configured to create the four basic Packman repositories, but it could be modified for use with openSUSE repositories. Please keep in mind just how large some of these repositories could be and just how long it might take you to download everything that might be online. None the less, it might be used to download all of Packman, when you have access for use later while you are offline. To use this bash script, copy and past the following text from the code field below into your favorite text editor:

#!/bin/bash

#: Title       : S.L.R.C. - SuSE Local Repository Creator
#: Date Created: Sat Oct 1 20:25:52 CDT 2011
#: Last Edit   : Sun Oct 2 14:27:00 CDT 2011
#: Author      : James D. McDaniel
#: Version     : 1.00
#: Description : Create a Local Repository
#: Options     : none

TITLE="S.L.R.C. - SuSE Local Repository Creator - Version 1.00"

#
# Written for the openSUSE forums on Sunday October 2, 2011
#

#
# Copy and Paste the text of this script into a text editor and save 
# it as the file slrc in the folder ~/bin (/home/username/bin).
# This script must be marked executable to be used.  Please run 
# the following Terminal command: chmod +x ~/bin/slrc
#

declare -a repository
declare -a repo_size
declare -a base_name
declare -a exit_code
declare -a update_status

#
# Packman Mirror was found at http://packman.links2linux.de/MIRRORS.html
#
mirror=packman.jacobs-university.de

mirror_name=Packman

num_repos=4

#
# Repo Folder Names should NOT end with a Slash!
#
repository[1]=packman/suse/openSUSE_11.4/Essentials
repository[2]=packman/suse/openSUSE_11.4/Extra
repository[3]=packman/suse/openSUSE_11.4/Games
repository[4]=packman/suse/openSUSE_11.4/Multimedia

#
# Base_Folder Download Location ***********************************************
#
base_folder=$HOME"/Downloads"

#
# rsync Dry Run Command & Standard Command
#
rsync_dr="rsync -tvzrulhn --delete-after "
rsync_sc="rsync -tvzrulh --delete-after "

#
# This is the standard GPL Statement, leave at the top of the script.
# Just use the command show_gpl after this function for it to be shown.
#
function show_gpl {
echo ""
echo "slrc is a bash script file written to be used with openSUSE."
echo "Copyright (C) 2011 by James D. McDaniel, jmcdaniel3@austin.rr.com"
echo ""
echo "This program is free software; you can redistribute it and/or modify"
echo "it under the terms of the GNU General Public License as published by"
echo "the Free Software Foundation; either version 2 of the License, or"
echo "(at your option) any later version."
echo ""
echo "This program is distributed in the hope that it will be useful,"
echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
echo "GNU General Public License for more details."
echo ""
echo "You should have received a copy of the GNU General Public License"
echo "along with this program; if not, write to the Free Software"
echo "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA"
echo ""
}

#
# Help on how to use the locally created repository ***************************
#
function use_repo {
clear
echo ""
echo "In order to use the Local Repository: $folder2/"
echo "which you just downloaded, you must do the following tasks:"
echo ""
echo "Start: YaST (Enter root Password) / Software / Software Repositories"
echo "Press: Add (Bottom Left Button) / Select: Local Directory (Bullet)"
echo "Select: Next (Button on Bottom Right) / Enter the Path to Directory"
echo "Select: Browse (Button on Right) / Find Folder and Press: Choose (or"
echo "just enter the full path name) /  Select: Next (Button on Bottom Right)"
echo "Select: OK (Button on Bottom Right) - You Are now Complete!"
echo ""
echo "You should get no errors & Software Repositories will close normally."
echo ""
echo "Should the repository you just created not be online, you must disable it"
echo "so as to not get any update errors when using Software Management next time!"
echo ""
echo -n "Press Any Key to return to the S.L.R.C.  main menu... "
read something
}  

#
# Create a String of spaces ***************************************************
#
function spad {
  spaces=""
  len=$1
  total=1
  while  $total -le $len ] ; do
    spaces=$spaces" "
    let total=total+1
  done
  return 0
}

#
# Pad string with spaces on right *********************************************
#
function rpad {
rword=$1
len=$2
wlen=${#rword}
if  ${#rword} -lt $len ] ; then
  let len2=len-wlen
  space=""
  while  $len2 -gt 0 ] ; do
    space=$space" "
    let len2=len2-1
  done
  rword=$rword$space
fi
return 0
}

#
# Pad string with spaces on left **********************************************
#
function lpad {
lword=$1
len=$2
wlen=${#lword}
if  ${#lword} -lt $len ] ; then
  let len2=len-wlen
  space=""
  while  $len2 -gt 0 ] ; do
    space=" "$space
    let len2=len2-1
  done
  lword=$space$lword
fi
return 0
}

#
# Allow the change of the base_folder name ************************************
#
function folder_ask {
  echo
  echo "Please Enter the new folder location to use.  This folder location must exist!"
  gui=true
  while $gui ; do
    echo
    echo -n "Type in type new folder location and press enter: "
    read CHOICE
    if  ! -d "$CHOICE" ]] ; then
      echo "You entered $CHOICE and this Folder DOES NOT EXIST!  Press <enter> and Please Try Again!"
      read something
    else
      base_folder=$CHOICE
      folder_sub=false
      gui=false
    fi
  done
  return 0  
}

# ----------------------------------------------------------------------------
# Prepair for install of rsync if needed by more than one distro
# The following coding provided by please_try_again
#

# install cpufrequtils under openSUSE, Fedora, Mandriva, Ubuntu
suselinux="zypper in"
mandrivalinux="urpmi"
fedora="yum install"
ubuntu="apt-get install"

# default to openSUSE if lsb_release is not found (or write more code to install
# lsb_release withouth confirmation or get the release name in another way)
install=suselinux
which lsb_release &>/dev/null && install=$(lsb_release -is | tr ":upper:]" ":lower:]" | tr -d " ")
install=${!install}
#-----------------------------------------------------------------------------

#
# Determine if the package rsync is installed ****************************
#
which rsync > /dev/null
Exit_Code=$?

if  $(( Exit_Code )) -ge 1 ] ; then
  tput clear
  echo "The rsync Utility Package is not installed!"
  echo
  echo -n "Would you like to install the rsync package(y/N)? "
  read CHOICE
  if  $CHOICE == [Yy] ]] ; then
    sudo $install rsync
  else
    echo "The rsync Utility Package was not installed!"
    exit 1
  fi
fi

#
# Determine/Ask about the base folder name ************************************
#
folder_sub=true
while $folder_sub ; do
  clear
  echo $TITLE
  echo
  echo -n "Using Base Folder: $base_folder"
  if  ! -d "$base_folder" ]] ; then
    echo " and this Folder DOES NOT EXIST!"
    folder_ask   
  else
    echo " and this folder does exist."
  fi
  echo
  echo -n "Do you wish to change this default folder location? (y/N): "
  read CHOICE
  if  $CHOICE == [Yy] ]] ; then
    folder_ask
  fi
  folder_sub=false
echo
done

#
# Do you have rights to use the selected folder? ******************************
#
ext=$(date +%y%m%d.%H%M%S)
test=testit.$ext
echo "This is a test of your selected base folder, this is only a test" > $base_folder/$test
if  ! -e "$base_folder/$test" ] ; then
  echo
  echo "You do not have rights to write to the selected folder: $base_folder"
  echo ""
  echo "Please use the startup command: sudo $0"
  echo
  exit 1
else
  rm $base_folder/$test
fi

#
# Main Program Starts Here ****************************************************
#
menu=true

while $menu ; do

  clear
  echo $TITLE
  echo
  echo " Using Base Folder: $base_folder"
  echo
  echo "Total Repositories: $num_repos"
  echo 
  echo -n "Reading the $mirror_name Repository Sizes: "

#
# Get Repository Size and record Exit Code ************************************
#
  start=1
  rlength=0
  slength=0
  while  $start -le $num_repos ] ; do

    echo -n "$start:"
    if  "${repo_size$start]}" == "" ] ; then
      base_name$start]=$(basename ${repository$start]})
      update_status$start]=false
      repo_size$start]=$($rsync_dr $mirror"::"${repository$start]}"/" 2>&1 | tail -1 | cut -f4 -d " ")
      if  "${repo_size$start]}" == "starting" ] ; then
    let exit_code$start]=1
    repo_size$start]=""
      else
    let exit_code$start]=0    
      fi
    fi
    if  ${#repository$start]} -gt $rlength ] ; then
      let rlength=${#repository$start]}
    fi
    if  ${exit_code$start]} -eq 0 ] ; then
      if  ${#repo_size$start]} -gt $slength ] ; then
    let slength=${#repo_size$start]}
      fi
    fi
    let start=start+1

  done

  echo "Done"
  echo

#
# Display Repository Names and Size or Exit Code  *****************************
#
  tlen=$rlength
  let tlen=tlen-15
  spad "$tlen"
  echo "   Repository Name $spaces   Size  Updated"
  echo
  start=1
  while  $start -le $num_repos ] ; do
    rpad "${repository$start]}" "$rlength"
    if  ${exit_code$start]} -eq 0 ] ; then
      lpad "${repo_size$start]}" "$slength"
      echo "$start) $rword  $lword  ${update_status$start]}"
    else
      echo "$start) $rword  Invalid"
    fi
    let start=start+1
  done

#
# Request User Input Selection ************************************************
#
  echo
  echo -n "Please enter the repository number to use [1-$num_repos] (q=Quit): "
  read CHOICE
  if  $CHOICE =~ ^[0-9]+$ ]] ; then
    if  $CHOICE -le $num_repos ]] &&  $CHOICE -gt 0 ]]; then
      repo=$CHOICE
      if  ${exit_code$repo]} -eq 0 ] ; then
    echo
    echo -n "Use the $mirror_name Repository: ${repository$repo]} Size: ${repo_size$repo]} (y/N): "
    read CHOICE
    if  $CHOICE == [Yy] ]] ; then

#
# Determine if target repository folders exit *********************************
#
      folder1="$base_folder/$mirror_name/"
      folder2="$base_folder/$mirror_name/${base_name$repo]}"
      if  ! -d "$folder1" ]] ; then
        echo
        echo -n "The Folder $folder1 does not Exist! Create This Folder? (y/N): "
        read CHOICE
        if  $CHOICE == [Yy] ]] ; then
          mkdir $folder1
        else
          echo
          echo "Folder $folder1 Creation Aborted by User!"
          show_gpl
          exit 1
        fi
      fi
      if  ! -d "$folder2" ]] ; then
        echo
        echo -n "The Folder $folder2 does not Exist! Create This Folder? (y/N): "
        read CHOICE
        if  $CHOICE == [Yy] ]] ; then
          mkdir $folder2
        else
          echo
          echo "Folder $folder2 Creation Aborted by User!"
          show_gpl
          exit 1
        fi
      fi
#
# Download the Requested Repopsitory ******************************************
#
      echo
      echo "Downloading Repository: ${repository$repo]}" 
      echo "to the folder: $folder2/, Please Wait ..."
          echo
      $rsync_sc $mirror"::"${repository$repo]}"/" $folder2"/" 2>&1
      Exit_Code=$?
      if  $(( Exit_Code )) -ge 1 ] ; then
        echo
        echo "An Error Code: $Exit_Code was reported for repo: ${repository$repo]}!"
        echo
        echo -n "The process has failed.  Press ANY Key to Continue."
        read something
      else
        update_status$repo]=true
        echo
        echo -n "Process is now Complete.  Press ANY Key to Continue."
        read something
        use_repo
      fi
    fi
      else
    echo
    echo -n "The Repository: ${repository$repo]} Size: Invlaid, can not be used!"
    read something
      fi
    fi
  else
    if  $CHOICE == [Qq] ]] ; then
      menu=false
    fi
  fi

done

show_gpl
exit 0

# End Of Script

Save this bash script as the text file slrc in the folder ~/bin (/home/username/bin). This script must be marked executable to be used. Please run the following Terminal command:

chmod +x ~/bin/slrc

To use slrc, just open a terminal session and ttpe in the command:

slrc

This bash script uses both local and online folder names. Do not add a trailing slash / as it will always be added in for you if required. If you have any questions about the usage of this bash script, please let me know,

Thank You,