aboutsummaryrefslogblamecommitdiffstats
blob: 00cb8c3ecb887b2b27fa5fae3ac0b32f5cad4f71 (plain) (tree)
1
2
3
4
5
6
7
8
9
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082


                                 

                                                                      
                                
                                                                                       
                                         
                                                                      



                                                                 
                                                                          
                                        
                                                                      

































































































                                                                                                                         
                                                                                                         































































































                                                                                                                                                 
                                               
                                                                                                     




































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                                    
#!/bin/bash

declare -A gen_tag="ol_R5.SP8.01"
#### R5 SP8.01
declare -A wl12xx_download_target="git://git.ti.com/wl12xx/wl12xx.git"
declare -A wl12xx_tag=${gen_tag}
declare -A compatwireless_download_target="git://git.ti.com/wl12xx/compat-wireless.git"
declare -A compat_wireless_tag=${gen_tag}
declare -A compat_download_target="git://git.ti.com/wl12xx/compat.git"
declare -A compat_tag=${gen_tag}
declare -A iw_download_target="git://git.sipsolutions.net/iw.git"
declare -A iw_tag="0a236ef5f8e4ba7218aac7d0cdacf45673d5b35c"

declare -A ti_utils_download_target="git://git.ti.com/wl12xx/ti-utils.git"
declare -A ti_utils_commit_id=${gen_tag}
declare -A hostap_download_target="git://git.ti.com/wl12xx/hostap.git"
declare -A hostap_commit_id=${gen_tag}



if [ ! -e setup-env ]
then
	echo "No setup-env"
	exit 1
fi
source setup-env
unset PKG_CONFIG_SYSROOT_DIR
ME=$0
components="libnl openssl iw hostap wpa_supplicant crda ti-utils ti-utils-firmware compat-wireless"
export GIT_TREE=${WORK_SPACE}/wl12xx
export GIT_COMPAT_TREE=${WORK_SPACE}/compat

old_dir=`pwd`

function download ()
{
	file="$2"
	[ -e ${WORK_SPACE}/${file} ] && echo "File $file already exists. Skipping download." && return 0
	wget "$1"
	if [ $? -ne 0 ]
	then 
		echo "Failed to download $file"
		exit 1
	fi
}

function git_clone ()
{
	file="$2"
	if [ -e ${WORK_SPACE}/${file} ]
	then
		echo "File $file alread exists. Skipping git clone."
		cd ${WORK_SPACE}/${file}
		git fetch || exit 1
		echo "git $file fetched."
		return 0
	fi
	git clone "$1"
	if [ $? -ne 0 ]
	then
		echo "Failed to download $2 git repository"
		exit 1
	fi
	echo "git $file cloned."
}

