#!/bin/sh # # Script for downloading snapshot files # Last modified: 23-03-2008 # common settings snaproot='/data/software/OpenBSD' cdrom='/dev/rcd0c:0,0,0' # mirror settings ftp_mirrors[0]='ftp://ftp.openbsd.org/pub' ftp_mirrors[1]='ftp://ftp.ca.openbsd.org/pub' ftp_mirrors[2]='ftp://ftp3.usa.openbsd.org/pub' ftp_mirrors[3]='ftp://ftp.stacken.kth.se/pub' ftp_mirrors[4]='ftp://openbsd.informatik.uni-erlangen.de/pub' ftp_mirrors[5]='ftp://ftp.nluug.nl/pub' # directory settings, defaults for OpenBSD packages_dir='OpenBSD/snapshots/packages/i386' snapshot_dir='OpenBSD/snapshots/i386' # time correction for the mirrors in seconds ftp_offset[0]=0 ftp_offset[1]=0 ftp_offset[2]=0 ftp_offset[3]=21600 ftp_offset[4]=25200 ftp_offset[5]=21600 # file settings file_names[0]='INSTALL.i386' file_names[1]='bsd' file_names[2]='bsd.mp' file_names[3]='bsd.rd' file_names[4]='base43.tgz' file_names[5]='comp43.tgz' file_names[6]='xbase43.tgz' file_names[7]='xfont43.tgz' file_names[8]='xserv43.tgz' file_names[9]='xshare43.tgz' # iso settings iso_names[0]='install43.iso' function parm_set { # Set the checkfiles array if [ "$snapshot" = "1" ]; then # copy file array into checkfiles array element_count=${#file_names[@]} index=0 while [ "$index" -lt "$element_count" ] do checkfiles[$index]=${file_names[$index]} index=$((index+1)) done else # copy iso array into checkfiles array element_count=${#iso_names[@]} index=0 while [ "$index" -lt "$element_count" ] do checkfiles[$index]=${iso_names[$index]} index=$((index+1)) done fi # first check max length of the file names, set first tab # this will be used for aligning control output behind file names tab1=0 index=0 element_count=${#file_names[@]} while [ "$index" -lt "$element_count" ] do tmp=${#file_names[$index]} if [[ $tab1 -lt $tmp ]]; then tab1=$tmp fi index=$((index+1)) done tab1=$((tab1+15)) # first check max length of ftp site names, set second tab # this will be used for aligning control output behind ftp and file names tab2=0 index=0 element_count=${#ftp_mirrors[@]} while [ "$index" -lt "$element_count" ] do tmp=${#ftp_mirrors[$index]}+${#snapshot_dir} if [[ $tab2 -lt $tmp ]]; then tab2=$tmp fi index=$((index+1)) done tab2=$((tab2+10)) # check max offset time # this will be used to compare mirrors with local file times max_offset=0 index=0 element_count=${#ftp_offset[@]} while [ "$index" -lt "$element_count" ] do tmp=${ftp_offset[$index]} if [[ $max_offset -lt $tmp ]]; then max_offset=$tmp fi index=$((index+1)) done } function check_aut { echo "\033[49m\033[1;34m - Checking mirrors... \033[0m" # now check mirrors index=0 mirrortime=0 element_count=${#ftp_mirrors[@]} while [ "$index" -lt "$element_count" ] do output=" ($index) ${ftp_mirrors[$index]}/$snapshot_dir/" counter=${#output} while [ "$counter" -lt "$tab2" ] do output="$output " counter=$((counter+1)) done echo -n "$output" wget --timestamping -q -T 5 -t 5 ${ftp_mirrors[$index]}/$snapshot_dir/MD5 --directory-prefix=$snaproot > /dev/null 2>&1 if [[ "$?" == "0" && -f MD5 ]]; then let "tmptime = $(stat -f "%m" -t "%s" MD5) - ${ftp_offset[$index]}" if [[ $index -eq 0 || $mirrortime -le $tmptime ]]; then mirror=$index mirrortime=$tmptime fi echo -e " Files are from $(perl -e "print scalar(localtime($tmptime))")" else echo -e "\033[49m\033[0;31m Failed \033[0m" fi rm -f MD5 index=$((index+1)) done index=$((index-1)) # here we show dates of files and select a mirror till the user confirms by pressing enter tmpmir="" while [ "$tmpmir" != "$mirror" ] do echo "" # show dates of files from nearest up to date mirror echo -e "\033[49m\033[1;34m - Filelist of selected mirror ($mirror) ${ftp_mirrors[$mirror]}/$snapshot_dir/: \033[0m" tmp=$(wget -O - -q ${ftp_mirrors[$mirror]}/$snapshot_dir/ | grep "File" | sed "s/File.*\">/File/" | sed "s/<\/a>.*//" | sed "s/File/ /" | sed "s/^[ \t]*//" | sed "s/ [ ]*/_/g") element_count=${#checkfiles[@]} index=0 while [ "$index" -lt "$element_count" ] do filename=${checkfiles[$index]} for i in $tmp do i=$(echo $i | sed "s/_/ /g" | grep " $filename$" | sed "s/ $filename//") if [ "$i" != "" ]; then output=" ($index) $filename" counter=${#output} while [ "$counter" -lt "$tab1" ] do output="$output " counter=$((counter+1)) done output="$output Mirror file time $i" counter=${#output} while [ "$counter" -lt "$tab2" ] do output="$output " counter=$((counter+1)) done # info about local files if [[ -f $snaproot/${checkfiles[$index]} ]]; then let "locdigtime = $(perl -e "print scalar($(stat -f "%m" -t "%s" $snaproot/${checkfiles[$index]}))")" locfiletime=$(perl -e "print scalar(localtime($locdigtime))") output="$output Local file time $locfiletime" locdigtime=$((locdigtime+$max_offset)) if [[ $locdigtime -le $tmptime && "$index" == "0" ]]; then letsupdate=1 fi else output="$output Local file does not exist" locdigtime=0 letsupdate=1 fi echo "$output" fi done index=$((index+1)) done echo "" if [[ "$letsupdate" == "1" ]]; then echo "\033[49m\033[1;31m NOTE: Local files seems to be outdated or do not exist. Updating is recommended... \033[0m" letsupdate=0 else echo "\033[49m\033[1;31m NOTE: Local files are up to date. Downloading those files seems to be unneccessary... \033[0m" fi echo "" tmpmir=$mirror # choose mirror echo -n " Enter the mirror you want to use [x for Exit, to confirm]: ($mirror)\b\b" read inp case "$inp" in $mirror) ;; [0-${#ftp_mirrors[@]}]) mirror=$inp ;; x*|X*) echo "" echo -e "\033[49m\033[0;31m Aborted by user \033[0m" cd $org exit 67 ;; *) if [[ "$inp" != "" ]]; then echo "" echo -e "\033[49m\033[0;31m Wrong input, exiting script \033[0m" cd $org exit 67 fi ;; esac done } function files_get { echo "" echo "\033[49m\033[1;34m - Downloading files from ${ftp_mirrors[$mirror]}/$snapshot_dir/... \033[0m" element_count=${#checkfiles[@]} index=0 while [ "$index" -lt "$element_count" ] do if [ "$force" == "1" ]; then rm -rf $snaproot/${checkfiles[$index]} > /dev/null 2>&1 fi output=" ($index) ${checkfiles[$index]}" counter=${#output} while [ "$counter" -lt "$tab2" ] do output="$output " counter=$((counter+1)) done echo -n "$output" wget --timestamping -q -t 45 ${ftp_mirrors[$mirror]}/$snapshot_dir/${checkfiles[$index]} --directory-prefix=$snaproot > /dev/null 2>&1 if [ "$?" == "0" ]; then echo -e "\033[49m\033[0;32m Succesfull \033[0m" else echo -e "\033[49m\033[0;31m Failed \033[0m" fi index=$((index+1)) done echo "" echo " Download(s) finished..." } function iso_burn { finish_burn=0 while [ "$finish_burn" != "1" ] do echo "" echo "\033[49m\033[1;34m - List of available iso files to burn on cd: \033[0m" element_count=${#checkfiles[@]} index=0 foundfiles=0 while [ "$index" -lt "$element_count" ] do output=" ($index) ${checkfiles[$index]}" counter=${#output} while [ "$counter" -lt "$tab2" ] do output="$output " counter=$((counter+1)) done # info about local files if [[ -f $snaproot/${checkfiles[$index]} ]]; then foundfiles=$((foundfiles+1)) if [ "$iso" = "" ]; then iso=$index fi let "locdigtime = $(perl -e "print scalar($(stat -f "%m" -t "%s" $snaproot/${checkfiles[$index]}))")" locfiletime=$(perl -e "print scalar(localtime($locdigtime))") output="$output Local file time $locfiletime" else output="$output Local file does not exist" fi echo "$output" index=$((index+1)) done if [ "$foundfiles" != "0" ]; then echo "" echo -n " Enter the iso you want to burn [x] for Exit, to confirm: ($iso)\b\b" read inp case "$inp" in $iso) ;; [0-${#checkfiles[@]}]) iso=$inp ;; x*|X*) echo "" echo -e "\033[49m\033[0;31m Aborted by user \033[0m" cd $org exit 67 ;; *) if [[ "$inp" != "" ]]; then echo "" echo -e "\033[49m\033[0;31m Wrong input, exiting script \033[0m" cd $org exit 67 fi ;; esac echo -n " > Please put a fresh cdrom in your drive and press " read inp output=" > Burning ${checkfiles[$iso]} to cd now, one moment please..." counter=${#output} while [ "$counter" -lt "$tab2" ] do output="$output " counter=$((counter+1)) done echo -n "$output" cdrecord -v dev=$cdrom speed=24 -dao ${checkfiles[$iso]} > /dev/null 2>&1 if [ "$?" == "0" ]; then echo -e "\033[49m\033[0;32m Succesfull \033[0m" else echo -e "\033[49m\033[0;31m Failed \033[0m" fi index=$((index+1)) else finish_burn=1 fi echo "" do_again=N finish_burn=1 echo -n " Do you want to burn another iso? [y/N], to confirm: ($do_again)\b\b" read inp case "$inp" in $do_again) ;; *n*|*N*) ;; *y*|*Y*) finish_burn=0 ;; *) if [[ "$inp" != "" ]]; then echo "" echo -e "\033[49m\033[0;31m Wrong input, exiting script \033[0m" cd $org exit 67 fi ;; esac done echo "" echo " Burning of iso(s) finished..." } function pkg_update { export PKG_PATH=${ftp_mirrors[0]}/$packages_dir/ echo "\033[49m\033[1;34m - Updating packages from $PKG_PATH... \033[0m" pkg_add -ui -F update -F updatedepends echo "" } if [ "$(id -u)" != "0" ]; then echo "This script must be run as root!" exit 67 fi if [ ! -e $snaproot ]; then mkdir -p $snaproot fi clear org=$(pwd) cd $snaproot case "$*" in *f*|*F*) force=1 ;; esac case "$*" in *b*|*B*) snapshot=0 burn=1 parm_set iso_burn ;; *i*|*I*) snapshot=0 burn=0 parm_set check_aut files_get iso_burn ;; *s*|*S*) snapshot=1 parm_set check_aut files_get ;; *u*|*U*) pkg_update ;; *) echo "${0##*/}: unknown option $1" echo "usage: sh ${0##*/} [-s (-f)] [-i (-f)] [-b]" echo "Script for downloading OpenBSD snapshot files" echo "" echo "-s download snapshot files" echo "-i download iso file" echo "-b burn existing iso file to cd" echo "-u update installed packages" echo "" echo "-f can be used to force downloading" cd $org exit 67 ;; esac echo "" cd $org