reviewed usage for completeness
[build.git] / vbuild-fedora-mirror.sh
1 #!/bin/bash
2 # this can help you create/update your fedora mirror
3 # $Id$
4
5 COMMAND=$(basename $0)
6
7 dry_run=
8 verbose=
9 skip_core=
10 root=/data/fedora/linux
11
12 us_rsyncurl=rsync://mirrors.kernel.org/fedora
13 eu_rsyncurl=rsync://ftp-stud.hs-esslingen.de/fedora/linux
14 # change this
15 jp_rsyncurl=rsync://ftp-stud.hs-esslingen.de/fedora/linux
16
17 default_distroname=f8
18 all_distronames="f7 f8"
19 default_arch=i386
20 all_archs="i386 x86_64"
21
22 case $(hostname) in 
23     *.fr|*.de|*.uk)
24         rsyncurl=$eu_rsyncurl ;;
25     *.jp)
26         rsyncurl=$jp_rsyncurl ;;
27     *)
28         rsyncurl=$us_rsyncurl ;;
29 esac
30
31 function usage () {
32     echo "Usage: $COMMAND [-n] [-v] [-c] [-r root] [-u rsyncurl|-e] [-f distroname|-F] [-a arch|-A]"
33     echo "Defaults to -r $root -u $rsyncurl -f $default_distroname -a $default_arch"
34     echo "Options:"
35     echo " -n : dry run"
36     echo " -v : verbose"
37     echo " -c : skips core repository"
38     echo " -r root (default is $root)"
39     echo " -u rsyncurl (default is $rsyncurl)"
40     echo " -e : uses European mirror $eu_rsyncurl"
41     echo " -f distroname - use vserver convention, e.g. fc6 and f7"
42     echo " -F : for distroname in $all_distronames"
43     echo " -a arch - use yum convention"
44     echo " -A : for arch in $all_archs"
45     exit 1
46 }
47
48 function mirror_distro_arch () {
49     distroname=$1; shift
50     arch=$1; shift
51
52     case $distroname in
53         fc*[1-6])
54             distroindex=$(echo $distroname | sed -e "s,fc,,g")
55             distro="Fedora Core"
56             ;;
57         f*[7-8])
58             distroindex=$(echo $distroname | sed -e "s,f,,g")
59             distro="Fedora"
60             ;;
61         centos*[4-5])
62             distroindex=$(echo $distroname | sed -e "s,centos,,g")
63             distro="CentOS"
64             ;;
65         *)
66             echo "Unknown distribution $distroname - skipped"
67             ;;
68     esac
69
70     excludelist="debug/ iso/ ppc/ source/"
71     options="--archive --compress --delete --delete-excluded $dry_run $verbose"
72     [ -n "$(rsync --help | grep no-motd)" ] && options="$options --no-motd"
73     for e in $excludelist; do
74         options="$options --exclude $e"
75     done
76
77     if [ -n "$verbose" ] ; then 
78         echo "root=$root"
79         echo "distro=$distroname"
80         echo "distroname=$distroname"
81         echo "distroindex=$distroindex"
82         echo "arch=$arch"
83         echo rsyncurl="$rsyncurl"
84         echo "rsync options=$options"
85     fi
86
87     RES=1
88     paths=""
89     case $distro in
90         [Ff]edora*)
91             case $distroindex in
92                 2|4|6)
93                     [ -z "$skip_core" ] && paths="core/$distroindex/$arch/os/"
94                     paths="$paths core/updates/$distroindex/$arch/ extras/$distroindex/$arch/"
95                     RES=0
96                     ;;
97                 7|8)
98                     [ -z "$skip_core" ] && paths="releases/$distroindex/Everything/$arch/os/"
99                     paths="$paths updates/$distroindex/$arch/"
100                     RES=0
101                     ;;
102             esac
103             ;;
104     
105         CentOS*)
106             case $distroindex in
107                 5)
108                     [ -z "$skip_core" ] && paths="$distroindex/os/$arch/"
109                     paths="$paths $distroindex/updates/$arch/"
110                     RES=0
111                     ;;
112             esac
113             ;;
114
115     esac
116
117     if [ "$RES" = 1 ] ; then
118         echo "$distro $distroindex currently unsupported - skipped"
119     else
120         for repopath in $paths; do
121             echo "============================== $distro -> $distroindex $repopath"
122             [ -z "$dry_run" ] && mkdir -p ${root}/${repopath}
123             command="rsync $options ${rsyncurl}/${repopath} ${root}/${repopath}"
124             echo $command
125             $command
126         done
127     fi
128
129     return $RES 
130 }
131
132 function main () {
133     distronames=""
134     archs=""
135     while getopts "nvcr:u:ef:Fa:Ah" opt ; do
136         case $opt in
137             n) dry_run=--dry-run ; verbose=--verbose ;;
138             v) verbose=--verbose ;;
139             c) skip_core=true ;;
140             r) root=$OPTARG ;;
141             u) rsyncurl=$OPTARG ;;
142             e) rsyncurl=$eu_rsyncurl ;;
143             f) distronames="$distronames $OPTARG" ;;
144             F) distronames="$distronames $all_distronames" ;;
145             a) archs="$archs $OPTARG" ;;
146             A) archs="$archs $all_archs" ;;
147             h|*) usage ;;
148         esac
149     done
150     [ -z "$distronames" ] && distronames=$default_distroname
151     [ -z "$archs" ] && archs=$default_arch
152
153     RES=0
154     for arch in $archs; do 
155         for distroname in $distronames ; do 
156             mirror_distro_arch "$distroname" "$arch" || RES=1
157         done
158     done
159
160     exit $RES
161 }
162
163 main "$@"