function compat-wireless()
{
	stage=$1


	if [ x"$stage" = "xdownload"  -o x"$stage" = "xall" ]
	then
                cd ${WORK_SPACE}
                git_clone ${wl12xx_download_target} wl12xx
                cd ${WORK_SPACE}/wl12xx
                git reset --hard ${wl12xx_tag} || exit 1
                cd ${WORK_SPACE}
                git_clone ${compat_download_target} compat
                cd ${WORK_SPACE}/compat
                git reset --hard ${compat_tag} || exit 1
                cd ${WORK_SPACE}
                git_clone ${compatwireless_download_target} compat-wireless
                cd ${WORK_SPACE}/compat-wireless
                git reset --hard ${compat_wireless_tag} || exit 1
	fi
	if [ x"$stage" = "xbuild" -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/compat-wireless
                ./scripts/admin-update.sh
                ./scripts/driver-select wl12xx
                patch -p1 -i ${old_dir}/patches/0001-wl12xx-additional-two-members-for-wl12xx_platform_da.patch || exit 1
		make KLIB_BUILD=${KLIB_BUILD} KLIB=${ROOTFS} || exit 1
	fi
	if [ x"$stage" = "xinstall"  -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/compat-wireless
		make KLIB_BUILD=${KLIB_BUILD} KLIB=${ROOTFS} install-modules
	fi

	if [ x"$stage" = "xclean" ]
	then
		cd $WORK_SPACE/compat-wireless
		#make KLIB=${ROOTFS} uninstall
		cd $WORK_SPACE && rm -rf compat-wireless
	fi

	cd $WORK_SPACE
}
function crda ()
{
	stage=$1
	if [ x"$stage" = "xdownload"  -o x"$stage" = "xall" ]
	then
		download "http://linuxwireless.org/download/crda/crda-1.1.1.tar.bz2" "crda-1.1.1.tar.bz2"
                download "http://linuxwireless.org/download/wireless-regdb/regulatory.bins/2011.04.28-regulatory.bin" "2011.04.28-regulatory.bin"
		tar xjf crda-1.1.1.tar.bz2
		cd ${WORK_SPACE}/crda-1.1.1
		cp ${WORK_SPACE}/2011.04.28-regulatory.bin .
	fi
	if [ x"$stage" = "xbuild" -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/crda-1.1.1
		make USE_OPENSSL=1 all_noverify || exit 1
	fi
	if [ x"$stage" = "xinstall"  -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/crda-1.1.1
		DESTDIR=${ROOTFS} make USE_OPENSSL=1 UDEV_RULE_DIR="etc/udev/rules.d/" install || exit 1
		mkdir -p ${ROOTFS}/usr/lib/crda
		cp 2011.04.28-regulatory.bin ${ROOTFS}/usr/lib/crda/regulatory.bin
	fi
	if [ x"$stage" = "xclean" ]
	then
		cd $WORK_SPACE/crda-1.1.1
		DESTDIR=${ROOTFS} make clean
		cd $WORK_SPACE && rm -rf crda-1.1.1
	fi
	cd $WORK_SPACE
}

function iw ()
{
	stage=$1
	if [ x"$stage" = "xdownload"  -o x"$stage" = "xall" ]
	then
		git_clone "${iw_download_target}" iw
		cd ${WORK_SPACE}/iw
		git reset --hard ${iw_tag} || exit 1 
	fi
	if [ x"$stage" = "xbuild" -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/iw
		make || exit 1
	fi

	if [ x"$stage" = "xinstall"  -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/iw
		DESTDIR=${ROOTFS} make install || exit 1
	fi
	if [ x"$stage" = "xclean" ]
	then
		cd $WORK_SPACE/iw
		make clean
	fi
	cd $WORK_SPACE
}
function libnl ()
{
	stage=$1
	if [ x"$stage" = x"download" -o x"$stage" = "xall" ]
	then
		download http://www.infradead.org/~tgr/libnl/files/libnl-2.0.tar.gz libnl-2.0.tar.gz
		tar xzf libnl-2.0.tar.gz
		cd ${WORK_SPACE}/libnl-2.0
		./configure --prefix=${ROOTFS} CC=${CROSS_COMPILE}gcc LD=${CROSS_COMPILE}ld RANLIB=${CROSS_COMPILE}ranlib --host=arm-linux
		if [ $? != 0 ]
		then
			echo "libnl failed to be configured"
			exit 1
		fi
	fi

	if [ x"$stage" = "xbuild" -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/libnl-2.0
		make || exit 1
	fi
	if [ x"$stage" = "xinstall" -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/libnl-2.0
		make install || exit 1
	fi
	if [ x"$stage" = "xclean" ]
	then
		cd $WORK_SPACE/libnl-2.0
		make uninstall
		cd .. && rm -rf libnl-2.0
	fi
	cd $WORK_SPACE

}
function openssl ()
{
	stage=$1
	if [ x"$stage" = x"download" -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}
		download "http://www.openssl.org/source/openssl-1.0.0d.tar.gz" "openssl-1.0.0d.tar.gz"
		tar xzf openssl-1.0.0d.tar.gz
		cd ${WORK_SPACE}/openssl-1.0.0d
		patch -p1 -i ${old_dir}/patches/0001-openssl-1.0.0d-new-target-os-for-configure.patch
		CROSS_COMPILE= perl ./Configure  shared --prefix=$ROOTFS/usr --openssldir=$ROOTFS/usr/lib/ssl linux-elf-arm
	fi || exit 1
	if [ x"$stage" = "xbuild" -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/openssl-1.0.0d
		make || exit 1
	fi
	if [ x"$stage" = "xinstall" -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/openssl-1.0.0d
		make install_sw || exit 1
	fi
	if [ x"$stage" = "xclean" ]
	then
		cd ${WORK_SPACE}/openssl-1.0.0d
		rm -f ${ROOTFS}/usr/lib/*ssl* ${ROOTFS}/usr/lib/pkgconfig/*ssl*
		cd .. && rm -rf openssl-1.0.0d
	fi
	cd $WORK_SPACE
}
function ti-utils ()
{
	stage=$1

	if [ x"$stage" = x"download" -o x"$stage" = "xall" ]
	then
		git_clone "${ti_utils_download_target}" ti-utils
		cd ${WORK_SPACE}/ti-utils
                git reset --hard "${ti_utils_commit_id}"
	fi

	if [ x"$stage" = "xbuild" -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/ti-utils
		NFSROOT=${ROOTFS} make || exit 1
	fi
	if [ x"$stage" = "xinstall" -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/ti-utils
		#NFSROOT=${ROOTFS} make install || exit 1
		if [ ! -x calibrator ]
		then
			echo "calibrator is not built, run 'make' first"
			exit 1
		fi
		cp -f ./calibrator ${ROOTFS}/home/root
		chmod 755 ${ROOTFS}/home/root/calibrator
	fi
	if [ x"$stage" = "xclean" ]
	then
		cd ${WORK_SPACE}/ti-utils
		NFSROOT=${ROOTFS} make clean
		rm -f ${ROOTFS}/home/root/calibrator ${ROOTFS}/home/root/wl12xx-tool.sh
	fi
	cd $WORK_SPACE
}
function ti-utils-firmware()
{
	stage=$1
	 
	if [ x"$stage" = "xinstall" -o x"$stage" = "xall" ]
	then
		mkdir -p $ROOTFS/lib/firmware/ti-connectivity
		cp ${WORK_SPACE}/ti-utils/hw/firmware/* $ROOTFS/lib/firmware/ti-connectivity
		rm -f $ROOTFS/lib/hw/firmware/ti-connectivity/Makefile
		cp -r ${WORK_SPACE}/ti-utils/hw/ini_files $ROOTFS/lib/firmware/ti-connectivity
	fi
	if [ x"$stage" = "xclean" ]
	then
		rm -rf $ROOTFS/lib/firmware/ti-connectivity
	fi
	cd $WORK_SPACE
}

function make_hostapd_defconfig ()
{
	cat > .config <<"hostapd_defconfig"
# Example hostapd build time configuration
#
# This file lists the configuration options that are used when building the
# hostapd binary. All lines starting with # are ignored. Configuration option
# lines must be commented out complete, if they are not to be included, i.e.,
# just setting VARIABLE=n is not disabling that variable.
#
# This file is included in Makefile, so variables like CFLAGS and LIBS can also
# be modified from here. In most cass, these lines should use += in order not
# to override previous values of the variables.

DESTDIR=$(ROOTFS)
# Driver interface for Host AP driver
CONFIG_DRIVER_HOSTAP=y

# Driver interface for wired authenticator
#CONFIG_DRIVER_WIRED=y

# Driver interface for drivers using the nl80211 kernel interface
CONFIG_DRIVER_NL80211=y
CONFIG_LIBNL20=y
# driver_nl80211.c requires a rather new libnl (version 1.1) which may not be
# shipped with your distribution yet. If that is the case, you need to build
# newer libnl version and point the hostapd build to use it.
#LIBNL=/usr/src/libnl
#CFLAGS += -I$(LIBNL)/include
#LIBS += -L$(LIBNL)/lib

# Driver interface for FreeBSD net80211 layer (e.g., Atheros driver)
#CONFIG_DRIVER_BSD=y
#CFLAGS += -I/usr/local/include
#LIBS += -L/usr/local/lib
#LIBS_p += -L/usr/local/lib
#LIBS_c += -L/usr/local/lib

# Driver interface for no driver (e.g., RADIUS server only)
#CONFIG_DRIVER_NONE=y

# IEEE 802.11F/IAPP
CONFIG_IAPP=y

# WPA2/IEEE 802.11i RSN pre-authentication
CONFIG_RSN_PREAUTH=y

# PeerKey handshake for Station to Station Link (IEEE 802.11e DLS)
CONFIG_PEERKEY=y

# IEEE 802.11w (management frame protection)
# This version is an experimental implementation based on IEEE 802.11w/D1.0
# draft and is subject to change since the standard has not yet been finalized.
# Driver support is also needed for IEEE 802.11w.
#CONFIG_IEEE80211W=y

# Integrated EAP server
CONFIG_EAP=y

# EAP-MD5 for the integrated EAP server
CONFIG_EAP_MD5=y

# EAP-TLS for the integrated EAP server
CONFIG_EAP_TLS=y

# EAP-MSCHAPv2 for the integrated EAP server
CONFIG_EAP_MSCHAPV2=y

# EAP-PEAP for the integrated EAP server
CONFIG_EAP_PEAP=y

# EAP-GTC for the integrated EAP server
CONFIG_EAP_GTC=y

# EAP-TTLS for the integrated EAP server
CONFIG_EAP_TTLS=y

# EAP-SIM for the integrated EAP server
#CONFIG_EAP_SIM=y

# EAP-AKA for the integrated EAP server
#CONFIG_EAP_AKA=y

# EAP-AKA' for the integrated EAP server
# This requires CONFIG_EAP_AKA to be enabled, too.
#CONFIG_EAP_AKA_PRIME=y

# EAP-PAX for the integrated EAP server
#CONFIG_EAP_PAX=y

# EAP-PSK for the integrated EAP server (this is _not_ needed for WPA-PSK)
#CONFIG_EAP_PSK=y

# EAP-SAKE for the integrated EAP server
#CONFIG_EAP_SAKE=y

# EAP-GPSK for the integrated EAP server
#CONFIG_EAP_GPSK=y
# Include support for optional SHA256 cipher suite in EAP-GPSK
#CONFIG_EAP_GPSK_SHA256=y

# EAP-FAST for the integrated EAP server
# Note: Default OpenSSL package does not include support for all the
# functionality needed for EAP-FAST. If EAP-FAST is enabled with OpenSSL,
# the OpenSSL library must be patched (openssl-0.9.9-session-ticket.patch)
# to add the needed functions.
#CONFIG_EAP_FAST=y

# Wi-Fi Protected Setup (WPS)
CONFIG_WPS=y
# Enable WSC 2.0 support
CONFIG_WPS2=y
# Enable UPnP support for external WPS Registrars
CONFIG_WPS_UPNP=y

# EAP-IKEv2
#CONFIG_EAP_IKEV2=y

# Trusted Network Connect (EAP-TNC)
#CONFIG_EAP_TNC=y

# PKCS#12 (PFX) support (used to read private key and certificate file from
# a file that usually has extension .p12 or .pfx)
CONFIG_PKCS12=y

# RADIUS authentication server. This provides access to the integrated EAP
# server from external hosts using RADIUS.
#CONFIG_RADIUS_SERVER=y

# Build IPv6 support for RADIUS operations
CONFIG_IPV6=y

# IEEE Std 802.11r-2008 (Fast BSS Transition)
#CONFIG_IEEE80211R=y

# Use the hostapd's IEEE 802.11 authentication (ACL), but without
# the IEEE 802.11 Management capability (e.g., madwifi or FreeBSD/net80211)
#CONFIG_DRIVER_RADIUS_ACL=y

# IEEE 802.11n (High Throughput) support
CONFIG_IEEE80211N=y

# Remove debugging code that is printing out debug messages to stdout.
# This can be used to reduce the size of the hostapd considerably if debugging
# code is not needed.
#CONFIG_NO_STDOUT_DEBUG=y

# Remove support for RADIUS accounting
#CONFIG_NO_ACCOUNTING=y

# Remove support for RADIUS
#CONFIG_NO_RADIUS=y

# Remove support for VLANs
#CONFIG_NO_VLAN=y

# Remove support for dumping state into a file on SIGUSR1 signal
# This can be used to reduce binary size at the cost of disabling a debugging
# option.
#CONFIG_NO_DUMP_STATE=y

CONFIG_NO_RANDOM_POOL=y
LIBNL=$(ROOTFS)
CFLAGS += -I$(LIBNL)/include -I$(ROOTFS)/usr/include/
LIBS += -L$(LIBNL)/lib -L$(LIBNL)/lib  -L$(ROOTFS)/usr/lib -lssl -lcrypto -ldl
LIBS_p += -L$(LIBNL)/lib -L$(LIBNL)/lib  -L$(ROOTFS)/usr/lib -lssl -lcrypto -ldl
hostapd_defconfig
}

function hostap()
{
	stage=$1
	if [ x"$stage" = x"download" -o x"$stage" = "xall" ]
	then
		git_clone "${hostap_download_target}" hostap
		cd ${WORK_SPACE}/hostap
		git reset --hard "${hostap_commit_id}"
	fi
	if [ x"$stage" = "xbuild" -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/hostap/hostapd
		make_hostapd_defconfig
		make clean || exit 1
		make || exit 1
	fi
	if [ x"$stage" = "xinstall" -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/hostap/hostapd
		make install || exit 1
		for i in hostapd hostapd_cli; do cp -f $i ${ROOTFS}/usr/sbin/$i || exit 1; done
	fi
	if [ x"$stage" = "xclean" ]
	then
		cd ${WORK_SPACE}/hostap/hostapd
		make clean
		cd ../wpa_supplicant
		make clean
	fi
	cd $WORK_SPACE

}

function make_wpa_sup_defconfig ()
{
	cat > .config <<"wpa_sup_defconfig"
# Example wpa_supplicant build time configuration
DESTDIR=$(ROOTFS)
CFLAGS += -I$(ROOTFS)/usr/include/
LIBS += -L$(ROOTFS)/usr/lib
LIBS_p += -L$(ROOTFS)/usr/lib
CONFIG_WAPI=y
CONFIG_LIBNL20=y
NEED_BGSCAN=y
CONFIG_BGSCAN_LEARN=y
# Driver interface for generic Linux wireless extensions
CONFIG_DRIVER_WEXT=y
# Driver interface for Linux drivers using the nl80211 kernel interface
CONFIG_DRIVER_NL80211=y
# Driver interface for wired Ethernet drivers
CONFIG_DRIVER_WIRED=y
# Enable IEEE 802.1X Supplicant (automatically included if any EAP method is
# included)
CONFIG_IEEE8021X_EAPOL=y
# EAP-MD5
CONFIG_EAP_MD5=y
# EAP-MSCHAPv2
CONFIG_EAP_MSCHAPV2=y
# EAP-TLS
CONFIG_EAP_TLS=y
# EAL-PEAP
CONFIG_EAP_PEAP=y
# EAP-TTLS
CONFIG_EAP_TTLS=y
# EAP-GTC
CONFIG_EAP_GTC=y
# EAP-OTP
CONFIG_EAP_OTP=y
# LEAP
CONFIG_EAP_LEAP=y
# Wi-Fi Protected Setup (WPS)
CONFIG_WPS=y
# Enable WSC 2.0 support
CONFIG_WPS2=y
# PKCS#12 (PFX) support (used to read private key and certificate file from
# a file that usually has extension .p12 or .pfx)
CONFIG_PKCS12=y
# Smartcard support (i.e., private key on a smartcard), e.g., with openssl
# engine.
CONFIG_SMARTCARD=y
# Select control interface backend for external programs, e.g, wpa_cli:
# unix = UNIX domain sockets (default for Linux/*BSD)
# udp = UDP sockets using localhost (127.0.0.1)
# named_pipe = Windows Named Pipe (default for Windows)
# y = use default (backwards compatibility)
# If this option is commented out, control interface is not included in the
# build.
CONFIG_CTRL_IFACE=y
# Select configuration backend:
# file = text file (e.g., wpa_supplicant.conf; note: the configuration file
#	path is given on command line, not here; this option is just used to
#	select the backend that allows configuration files to be used)
# winreg = Windows registry (see win_example.reg for an example)
CONFIG_BACKEND=file
# PeerKey handshake for Station to Station Link (IEEE 802.11e DLS)
CONFIG_PEERKEY=y
# Add support for writing debug log to a file (/tmp/wpa_supplicant-log-#.txt)
CONFIG_DEBUG_FILE=y
LIBNL=$(ROOTFS)
CFLAGS += -I$(LIBNL)/include
LIBS += -L$(LIBNL)/lib  -lssl -lcrypto -ldl
LIBS_p += -L$(LIBNL)/lib  -lssl -lcrypto -ldl
# for p2p
CONFIG_P2P=y
CONFIG_AP=y
wpa_sup_defconfig
}

function wpa_supplicant ()
{
	stage=$1
	if [ x"$stage" = x"build" -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/hostap/wpa_supplicant
		make clean
		make_wpa_sup_defconfig
		make || exit 1
	fi
	if [ x"$stage" = x"install" -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/hostap/wpa_supplicant
		make install || exit 1
		for i in wpa_supplicant wpa_cli wpa_passphrase; do cp $i ${ROOTFS}/usr/sbin//$i || exit 1; done
	fi
	cd $WORK_SPACE
}

function bridge ()
{
	stage=$1
	if [ x"$stage" = x"download" -o x"$stage" = "xall" ]
	then
		download "http://downloads.sourceforge.net/project/bridge/bridge/bridge-utils-1.5.tar.gz" "bridge-utils-1.5.tar.gz"
		tar -xvzf bridge-utils-1.5.tar.gz
                cd ${WORK_SPACE}/bridge-utils-1.5
		autoconf
		./configure --prefix=${ROOTFS} CC=${CROSS_COMPILE}gcc LD=${CROSS_COMPILE}ld RANLIB=${CROSS_COMPILE}ranlib --host=arm-linux
		if [ $? != 0 ]
			then
			echo "bridge failed to be configured"
			exit 1
		fi
	fi
	if [ x"$stage" = "xbuild" -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/bridge-utils-1.5
		make || exit 1
	fi
	if [ x"$stage" = "xinstall" -o x"$stage" = "xall" ]
	then
		cd ${WORK_SPACE}/bridge-utils-1.5
		make install
		cp ./brctl/brctl ${ROOTFS}/usr/sbin/ || exit 1
	fi
	if [ x"$stage" = "xclean" ]
	then
	cd $WORK_SPACE/bridge-utils-1.5
#		make uninstall
		cd .. && rm -rf bridge-utils-1.5
	fi
	cd $WORK_SPACE
}



function usage ()
{
	echo "This script compiles one of following utilities: libnl, openssl, hostapd, wpa_supplicant,wl12xx_modules,firmware,crda,calibrator"
	echo "by calling specific utility name and action."
	echo "In case the options is 'all' all utilities will be downloaded and installed on root file system."
	echo "File setup-env contains all required environment variables, for example:"
	echo "	ROOTFS=<path to target root file system>."
	echo "Part of operations requires root access."
	echo "Usage: `basename $ME` target <libnl"
	echo "                              openssl"
	echo "                              hostapd"
	echo "                              wpa_supplicant"
	echo "                              wl12xx_modules"
	echo "                              firmware"
	echo "                              crda"
	echo "                              bridge"
	echo "                              calibrator>  action <download|build|install>"
	echo "                      all"
	echo "                      clean-all"
}

function check_libs()
{
	local openssl=`pkg-config --exists openssl`
	local libnl=`pkg-config --exists libnl-2.0`
	package=$1

	if [ $openssl -ne 0 -o $libnl -ne 0 ]
	then
		echo "Cannot build $1: openssl and libnl should be installed first."
		exit 1
	fi
}
function package_dir_exists()
{
	if [ -d "$1" ]
	then
		echo "Package $2 already downloaded at: $1"
		return 1
	fi
	return 0
}
function check_env()
{
	[ -e ${WORK_SPACE}/.check_env.stamp ] && return 0
	which dpkg 2>&1>/dev/null || return 0
	err=0
	ret=0
	packages="python python-m2crypto bash bison flex perl bc corkscrew"
	for p in ${packages}
	do
		echo -n "Checking ${p}..."
		present=`dpkg --get-selections ${p} 2>/dev/null | awk '{print $1}'`
		[ x"${present}" != x"${p}" ] && echo "Package ${p} is not found. Please run 'apt-get install ${p}' to install it." && err=1 && ret=1
		[ ${err} -ne 1 ] && echo "OK"
		err=0
	done
	return ${ret}
}

############################# MAIN ##############################################
# First building environment should be checked
check_env || exit 1
if [ -z $CROSS_COMPILE ]
then
	#lets find some
	tool_path=`which arm-none-linux-gnueabi-gcc`
	if [ $? -ne 0 ]
	then
		echo "No tool chain is found"
		exit 1
	fi	
	export CROSS_COMPILE=`dirname $tool_path`/arm-none-linux-gnueabi-
fi

if [ -z $KLIB_BUILD ]
then
	echo "Path to kernel sources has to be defined"
	exit 1
fi

if [ -z $ROOTFS ]
then
	echo "No path to root file system"
	exit 1
fi
argc=$#
if [ $argc -lt 1 ]
then
	usage
	exit 1
elif [ $argc -eq 1 ]
then
	if [ x"$1" != x"all" -a x"$1" != x"clean-all" ]
	then
		usage
		exit 1
	else
		package="$1"
	fi
fi

if [ $argc -eq 2 ]
then
	package=$1
	stage=$2
fi
if [ ! -d $WORK_SPACE ]
then
	mkdir -p $WORK_SPACE
	touch ${WORK_SPACE}/.check_env.stamp
fi
cd $WORK_SPACE

case $package in
	libnl)
		case $stage in
			download )
				package_dir_exists ${WORK_SPACE}/libnl-2.0 libnl-2 && libnl "download"
				;;
			build)
				package_dir_exists ${WORK_SPACE}/libnl-2.0 libnl-2
				if [ ! $? ]
				then
					libnl "download"
				fi
				cd ${WORK_SPACE}/libnl-2.0
				libnl "build"
				;;
			install)
				package_dir_exists ${WORK_SPACE}/libnl-2.0 libnl-2
				if [ ! $? ]
				then
					libnl "all" && exit
				else
					cd ${WORK_SPACE}/libnl-2.0
					libnl "install"
				fi
				;;
			all)
				package_dir_exists ${WORK_SPACE}/libnl-2.0 libnl-2 || rm -rf libnl-2.0
				libnl "all"
				;;
			*)
				echo "Error: illegal action for libnl"
				exit 1
		esac
		;;
	openssl)
		case $stage in
			download )
				package_dir_exists ${WORK_SPACE}/openssl-1.0.0d openssl || exit 1
				openssl "download"
				;;
			build)
				package_dir_exists ${WORK_SPACE}/openssl-1.0.0d openssl
				if [ ! $? ]
				then
					openssl "download"
				fi
				cd ${WORK_SPACE}/openssl-1.0.0d
				openssl "build"
				;;
			install)
				package_dir_exists ${WORK_SPACE}/openssl-1.0.0d openssl
				test [ ! $? ] && openssl "all" && exit 0
				cd ${WORK_SPACE}/openssl-1.0.0d
				openssl "install"
				;;
			all)
				package_dir_exists ${WORK_SPACE}/openssl-1.0.0d openssl || rm -rf openssl-1.0.0d
				openssl "all"
				;;
		esac
		;;
	iw)
		case $stage in
			download )
				package_dir_exists ${WORK_SPACE}/iw iw || exit 1
				iw "download"
				;;
			build)
				package_dir_exists ${WORK_SPACE}/iw iw
				[ ! $? ] && iw "download"
				check_libs iw
				iw "build"
				;;
			install)
				package_dir_exists ${WORK_SPACE}/iw iw
				[ ! $? ] && iw "donwload"
				[ ! -x ${WORK_SPACE}/iw/iw ] && iw "build"
				iw "install"
				;;
			all)

				package_dir_exists ${WORK_SPACE}/iw iw || rm -rf ${WORK_SPACE}/iw
				iw "all"
				;;
			*)
				echo "Error: illegal action for iw"
				exit 1
				;;
		esac
		;;
	hostapd)
		case $stage in
			download)
				package_dir_exists ${WORK_SPACE}/hostap hostapd || exit 1
				hostap "download"
				;;
			build)
				if [ ! -d ${WORK_SPACE}/hostap ]
				then
					hostap "download"
				fi
				cd ${WORK_SPACE}/hostap
				hostap "build"
				;;
			install)
				if [ ! -d ${WORK_SPACE}/hostap ]
				then
					hostap "download"
				fi
				if [ ! -e ${WORK_SPACE}/hostap/hostapd/hostapd ]
				then
					hostap "build"
				fi
				hostap "install"
				;;
			all)
				package_dir_exists ${WORK_SPACE}/hostap hostapd || rm -rf ${WORK_SPACE}/hostap
				hostap "all"
				;;
			*)
				echo "Error: illegal action for hostapd"
				exit 1
				;;
		esac
		
		;;
	wpa_supplicant)
		case $stage in
			download)
				package_dir_exists ${WORK_SPACE}/hostap wpa_supplicant || exit 1
				hostap "download"
				;;
			build)
				if [ ! -e ${WORK_SPACE}/hostap ]
				then
					hostap "download"
				fi
				wpa_supplicant "build"
				;;
			install)
				if [ ! -e ${WORK_SPACE}/hostap ]
				then
					hostap "download"
				fi
				if [ ! -e ${WORK_SPACE}/hostap/wpa_supplicant/wpa_supplicant ]
				then
					wpa_supplicant "build"
				fi
				wpa_supplicant "install"
				;;
			*)
				echo "Error: illegal action for hostapd"
				exit 1
				;;
		esac
		;;
	wl12xx_modules)
		case $stage in
			download)
				package_dir_exists ${WORK_SPACE}/compat-wireless compat-wireless || exit 1
				compat-wireless "download"
				;;
			build)
				if [ ! -d ${WORK_SPACE}/compat-wireless ]
				then
					compat-wireless "download"
				fi
				cd ${WORK_SPACE}/compat-wireless
				compat-wireless "build"
				;;
			install)
				if [ ! -d ${WORK_SPACE}/compat-wireless ]
				then
					compat-wireless "all"
				else
					cd ${WORK_SPACE}/compat-wireless
					compat-wireless "install"
				fi
				;;
			all)
				package_dir_exists ${WORK_SPACE}/compat-wireless compat-wireless || rm -rf ${WORK_SPACE}/compat-wireless
				compat-wireless "all"
				;;
			*)
				echo "Error: illegal action for wl12xx_modules"
				exit 1
				;;
		esac

		;;
	calibrator)
		case $stage in
			download)
				if [ -d ${WORK_SPACE}/ti-utils ]
				then
					echo "Calibrator is part of ti-utils package that already exists at: ${WORK_SPACE}/ti-utils"
					exit 0
				fi
				ti-utils "download"
				;;
			build)
				if [ ! -d ${WORK_SPACE}/ti-utils ]
				then
					ti-utils "download"
				fi
				cd ${WORK_SPACE}/ti-utils
				ti-utils "build"
				;;
			install)
				if [ ! -d ${WORK_SPACE}/ti-utils ]
				then
					ti-utils "all"
				else
					if [ ! -e ${WORK_SPACE}/ti-utils/calibrator ]
					then
						ti-utils "build"
					fi
					ti-utils "install"
				fi
				;;
			all)
				ti-utils "all"
				;;
			*)
				echo "Error: illegal action for calibrator"
				exit 1
				;;
		esac
		;;
	firmware)
		if [  x$stage = "xclean" -o x$stage = "xinstall"  -o x$stage = "xall"   ]
		then

			if [ ! -d ti-utils/ ]
			then
				ti-utils "download"
			fi

			ti-utils-firmware $2 
		else
			echo "illegal action for firmware"
			exit 1
		fi
		;;
	crda)
		case $stage in
			download)
				package_dir_exists ${WORK_SPACE}/crda-1.1.1 crda || exit 1
				crda "download"
				;;
			build)
				if [ ! -d ${WORK_SPACE}/crda-1.1.1 ]
				then
					crda "download"
				fi
				cd ${WORK_SPACE}/crda-1.1.1
				crda "build"
				;;
			install)
				if [ ! -d ${WORK_SPACE}/crda-1.1.1 ]
				then
					crda "all"
				else
					cd ${WORK_SPACE}/crda-1.1.1
					crda "install"
				fi
				;;
			all)
				package_dir_exists ${WORK_SPACE}/crda-1.1.1 crda || rm -rf ${WORK_SPACE}/crda-1.1.1
				crda "all"
				;;
			*)
				echo "Error: illegal action for crda"
				exit 1
				;;
		esac
		;;
	bridge)
		case $stage in
			download)
				package_dir_exists ${WORK_SPACE}/bridge-utils-1.5 bridge || exit 1
				bridge "download"
				;;
			build)
				if [ ! -d ${WORK_SPACE}/bridge-utils-1.5 ]
				then
					bridge "download"
				fi
				cd ${WORK_SPACE}/bridge-utils-1.5
					bridge "build"
				;;
			install)
				if [ ! -d ${WORK_SPACE}/bridge-utils-1.5 ]
				then
					bridge "all"
				else
					cd ${WORK_SPACE}/bridge-utils-1.5
					bridge "install"
				fi
				;;
			all)
				package_dir_exists ${WORK_SPACE}/bridge-utils-1.5 bridge || rm -rf ${WORK_SPACE}/bridge-utils-1.5
				bridge "all"
				;;
			*)
				echo "Error: illegal action for bridge"
				exit 1
				;;
		esac
		;;
	all)
		echo "bridge ***********************************************************************************"
		bridge "all"
		echo "libnl ***********************************************************************************"
		libnl "all"
		echo "openssl ***********************************************************************************"
		openssl "all"
		echo "iw ***********************************************************************************"
		iw "all"
		echo "hostap ***********************************************************************************"
		hostap "all"
		echo "wpa_supplicant ***********************************************************************************"
		wpa_supplicant "all"
		echo "crda ***********************************************************************************"
		crda "all"
		echo "ti-utils ***********************************************************************************"
		ti-utils "all"
		echo "ti-utils-firmware ***********************************************************************************"
		ti-utils-firmware "all"
		echo "compat-wireless ***********************************************************************************"
		compat-wireless "all"
		;;
	clean-all)
		bridge "clean"
		compat-wireless "clean"
		ti-utils "clean"
		crda "clean"
		hostap "clean"
		iw "clean"
		openssl "clean"
		libnl "clean"
		ti-utils-firmware "clean"
		;;
	*)
		usage
		exit 1
esac